Page 1 of 1

String Replacement in data-updates.lua

Posted: Thu Dec 12, 2024 8:52 pm
by Pikachar
I'm currently working on a mod where I need to replace sprite sheets for some of the existing entities. For vanilla entities, I was able to just grab the file names from data.raw and use filename:gsub(old_loc, new_loc) to change the base location for the sprite sheets. This is not working for the Space Age entities. I can print out the filenames of these entities just fine, but they wont change when I try to swap the path info.

Again, it worked fine for the vanilla entities in data.lua. But since my mod seems to load before Space Age, I need to access them in data-updates.lua and substring replacement does not seem to be working correctly.
So, if somebody could tell me what I'm doing incorrectly, I'd appreciate it.

As always, any help is greatly appreciated!

Re: String Replacement in data-updates.lua

Posted: Thu Dec 12, 2024 9:09 pm
by s6x
If you need your mod to load after Space Age, you can just add space-age as a dependency. You can make it optional with a ? (e.g., "? space-age") if you also want the mod to be usable by vanilla Factorio.

Re: String Replacement in data-updates.lua

Posted: Fri Dec 13, 2024 12:53 am
by Pikachar
Well, good news-bad news situation now.
Good news: Adding the optional dependency on space-age allows me to find space-age entities in data.raw.
Bad news: Still cannot seem to do a substring replace on the filename for the space-age entity sprites.

For reference, I'm doing:

Code: Select all

space_dir = "__space-age__/graphics/entity/"
objects = {"biolab","captive-spawner"}
on_anim = data.raw["lab"]["biolab"].on_animation
on_anim.layers[k].filename = on_anim.layers[k].filename:gsub(space_dir,mod_dir)
log ('antitrypophobia --- AFTER ---- on_anim.layers[k].filename: ' .. serpent.block(on_anim.layers[k].filename))
where 'k' is the item number of layers.
And I'm getting:

Code: Select all

   1.878 Script @__antitrypophobia__/data.lua:38: antitrypophobia --- AFTER ---- on_anim.layers[k].filename: "__space-age__/graphics/entity/biolab/biolab-anim.png"
And I'm not sure why it's not working.

Re: String Replacement in data-updates.lua

Posted: Fri Dec 13, 2024 2:15 am
by s6x
The reason it works for base but not space-age is because - is a "magic character" that Lua uses for pattern matching. Change space_dir to "__space%-age__/graphics/entity/" and it should work.

Re: String Replacement in data-updates.lua

Posted: Fri Dec 13, 2024 3:26 am
by Pikachar
Yup. That was it. Thank you!