[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  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] Refresh page


Posted by Wingard   (6 posts)  [Biography] 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?
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] 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
[Go to top] top

Posted by Wingard   (6 posts)  [Biography] 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.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] 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.


17,129 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]