Modding help - How do I read/write the data table?

Place to get help with not working mods / modding interface.
wahming
Fast Inserter
Fast Inserter
Posts: 192
Joined: Sun May 17, 2015 8:30 pm
Contact:

Modding help - How do I read/write the data table?

Post by wahming »

The modding overview wiki page (https://forums.factorio.com/wiki/inde ... g_overview) gives the following snippet as an example, but I can't get it to work. I've tried placing it in control.lua and calling it from the console. Am I missing something obvious?

data.raw.ammo["piercing-bullet-magazine"].magazine_size = 20
User avatar
ThaPear
Fast Inserter
Fast Inserter
Posts: 226
Joined: Fri May 30, 2014 8:05 am
Contact:

Re: Modding help - How do I read/write the data table?

Post by ThaPear »

You need to place it in data.lua, as that file is loaded when the game initializes.
The file control.lua is only run when you start/load a game and I believe it is unable to alter the raw data.

It can also be placed in data-updates.lua or data-final-fixes.lua files, depending on when you want it to be executed.
First all data.lua files are executed, then all data-updates.lua files, then all data-final-fixes.lua files.

I'd recommend re-reading the page you linked, this information is available in the same chapter as the code you specified. ;)
wahming
Fast Inserter
Fast Inserter
Posts: 192
Joined: Sun May 17, 2015 8:30 pm
Contact:

Re: Modding help - How do I read/write the data table?

Post by wahming »

Oh.. is there any way to READ the raw data?

On a related question - I'm trying to create a custom transport belt, with an additional integer variable. This variable can be different for each instance of the belt, and should be writeable to me. What's the best way for me to do this?
User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Modding help - How do I read/write the data table?

Post by L0771 »

wahming wrote:Oh.. is there any way to READ the raw data?
game.entityprototypes have all entity prototipes.
Or if you have the entity (in onbuildentity for example), can call [entity].prototype, isn't like raw data, but is the best option for now, i think.
wahming wrote:On a related question - I'm trying to create a custom transport belt, with an additional integer variable. This variable can be different for each instance of the belt, and should be writeable to me. What's the best way for me to do this?
i don't know what do that "integer variable", but i think the best way to know about differences with some entities, is the name.
For example, you have 2 belts with the same speed, but change the color, with names "belt-red", "belt-yellow" and "belt-black", you can use game.player.print(string.sub([entity].name,6)) to write the color of entity.

But you can't edit the prototype in the game (data.raw), i don't know what variable is doing, if you need add a number to only 1 entity, like if 1 "belt-red" need know is the number 1, and all other without number, the best way is with a table, you can save the entity in a variable, for example with this, you need: belts = { entity = [entity], number = 1 }.

But i don't know what you need do.
wahming
Fast Inserter
Fast Inserter
Posts: 192
Joined: Sun May 17, 2015 8:30 pm
Contact:

Re: Modding help - How do I read/write the data table?

Post by wahming »

L0771 wrote:
wahming wrote:Oh.. is there any way to READ the raw data?
game.entityprototypes have all entity prototipes.
Or if you have the entity (in onbuildentity for example), can call [entity].prototype, isn't like raw data, but is the best option for now, i think.
What operations/methods are available for me on the prototype? I can get the reference to it, but not sure what to do with it.
L0771 wrote:
wahming wrote:On a related question - I'm trying to create a custom transport belt, with an additional integer variable. This variable can be different for each instance of the belt, and should be writeable to me. What's the best way for me to do this?
i don't know what do that "integer variable", but i think the best way to know about differences with some entities, is the name.
For example, you have 2 belts with the same speed, but change the color, with names "belt-red", "belt-yellow" and "belt-black", you can use game.player.print(string.sub([entity].name,6)) to write the color of entity.

But you can't edit the prototype in the game (data.raw), i don't know what variable is doing, if you need add a number to only 1 entity, like if 1 "belt-red" need know is the number 1, and all other without number, the best way is with a table, you can save the entity in a variable, for example with this, you need: belts = { entity = [entity], number = 1 }.
I need to have 1 number for each and every entity that exists in the game, of type "transport-belt". It can be different for each belt. Does that mean I need to create a huge table that contains every single transport belt in-game?
User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Modding help - How do I read/write the data table?

Post by L0771 »

wahming wrote:
L0771 wrote:
wahming wrote:Oh.. is there any way to READ the raw data?
game.entityprototypes have all entity prototipes.
Or if you have the entity (in onbuildentity for example), can call [entity].prototype, isn't like raw data, but is the best option for now, i think.
What operations/methods are available for me on the prototype? I can get the reference to it, but not sure what to do with it.
[entity].prototype.help()
I think are these
wahming wrote:
L0771 wrote:
wahming wrote:On a related question - I'm trying to create a custom transport belt, with an additional integer variable. This variable can be different for each instance of the belt, and should be writeable to me. What's the best way for me to do this?
i don't know what do that "integer variable", but i think the best way to know about differences with some entities, is the name.
For example, you have 2 belts with the same speed, but change the color, with names "belt-red", "belt-yellow" and "belt-black", you can use game.player.print(string.sub([entity].name,6)) to write the color of entity.

But you can't edit the prototype in the game (data.raw), i don't know what variable is doing, if you need add a number to only 1 entity, like if 1 "belt-red" need know is the number 1, and all other without number, the best way is with a table, you can save the entity in a variable, for example with this, you need: belts = { entity = [entity], number = 1 }.
I need to have 1 number for each and every entity that exists in the game, of type "transport-belt". It can be different for each belt. Does that mean I need to create a huge table that contains every single transport belt in-game?
Is the only way, treefarm have a table with every field and every tree on a field, i have something like 30 fields with... 25 trees everyone? i have more trees than belts, without problem.

With my experience, the only problem is overload the event ontick ;)
wahming
Fast Inserter
Fast Inserter
Posts: 192
Joined: Sun May 17, 2015 8:30 pm
Contact:

Re: Modding help - How do I read/write the data table?

Post by wahming »

L0771 wrote:
wahming wrote:
L0771 wrote:
wahming wrote:Oh.. is there any way to READ the raw data?
game.entityprototypes have all entity prototipes.
Or if you have the entity (in onbuildentity for example), can call [entity].prototype, isn't like raw data, but is the best option for now, i think.
What operations/methods are available for me on the prototype? I can get the reference to it, but not sure what to do with it.
[entity].prototype.help()
I think are these
Hmm. Not much info available from there. I wanted to get info like belt speed, for example. Guess it may not be possible.
L0771 wrote:
wahming wrote:
L0771 wrote:
wahming wrote:On a related question - I'm trying to create a custom transport belt, with an additional integer variable. This variable can be different for each instance of the belt, and should be writeable to me. What's the best way for me to do this?
i don't know what do that "integer variable", but i think the best way to know about differences with some entities, is the name.
For example, you have 2 belts with the same speed, but change the color, with names "belt-red", "belt-yellow" and "belt-black", you can use game.player.print(string.sub([entity].name,6)) to write the color of entity.

But you can't edit the prototype in the game (data.raw), i don't know what variable is doing, if you need add a number to only 1 entity, like if 1 "belt-red" need know is the number 1, and all other without number, the best way is with a table, you can save the entity in a variable, for example with this, you need: belts = { entity = [entity], number = 1 }.
I need to have 1 number for each and every entity that exists in the game, of type "transport-belt". It can be different for each belt. Does that mean I need to create a huge table that contains every single transport belt in-game?
Is the only way, treefarm have a table with every field and every tree on a field, i have something like 30 fields with... 25 trees everyone? i have more trees than belts, without problem.

With my experience, the only problem is overload the event ontick ;)
Ok, will try that, thanks!
Post Reply

Return to “Modding help”