Page 1 of 1
Help dividing entity stats based on existing entity stats
Posted: Sat Jan 13, 2024 4:50 am
by DwarfTech
Hello all! I'm in need of a little help with balancing a new entity based on the stats of another. What I'm wanting to do is set the power use of the furnace from the 'Electrified Stone Furnace' mod (via a separate personal tweak mod) to always be half the energy consumption of the vanilla furnace, since that's the original ratio and some mods change furnace power needs.
Here's what I have so far, and I'm getting a syntax error near the == per the game on launch:
Code: Select all
if mods["wtxElectrifiedStoneFurnace"] then
data.raw[furnace][elstfu].energy_consumption == data.raw[furnace][stone-furnace].energy_consumption * .5
end
Any help with the syntax issue is much appreciated.
Re: Help dividing entity stats based on existing entity stats
Posted: Sat Jan 13, 2024 5:37 am
by Nidan
Assignment is "=", not "==". See the
Lua 5.2 manual.
Re: Help dividing entity stats based on existing entity stats
Posted: Sat Jan 13, 2024 6:10 am
by DwarfTech
Thanks. Now having issues with the stone furnace part, keeps saying attempted to index "stone-furnace", a nil value. Any help there? I'm new to this, but assumed referencing it via data.raw would be enough.
Edit: Through some manner of dithering, that error has disappeared, replaced by "attempted to perform arithmetic on field 'energy_consumption' (a nil value)"
I do apologize if these are simple errors, I've only dabbled in Lua on occasion with simple mods, and am more of a visual learner. How do I get the game to recognize 'energy_consumption' in my above code as the stone furnace's energy consumption?
Re: Help dividing entity stats based on existing entity stats
Posted: Sat Jan 13, 2024 6:50 am
by Stringweasel
It's
energy_usage and not `energy_consumption`.
Re: Help dividing entity stats based on existing entity stats
Posted: Sat Jan 13, 2024 7:09 am
by DwarfTech
Thanks for the help there. I went back and double checked the energy section, and I have no idea where I got the energy_consumption from. Changing it to energy_usage did indeed clear that error up. Now I'm presented with an error about performing arithmetic on field 'energy_usage' (a string value). How would I go about converting it to a pure number value?
Edit: I've tried using tonumber() to convert it but to no success. The variable I set up to use, num, still comes back nil upon loading.
Re: Help dividing entity stats based on existing entity stats
Posted: Sat Jan 13, 2024 7:53 am
by Stringweasel
If you look at the actual data you're trying to use it's not really a number but a string "90kW". You van find it
here, or in your game files. So you need to convert it. Luckily Factoeio provides a
tool to do this. Remember, you'll to convert it back again
Code: Select all
local stone_furnace_power = ...
local new_power = (util.parse_energy(stone_furnace_power) / 2) .. "W"
Re: Help dividing entity stats based on existing entity stats
Posted: Sat Jan 13, 2024 10:43 pm
by DwarfTech
Thank ye. I've revised using your advice and code to test, it does clear that error up. That said, I'm encountering another error, this time as follows (attached as a screenshot). I'll also attach the full code block as it is right now:
Code: Select all
if mods["wtxElectrifiedStoneFurnace"] then
local stone_furnace_power = ...
local electrified_power = (util.parse_energy(stone_furnace_power) / 2) .. "W"
data.raw["furnace"]["elstfu"].energy_usage = electrified_power
end
What might I need to modify to clear up this error?
Re: Help dividing entity stats based on existing entity stats
Posted: Sun Jan 14, 2024 12:12 am
by Qon
DwarfTech wrote: Sat Jan 13, 2024 10:43 pm
What might I need to modify to clear up this error?
If you read the error message yourself then maybe you would figure it out?
You weren't supposed to just paste in "...", the variable named "stone_furnace_power" needs to hold the value of the power used by the stone furnace. Did you think "..." would magically supply the number you were thinking of in your head without actually having to write in the code what you want to happen?
You had data.raw[furnace][stone-furnace].energy_usage in the code before. Did you just removed it and thought that you didn't need your input value any more?
"..." is the varargs operator in Lua, is your code in a varargs function? You would still need to extract the correct indexed value, parse_energy is expecting a string not a table.
Re: Help dividing entity stats based on existing entity stats
Posted: Sun Jan 14, 2024 1:47 am
by DwarfTech
Okay okay man, no need to be aggressive about it. I'm far from a professional modder/programmer and make simple mistakes all the time. When I pasted it to work things out I forgot to add the stone furnace bit back in. And I saw the error myself, else I wouldn't have posted it here. Anyway, thanks for mentioning it, I added it back in.
Re: Help dividing entity stats based on existing entity stats
Posted: Sun Jan 14, 2024 2:24 am
by DwarfTech
Everything seems to work now peachily, thanks everybody for all the help with my troubleshooting.