Page 1 of 1

[0.10.9] crash on game.player.gui.top.add

Posted: Wed Sep 10, 2014 1:39 pm
by jeroon
So I finally got around to do some modding, and as soon as I try to add a single GUI item, the game crashes on me :/

win 7 64 bit

this is my control.lua

Code: Select all

require "defines"

if game.player.gui.top.menu_top == nil then
    game.player.gui.top.add({type="frame", name="menu_top", caption="Factorio Maps", direction="vertical"})
end
no clue if it's bad code, but my guess is, it shouldn't crash the game :)

code works fine when entered from the console ingame.

Re: [0.10.9] crash on game.player.gui.top.add

Posted: Wed Sep 10, 2014 2:08 pm
by kovarex
Hello, the problem is, that the code is put in the "root context", in the file, that is processed before the game is fully initialised to setup some basic "stuff".

The correct way to do it would be:

Code: Select all

require "defines"
game.oninit(function()
  if game.player.gui.top.menu_top == nil then
    game.player.gui.top.add({type="frame", name="menu_top", caption="Factorio Maps", direction="vertical"})
  end
end)
The truth is, that it shouldn't crash, but give some nice error message instead. There is a way for us to solve it, but we have no time to solve it now, and we also don't want to make bigger changing of the script interface in the release stabilistation, so I'm moving this to minor issues.

Re: [0.10.9] crash on game.player.gui.top.add

Posted: Wed Sep 10, 2014 2:43 pm
by jeroon
aah, thanks, that fixed it :)
(for anyone interested: you need game.onload as well, if you use a save game instead of starting a new game)