String Replacement in data-updates.lua

Place to get help with not working mods / modding interface.
Pikachar
Inserter
Inserter
Posts: 32
Joined: Wed Apr 19, 2023 9:16 pm
Contact:

String Replacement in data-updates.lua

Post 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!
s6x
Inserter
Inserter
Posts: 34
Joined: Fri Nov 15, 2024 8:22 pm
Contact:

Re: String Replacement in data-updates.lua

Post 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.
Pikachar
Inserter
Inserter
Posts: 32
Joined: Wed Apr 19, 2023 9:16 pm
Contact:

Re: String Replacement in data-updates.lua

Post 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.
s6x
Inserter
Inserter
Posts: 34
Joined: Fri Nov 15, 2024 8:22 pm
Contact:

Re: String Replacement in data-updates.lua

Post 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.
Pikachar
Inserter
Inserter
Posts: 32
Joined: Wed Apr 19, 2023 9:16 pm
Contact:

Re: String Replacement in data-updates.lua

Post by Pikachar »

Yup. That was it. Thank you!
Post Reply

Return to “Modding help”