[RESOLVED] control.lua script question?...

Place to get help with not working mods / modding interface.
User avatar
Volvith
Inserter
Inserter
Posts: 40
Joined: Mon Jun 26, 2017 12:28 pm
Contact:

[RESOLVED] control.lua script question?...

Post by Volvith »

I've been messing with mods for a little while now, and i've got a pretty good idea of how mods function when it comes to prototypes and such.
However, i have no idea how to do anything in control.lua without copy&pasting pre-existing code into it, and changing it a bit to fit my needs.

What i'm trying to do below is to get this "command" script to run each time i fire up a save, or when i start a new game.
For the new game, i have managed to get an on_event/on_player_created script running.
For the on_load, when loading a game (new game works fine), however, Factorio gives me the error:
Error while running the on_load:
__modname__/control.lua:2: attempt to index global 'game' (a nil value)
So, what do i have to do with the "local player = game.players... etc." line?...

Also looking for a bit of guidance here, because i really want to understand scripting in control.lua, but i simply CANNOT make sense of the API, or anything i read, and i have absolutely no fucking clue where to even start looking. T_T
Script
Last edited by Volvith on Tue Aug 01, 2017 3:30 pm, edited 2 times in total.
Rational People: If it ain't fucked, don't fuck it.
Engineers: If it ain't fucked, unfuck it till it's perfect.
Me as an Engineer: If it ain't fucked, i'll fuck it till it is.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5290
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: control.lua script question?...

Post by Klonan »

Volvith wrote:I've been messing with mods for a little while now, and i've got a pretty good idea of how mods function when it comes to prototypes and such.
However, i have no idea how to do anything in control.lua without copy&pasting pre-existing code into it, and changing it a bit to fit my needs.

What i'm trying to do below is to get this "command" script to run each time i fire up a save, or when i start a new game.
For the new game, i have managed to get an on_event/on_player_created script running.
For the on_load, when loading a game (new game works fine), however, Factorio gives me the error:
Error while running the on_load:
__modname__/control.lua:2: attempt to index global 'game' (a nil value)
So, what do i have to do with the "local player = game.players... etc." line?...

Also looking for a bit of guidance here, because i really want to understand scripting in control.lua, but i simply CANNOT make sense of the API, or anything i read, and i have absolutely no fucking clue where to even start looking. T_T
Script
You are using on_load incorrectly, it does not and should not access any game objects

secondly on_load isnt a game event, so no 'event' field will be present, so event.player_index will give you an error

So you should use on_init or on_player_created
User avatar
Volvith
Inserter
Inserter
Posts: 40
Joined: Mon Jun 26, 2017 12:28 pm
Contact:

Re: control.lua script question?...

Post by Volvith »

Klonan wrote: You are using on_load incorrectly, it does not and should not access any game objects
secondly on_load isnt a game event, so no 'event' field will be present, so event.player_index will give you an error

So you should use on_init or on_player_created
Yeah, but the point is that on_player_created does not execute when you load a pre-existing file, and neither does on_init.
I'm trying to get a script with the result above to run when i load a pre-existing game, and i have no idea how to accomplish something like that, as i'm only just starting to learn...

Nevermind, found a way to make it work with shortcuts instead of reloading, which works better for mod-testing purposes.
Rational People: If it ain't fucked, don't fuck it.
Engineers: If it ain't fucked, unfuck it till it's perfect.
Me as an Engineer: If it ain't fucked, i'll fuck it till it is.
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: control.lua script question?...

Post by Bilka »

You can use _on_configuration_changed. it get's called whenever anything in your mod files changes, which means that you can use it to execute things "on load" for mod testing purposes, since your mod almost always changes when you reload the game.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
Volvith
Inserter
Inserter
Posts: 40
Joined: Mon Jun 26, 2017 12:28 pm
Contact:

Re: control.lua script question?...

Post by Volvith »

Bilka wrote:You can use _on_configuration_changed. it get's called whenever anything in your mod files changes, which means that you can use it to execute things "on load" for mod testing purposes, since your mod almost always changes when you reload the game.
Oh, that's actually pretty useful!
Right now i just created a bunch of custom shortcuts to which i linked the scripts which need to be executed, but i'll keep this one in mind. (I thought on_configuration_changed meant enabling/disabling/adding/removing entire mods only. Oops. :P)
Rational People: If it ain't fucked, don't fuck it.
Engineers: If it ain't fucked, unfuck it till it's perfect.
Me as an Engineer: If it ain't fucked, i'll fuck it till it is.
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: [RESOLVED] control.lua script question?...

Post by Bilka »

Volvith wrote:(I thought on_configuration_changed meant enabling/disabling/adding/removing entire mods only. Oops. :P)
I also remembered that that might be the case, so I looked at the api and decided to ask Rseding and... control.lua changes do not trigger it.. sorry! Image
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
Volvith
Inserter
Inserter
Posts: 40
Joined: Mon Jun 26, 2017 12:28 pm
Contact:

Re: [RESOLVED] control.lua script question?...

Post by Volvith »

Bilka Quote
Oh, don't worry, i haven't tried messing with it yet.
I'll look into a better solution for the future, but for now, the keybinds seem to work pretty well. :P
Cheers mate! ;)
Rational People: If it ain't fucked, don't fuck it.
Engineers: If it ain't fucked, unfuck it till it's perfect.
Me as an Engineer: If it ain't fucked, i'll fuck it till it is.
Post Reply

Return to “Modding help”