Page 1 of 1

[SOLVED] on_tick event not working

Posted: Tue Jan 17, 2017 12:36 am
by TigBits
I'm trying to update my clock mod from v0.11.22 to 0.14.21. I have fixed all of the errors but the on_tick event still isn't working.

This is the code I am running. When I start a new game, I get the clock label in the upper left corner but it never changes. None of the four test lines in the on_tick function do anything. Does anybody know what's wrong here?

Code: Select all

local function create_clock(playerindex)
	local player = game.players[playerindex]
	player.gui.left.add{type="label", name="clock", caption="clock"}
	player.gui.left.clock.style.font_color = {r=1, g=1, b=1}
	player.gui.left.clock.style.font = "default-bold"
end

script.on_event(defines.events.on_player_created, function(event)
	create_clock(event.player_index)
end)

script.on_event(defines.events.on_tick, function(event)
	for playerindex, player in ipairs(game.players) do
		if (player.gui.left.clock == nil) then
			create_clock(playerindex)
		end
		-- the next four lines test to make sure this function works
		player.gui.left.clock.caption = "Hello"
		game.players[playerindex].gui.left.clock.caption = "Hello"
		game.players[playerindex].print("Hello")
		player.print("Hello")
	end
end)
Screenshot of clock label
Untitled.png
Untitled.png (20.06 KiB) Viewed 1631 times

Re: [0.14.21] on_tick event not working

Posted: Tue Jan 17, 2017 12:45 am
by Klonan
TigBits wrote:

Code: Select all

script.on_event(defines.events.on_tick, function(event)
	for playerindex, player in ipairs(game.players) do
		if (player.gui.left.clock == nil) then
			create_clock(playerindex)
		end
		-- the next four lines test to make sure this function works
		player.gui.left.clock.caption = "Hello"
		game.players[playerindex].gui.left.clock.caption = "Hello"
		game.players[playerindex].print("Hello")
		player.print("Hello")
	end
end)
ipairs doesn't work any longer on game.players, just use pairs and it will work

Re: [0.14.21] on_tick event not working

Posted: Tue Jan 17, 2017 12:48 am
by TigBits
Klonan wrote:ipairs doesn't work any longer on game.players, just use pairs and it will work
Thanks, I realized after I posted that it was actually the loop that was failing to work, but I was still looking into why.

I have the mod updated and fully working now.