I have the following function, and a variable called "momentum", which will always be captured as a string but needs to be converted to an integer. However, momentum is also sometimes nil, which does not work here.
function stringconv(t, name)
local v = GetVariable(name)
local n = tonumber (v)
if n and tostring (n) == v then
return n
else
return v
end
end
Any idea how I can fix this?
Incredibly enough nick, that didn't work.
[string "Script file"]:16: bad argument #1 to 'GetVariable' (string expected, got nil)
stack traceback:
[C]: in function 'GetVariable'
[string "Script file"]:16: in function 'stringconv'
[string "Script file"]:28: in function 'katah'
[string "Alias: "]:1: in main chunk
I still get that error when running the function.
Here's exactly what's going on:
I have a function called katah, it relies on the "momentum" var to do several different attacks when it's called, depending on what my momentum currently is. I have stringconv, which you already know about.
Momentum is always captured as a string from my prompt, but when I have 0 momentum, there's nothing to capture, so it becomes Nil. When the katah function runs, the first thing it does is var.momentum = stringconv(momentum), to properly set momentum to an integer.
There's a difference between MUSHclient variables (which you access using Get/SetVariable) and Lua variables (which are written "as-is"). In the function you first posted, "name" is a Lua-space variable, but you are trying to look up the MUSHclient variable specified by that name.
What you probably want is something like this, assuming that this really is a Lua-space variable:
function stringconv(str)
return tonumber(str or 0)
end