Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Trying to convert nil to 0 with this command

Trying to convert nil to 0 with this command

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Wingard   (6 posts)  Bio
Date Sat 25 Apr 2009 11:56 PM (UTC)
Message
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?
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 26 Apr 2009 03:21 AM (UTC)
Message
Make it:


local v = GetVariable(name) or 0



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Wingard   (6 posts)  Bio
Date Reply #2 on Sun 26 Apr 2009 06:45 AM (UTC)
Message
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.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Sun 26 Apr 2009 06:33 PM (UTC)
Message
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

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


18,984 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.