Jump to content




termination eventname? [converting strings into special timeSettings]


  • You cannot reply to this topic
5 replies to this topic

#1 Goof

  • Members
  • 751 posts

Posted 24 January 2014 - 05:09 PM

Hello

I am making a game board, which shows the teams points countdown and all that stuff...

but im having trouble converting an "advanced" timesetting-string into seconds, or into another form.


the timesetting unconverted
"15m|30s"


the timesetting converted ( as I want it to be )
##--converted into seconds and then into a hour, minute, and a seconds variable:
hours=0
minutes=0
seconds=0
-- converting thingy here:

hours = convertedHours -- is now 0
minutes = convertedMinutes -- is now 15
seconds = convertedSeconds -- is now 30

At the beginning i tried with:
string.gsub(advancedtime,"|(.-)%a","")


I've tried with different string.gsub's but I find it really hard to find the matching patterns for this usage...

Any help is appreciated.


Thanks in Advance

Edited by Mikk809h, 24 January 2014 - 06:04 PM.


#2 CometWolf

  • Members
  • 1,283 posts

Posted 24 January 2014 - 05:24 PM

What you want is string.gmatch, not gsub. This will return the parts of the string matching your pattern.
local tTime = {}
for time,type in string.gmatch("15m|30s","(%d-)([smh])|?") do --get both the type(s, m or h) aswell as the time value
  tTime[type] = tonumber(time) --stores the time value in a table
end
tTime.s = tTime.s or 0 --ensure there's an actual value in each index
tTime.m = tTime.m or 0
tTime.h = tTime.h or 0
tTime["totSecs"] = tTime.s+(tTime.h*60*60)+(tTime.m*60) --calculate total seconds
print(tTime.s.." seconds\n"..tTime.m.." minutes\n"..tTime.h.." hours\n"..tTime.totSecs.." total seconds")

Edited by CometWolf, 24 January 2014 - 05:35 PM.


#3 Goof

  • Members
  • 751 posts

Posted 24 January 2014 - 05:38 PM

Ohh. That explains quite a lot, why my code didn't work :wacko:

Thank you very much for helping!

:D

#4 Goof

  • Members
  • 751 posts

Posted 24 January 2014 - 05:56 PM

Oh, and I've made a counter... but i cannot terminate my program when its running?
can anyone tell me what the eventname for the termination combination is?

Thanks

function calculation()
	while true do
		local counter=os.startTimer(1)
		local e = {coroutine.yield()}
		if e[1]=="timer" then
			if e[2]==counter then
				data["tavle"].currentSecondsLeft = data["tavle"].currentSecondsLeft - 1 -- "tavle" means board
				term.setCursorPos(1,1)
				term.write("TID tilbage: "..tostring(data["tavle"].currentSecondsLeft)) --- This is danish... therefore quite weird words xD
			end
		elseif e[1]=="termination" then --- this for example... what is the name for this event?
			error()
		end
	end
end

Edited by Mikk809h, 24 January 2014 - 05:58 PM.


#5 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 24 January 2014 - 06:33 PM

Because you do coroutine.yield.
os.pullEvent checks for termination and is a wrapper for os.pullEventRaw which is a wrapper for coroutine.yield.

EDIT:
Looked at the full code...
That isn't the problem.
The termination event is 'terminate' not 'termination'

Edited by Freack100, 24 January 2014 - 06:35 PM.


#6 Goof

  • Members
  • 751 posts

Posted 24 January 2014 - 06:38 PM

Oh Okay, Thank you!

:D





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users