Page 1 of 1

Create a data.raw modifying function

Posted: Fri Jul 29, 2016 12:31 pm
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)

Re: Create a data.raw modifying function

Posted: Fri Jul 29, 2016 12:50 pm
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.

Re: Create a data.raw modifying function

Posted: Fri Jul 29, 2016 12:51 pm
by Helfima
you must initialize the table before set properties.
try this

Re: Create a data.raw modifying function

Posted: Fri Jul 29, 2016 1:46 pm
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).

Re: Create a data.raw modifying function

Posted: Fri Jul 29, 2016 1:56 pm
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.

Re: Create a data.raw modifying function

Posted: Fri Jul 29, 2016 2:01 pm
by Nexela
log() writes to the factorio current log no matter where its called.

Re: Create a data.raw modifying function

Posted: Fri Jul 29, 2016 2:06 pm
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 :)

Re: Create a data.raw modifying function

Posted: Fri Jul 29, 2016 2:50 pm
by Qfa
Works like a charm, thanks a lot to both of you !