Page 1 of 1

In on_load, 'game' is nil, only if mod is called 'bluMisc'.

Posted: Wed May 18, 2016 9:39 pm
by bluMyst
So this is a really weird problem.

Code: Select all

-- bluMisc_0.1.0/info.json
{
    "name": "bluMisc",
    "version":"0.1.0",
    "title":"", "description":"", "author":"", "contact":""
}

-- bluMisc_0.1.0/control.lua
script.on_load(function()
  return game.players[1].print('test')
end)
-- 'game' is nil and throws an error when trying to index it

Code: Select all

-- asdf_0.1.0/info.json
{
    "name": "asdf",
    "version":"0.1.0",
    "title":"", "description":"", "author":"", "contact":""
}

-- asdf_0.1.0/control.lua
script.on_load(function()
  return game.players[1].print('test')
end)
-- everything works fine, except 'test' doesn't get printed.
Just... what? The only thing I can think of is that I stored two integers in 'global' a while back, and I haven't bothered to manually remove them. But I'm not really sure if global persists between launches of the game anyway.

Re: In on_load, 'game' is nil, only if mod is called 'bluMisc'.

Posted: Wed May 18, 2016 9:48 pm
by Rseding91
"game" is always nil on_load by design because you're not meant to do anything with it on_load.

There are only 2 things meant to be done on_load:

1. Re-register event handlers (if you do conditional event registration)
2. Re-setup meta-tables (if you use meta-tables)

Other than those 2 things you shouldn't need to do anything on_load.

Re: In on_load, 'game' is nil, only if mod is called 'bluMisc'.

Posted: Wed May 18, 2016 9:53 pm
by bluMyst
Rseding91 wrote:"game" is always nil on_load by design because you're not meant to do anything with it on_load.

There are only 2 things meant to be done on_load:

1. Re-register event handlers (if you do conditional event registration)
2. Re-setup meta-tables (if you use meta-tables)

Other than those 2 things you shouldn't need to do anything on_load.
Huh, okay. Thanks for the info. :) In that case, how do I make it so that as soon as the player enters the game, I create a GUI element? I'm trying to extend the game's HUD.

Edit: Oooh, wait! I bet on_player_created would work.

Edit2: It seems to work for the normal gamemode, but in sandbox it doesn't. Probably because you aren't a player, you're sort of a floating camera without a body. Does anyone know of an event that works for both?

Re: In on_load, 'game' is nil, only if mod is called 'bluMisc'.

Posted: Wed May 18, 2016 10:22 pm
by Klonan
bluMyst wrote:
Rseding91 wrote:"game" is always nil on_load by design because you're not meant to do anything with it on_load.

There are only 2 things meant to be done on_load:

1. Re-register event handlers (if you do conditional event registration)
2. Re-setup meta-tables (if you use meta-tables)

Other than those 2 things you shouldn't need to do anything on_load.
Huh, okay. Thanks for the info. :) In that case, how do I make it so that as soon as the player enters the game, I create a GUI element? I'm trying to extend the game's HUD.

Edit: Oooh, wait! I bet on_player_created would work.

Edit2: It seems to work for the normal gamemode, but in sandbox it doesn't. Probably because you aren't a player, you're sort of a floating camera without a body. Does anyone know of an event that works for both?
on_init and on_configuration_changed should cover all your cases

Re: In on_load, 'game' is nil, only if mod is called 'bluMisc'.

Posted: Wed May 18, 2016 10:29 pm
by Rseding91
on_player_created works just fine in the sandbox mode. That's how sandbox works from a scenario perspective - it removes the character on_player_created.