I just started modding and just finished my first attempt on a mod. On the way to this goal, I came across a problem, which some of you might have faced aswell. The forums search didn't help me much, so I'm asking here.
When I try to add some GUI-elements for example, I want them to be there for the entiry time. So I add them in the game.oninit event. At least, I tried! It simply won't work for me. Just to make sure, I'm not missing something, here is my attempt on achieving this:
# mod_directory\control.lua
Code: Select all
game.oninit(function()
game.player.gui.top.add{type = "button", name="some_button", caption={"button_name"}}
end)
I don't really understand why this happens, since in the base mod for example, game.player works pretty well in the game.oninit event.Unknown key:"Error while running the oninit: __test_mod__\control.lua:29: Map doesn't contain 1 player, this function can't be used"
Any help on this is greatly appreciated!
Greetings,
Schorty
P.S.: Just for the lolz: For now, my "solution" is this ugly piece of code:
Code: Select all
buttonsCreated = false
game.onevent(defines.events.ontick, function(event)
if buttonsCreated == false then
local player = game.getplayer(1)
if player ~= nil then
createButtons()
buttonsCreated = true
end
end
end)