Substring /remove kW

Place to get help with not working mods / modding interface.
Post Reply
AntiElitz
Filter Inserter
Filter Inserter
Posts: 446
Joined: Sat Aug 29, 2015 11:37 pm
Contact:

Substring /remove kW

Post by AntiElitz »

if trying to do this:

Code: Select all

local entity = data.raw["assembling-machine"]["assembling-machine-1"]
entity.energy_usage = entity.energy_usage * factor
However this does not work, since there it is a string with "kW" in it.
So i tried substr(entity.energy_usage, 1, strlen (entity.energy_usage) - 2) to get rid of the kW, but these lua function don't work in Factorio.

Any ideas? Thx for the help!



PS: I also look for a way to increase the item input buffer of an assembling machine (how many items are grabbed in when the assembler is on idle) - help is appreaciated ;)

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Substring /remove kW

Post by aubergine18 »

See if this works...

Code: Select all

local energy_usage = tonumber( entity.energy_usage:match('%d+') ) -- get number

entity.energy_usage = tostring( energy_usage * factor ) .. 'kW'
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

AntiElitz
Filter Inserter
Filter Inserter
Posts: 446
Joined: Sat Aug 29, 2015 11:37 pm
Contact:

Re: Substring /remove kW

Post by AntiElitz »

thx! that worked perfectly

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Substring /remove kW

Post by DedlySpyder »

You can get a substring btw, it's string.sub()

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Substring /remove kW

Post by aubergine18 »

DedlySpyder wrote:You can get a substring btw, it's string.sub()
Only if you know the length of the numeric portion of the string... which could be different if some other mod already changed the value to bigger/smaller number.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Substring /remove kW

Post by DedlySpyder »

aubergine18 wrote:
DedlySpyder wrote:You can get a substring btw, it's string.sub()
Only if you know the length of the numeric portion of the string... which could be different if some other mod already changed the value to bigger/smaller number.
string.sub(string, 1, (#string - 2))

You had the right idea originally, just wrong syntax

Rseding91
Factorio Staff
Factorio Staff
Posts: 13247
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Substring /remove kW

Post by Rseding91 »

Energy values must end with either "W" or "J". Other than that the 2nd to last character can be any one of the following: k, K, M, G, T, P ,E ,Z, Y.

So, using those rules you can parse out what the value is, remove it, do what you need to do, and then put back what ever symbols you want.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Modding help”