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

Place to get help with not working mods / modding interface.
bluMyst
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sun May 15, 2016 3:53 am
Contact:

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

Post 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.
Attachments
Untitled.png
Untitled.png (926.2 KiB) Viewed 1951 times
Last edited by bluMyst on Wed May 18, 2016 9:51 pm, edited 1 time in total.
In my infinite ambivalence.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15912
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.
bluMyst
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sun May 15, 2016 3:53 am
Contact:

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

Post 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?
In my infinite ambivalence.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5409
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

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

Post 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
Rseding91
Factorio Staff
Factorio Staff
Posts: 15912
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Modding help”