This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Goof's profile picture

termination eventname? [converting strings into special timeSettings]

Started by Goof, 24 January 2014 - 04:09 PM
Goof #1
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 on 24 January 2014 - 05:04 PM
CometWolf #2
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 on 24 January 2014 - 04:35 PM
Goof #3
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/>
Goof #4
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 on 24 January 2014 - 04:58 PM
H4X0RZ #5
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 on 24 January 2014 - 05:35 PM
Goof #6
Posted 24 January 2014 - 06:38 PM
Oh Okay, Thank you!

:D/>