Jump to content

loadtstring / eval math-string eg. "1+3"?


Recommended Posts

For a mod I want a modsetting that lets you set 2 values in one setting.
So sth like:
"Set start and end health:  option: "200 to 10000" -> data "200-10000"
Then I will do string.split() and tonumber to get the 200 and the 10000 from the data. This makes it easier.

But now I want values like "1/3 to 3" and the data would be "1/3-3"
Problem here is, that I cant eval the 1/3, loadstring does not work for some reason, telling me that all mathematical things like "+" or "/" are unknown.
Of course I could write 0.33333333333 instead, but this is ugly and not exact enough... but might be the only solution.
Or do you see another solution?

Edited by Serpens
Link to comment
Share on other sites

If your data is in quotations then it should be considered a string and so it should not give you these calculation errors. Can you provide an example from your modinfo.lua of how exactly you're defining these settings, and the code where you're reading and interpreting that data?

Link to comment
Share on other sites

the data is a string, yes, it is like I said for example "200-10000".

Then I do:

local upgrade_workingspeed = string.split(GetModConfigData("upgrade_workingspeed"),"-")
local TOOL_E_MINVALUE = GLOBAL.tonumber(upgrade_workingspeed[1])
local TOOL_E_MAXVALUE = GLOBAL.tonumber(upgrade_workingspeed[2])

and have the 200 and the 10000 stored in both local variables.

Doing it this way is much shorter and less work than making if/else statements like:

if upgrade_workingspeed=="200-10000" then
    TOOL_E_MINVALUE = 200
    TOOL_E_MAXVALUE = 10000
elseif...
elseif...
else...
end

(and also better than making 2 modsettings out of one, cause I already have too many settings :D)

The problem now is, that if the data string is "1/3-3" instead, so I would like to go it from 1/3 (=0.33333) to 3, then this code does not work, cause tonumber can not evaluate 1/3, nor can loadstring do this. And the question here is, what can evaluate a string "1/3" to the number 1/3 == 0.33333...

I "solved" it now by writing 0.333333333 into the string, instead of 1/3, but I would prefer a real solution.

Edited by Serpens
Link to comment
Share on other sites

Yeah, tonumber() can only directly convert string-numbers to a number. You can't parse a calculation like that. You could make your own wrapper function like this:

local mySettingConverter = function(settingString)
	if settingString:match('%/') then
		local splitString = string.split(settingString,"/")
		return tonumber(splitString[1]) / tonumber(splitString[2])
	else
		return tonumber(settingString)
	end
end

local upgrade_workingspeed = string.split(GetModConfigData("upgrade_workingspeed"),"-")
local TOOL_E_MINVALUE = mySettingConverter(upgrade_workingspeed[1])
local TOOL_E_MAXVALUE = mySettingConverter(upgrade_workingspeed[2])

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...