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

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Trying to convert nil to 0 with this command
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?

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)
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
return v

Any idea how I can fix this?
[Go to top] top

Posted by Nick Gammon   Australia  (21,607 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sun 26 Apr 2009 03:21 AM (UTC)
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)
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)
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)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

[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.


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.


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 FutureQuest]