[solved] "game is nil" in on_load method
Posted: Wed Aug 24, 2016 8:10 pm
Here's the thing, I have an on_load method implemented where I retrieve the player (singleplayer mode) and it fails when I use the local variable of the following line:
local lplayer = game.player
What I don't understand is WHY this works for a new game but not for a saved one. What's happening here with the "game"?
If anyone wants the context: I'm trying to make a "test mod" where I clean every inventory slot when a new world is generated. I'm doing it in the on_load method because the on_init method seems to not like the 'player' field of 'game' (so I've setup a global var to know if it is a new game)
Code below:
Thanks in advance!
local lplayer = game.player
What I don't understand is WHY this works for a new game but not for a saved one. What's happening here with the "game"?
If anyone wants the context: I'm trying to make a "test mod" where I clean every inventory slot when a new world is generated. I'm doing it in the on_load method because the on_init method seems to not like the 'player' field of 'game' (so I've setup a global var to know if it is a new game)
Code below:
Code: Select all
script:on_init(function(event)
global.empty_inventories = true
end)
script:on_load(function(event)
if global.empty_inventories == nil then
global.empty_inventories = true
if global.empty_inventories == true then
local lplayer = game.player
for i,6,1 do
lplayer.get_inventory(i).clear()
end
end
end)