Create a data.raw modifying function

Place to get help with not working mods / modding interface.
Qfa
Burner Inserter
Burner Inserter
Posts: 15
Joined: Tue May 31, 2016 7:29 pm
Contact:

Create a data.raw modifying function

Post by Qfa »

I'm currently trying to create a function to easily modify the subgroup and order of a very large part of the game's objects (items/guns/ammos/etc...)

Writting :

Code: Select all

data.raw["foo"]["bar"].subgroup = "baz"
data.raw["foo"]["bar"].subgroup = "qux"
1 time for each object worked just fine, but i'd like to create a function to do that

Something like

Code: Select all

modify = function(dataType, name, subgroup, order)
	data.raw[dataType][name].subgroup = subgroup
	data.raw[dataType][name].subgroup = order
end

modify("foo", "bar", "baz", "qux")
Problem :
The function in itself seems to work, but when a enter an incorrect value, for exemple : modify("itemINCORRECTVALUE", "land-mine", "weapons-capsule", "a"), the error I get on load is
attempt to index field '?' (a nil value)
How can get the error message to display where is the error ? In this case i'd like to have is
attempt to index field 'itemINCORRECTVALUE' (a nil value)
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Create a data.raw modifying function

Post by prg »

Not sure how to improve the error message itself, but you could just log() the parameters at the beginning of the function to help with debugging.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Helfima
Fast Inserter
Fast Inserter
Posts: 200
Joined: Tue Jun 28, 2016 11:40 am
Contact:

Re: Create a data.raw modifying function

Post by Helfima »

you must initialize the table before set properties.
try this
Qfa
Burner Inserter
Burner Inserter
Posts: 15
Joined: Tue May 31, 2016 7:29 pm
Contact:

Re: Create a data.raw modifying function

Post by Qfa »

thanks for these answers
Helfima wrote:you must initialize the table before set properties.
try this
I understand what you're saying but i'm not trying to modify the properties of a table which hasn't been already initialized in the base mod (which has been set as a dependence in the info.json file
Therefore I shouldn't need to initialize the table a second time, am I wrong?
prg wrote:Not sure how to improve the error message itself, but you could just log() the parameters at the beginning of the function to help with debugging.
I've tried logging the parameters, but the only way I've found to do this is to write the logs in an external file with the function game.write_file(), which doesn't seem to work when you're just starting Factorio (and loading the mods).
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Create a data.raw modifying function

Post by prg »

Qfa wrote:I've tried logging the parameters, but the only way I've found to do this is to write the logs in an external file with the function game.write_file(), which doesn't seem to work when you're just starting Factorio (and loading the mods).
log() already works during the data loading phase. It simply writes to the log file, factorio-current.log. print() also is always usable and writes to stdout which can be seen when running the game in a terminal. This is the most convenient way for me to get at debug output while developing.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Create a data.raw modifying function

Post by Nexela »

log() writes to the factorio current log no matter where its called.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Create a data.raw modifying function

Post by Nexela »

Since we know we have data.raw

Code: Select all

modify = function(dataType, name, subgroup, order)
   if data.raw[dataType] and data.raw[dataType][name] then
   -- Make sure these are both valid so we don't crash! lua lazy evaluation so if first fails second wont be called

   data.raw[dataType][name].subgroup = subgroup
   data.raw[dataType][name].order = order
else
  log("Datatype or name isn't valid" ..dataType .."."..name)
  --log message so we can figure out why
end
end

modify("foo", "bar", "baz", "qux")
[edit:typo and cleaned up code code in code :)
Last edited by Nexela on Fri Jul 29, 2016 3:53 pm, edited 3 times in total.
Qfa
Burner Inserter
Burner Inserter
Posts: 15
Joined: Tue May 31, 2016 7:29 pm
Contact:

Re: Create a data.raw modifying function

Post by Qfa »

Works like a charm, thanks a lot to both of you !
Post Reply

Return to “Modding help”