Forum

> > CS2D > Scripts > Creating custom timer and CS2D suddenly close
Forums overviewCS2D overview Scripts overviewLog in to reply

English Creating custom timer and CS2D suddenly close

4 replies
To the start Previous 1 Next To the start

old Creating custom timer and CS2D suddenly close

Dousea
User Off Offline

Quote
Recently I have a problem with cs2d lua cmd timer. I use this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
cs2d = {}

api = {}
api.timer = {}

function cs2d.timer (time, count, func, parameters)
	local timerfunction, timerargument, timerid = func .. ((parameters ~= nil and parameters:gsub (";", ""):gsub (" ", ""):gsub ("'", ""):gsub ("\"", "")) or ""), {}, 0
	
	for parameter in parameters:gmatch ("[^;]+") do
		if (type (tonumber (parameter)) == "number") then
			table.insert (timerargument, tonumber (parameter))
		else
			table.insert (timerargument, parameter)
		end
	end
	
	for index, othertimer in pairs (api.timer) do
		if (othertimer[2] == timerfunction) then
			timerid = index
			
			break
		end
	end
	
	if (timerid == 0) then
		timerid = #api.timer + 1
		
		table.insert (api.timer, {func, timerfunction, timerargument, count})
	end
	
	_G[timerfunction] = function (id)
		id = tonumber (id)
		api.timer[id][4] = api.timer[id][4] - 1
		
		if (api.timer[id][4] == 0) then
			_G[api.timer[id][2]] = nil
			api.timer[id] = nil
		elseif (timercount > 0) then
			_G[api.timer[id][1]] (unpack (api.timer[id][3]))
		end
	end
	
	return {
		start = function ()
			timer (time, timerfunction, timerid, count)
		end,
		remove = function ()
			if (api.timer[timerid] ~= nil) then
				freetimer (timerfunction)
				
				_G[timerfunction] = nil
				api.timer[timerid] = nil
			end
		end
	}
end

addhook ("spawn", "spawnhook")

function spawnhook (id)
	local timer = cs2d.timer (1000, 1, "localfunction", id .. ";cs2d.timer finished@C")
	
	function localfunction (id, message)
		msg2 (id, message)
	end
	
	timer.start ()
	timer.remove ()
end

Well when I spawn, after one second, CS2D suddenly close. There is no error message. Where is the problem?

old Re: Creating custom timer and CS2D suddenly close

DC
Admin Off Offline

Quote
I executed CS2D in BlitzMax debug mode and got an "PANIC: unprotected error in call to Lua API (attempt to index a nil value)" error. What you're passing to the cs2d lua cmd timer function as function-parameter is "localfunction1cs2d.timerfinished@C" which is not a valid function.

Not sure why CS2D crashes because I'm actually using a protected Lua API function call which should catch the error. I'll see if I can prevent the crash at this point and get CS2D to print meaningful error messages instead.

Edit: Nope, looks like I can't easily fix this without messing around in the Lua BlitzMax module... You however can simply fix your problem by calling a function which actually exists and which has a valid identifier
edited 2×, last 31.08.14 10:42:31 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview