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, 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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ A counter

A counter

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


Posted by Warbit   USA  (47 posts)  Bio
Date Tue 27 Sep 2011 01:14 PM (UTC)
Message
Trying to make a counter, but when I run it i get this error.


Error number: 0
Event:        Run-time error
Description:  [string "Trigger: "]:1: attempt to perform arithmetic on global 'wholistcounter' (a nil value)

stack traceback:

	[string "Trigger: "]:1: in main chunk
Called by:    Immediate execution

I try to initialize my variable wholistcounter with this trigger.

<triggers>
  <trigger
   enabled="y"
   match="*[pri sec ter qua qui hex sep oct]*"
   send_to="9"
   sequence="100"
   variable="wholistcounter"
  >
  <send>wholistcounter = 0</send>
  </trigger>
</triggers>

I try to count the number of people on the who list with this trigger.

<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="Solo"
   match="[*] * *"
   send_to="12"
   sequence="100"
  >
  <send>wholistcounter = wholistcounter + 1

Send("Say there are @wholistcounter ")
</send>
  </trigger>
</triggers>

I then get this error.


Error number: 0
Event:        Run-time error
Description:  [string "Trigger: "]:1: attempt to perform arithmetic on global 'wholistcounter' (a nil value)

stack traceback:

	[string "Trigger: "]:1: in main chunk
Called by:    Immediate execution

The wholist looks like this normally.

*Immortals of SlothMUD* 
--------------------------------
[   *Creator*   ] Splork www.wintin.org   vote vote
[     *God*     ] Tox #sub Nia Kolbor :)~

[pri sec ter qua qui hex sep oct]  Players
--------------------------------
[        *Seer*  Ma:7.38         ] Tuk !=kuT<<M.A.G.I>> HAS ICQ ADD ME!
[       *Bandit*  Th:4.38        ] Burl ~Dragonsworn~
[      *Myrmidon*  Wa:4.24       ] Linx ..
[40t 38w 21c 12m| 3n 12b  4d  0  ] Nookie wtb mu-jen doll and hexes
[20w 20t 20o 17b|11d 12n  9c  8m ] Warbit ...
[20t 20w 20c 10m| 0   0   0   0  ] Spuk da stabbing machine
[20w 20c 20m 10n|10b  6d  5t  4o ] Tonto saw winning as a good idea at the time.
[13m  2d  3c  1b| 1w  0   0   0  ] Emme ~
[11o 11w 11b 11t|11n 11d 11m 11c ] Alyhs the reverse
[ 9t  9o  9c  9m| 9w  9d  9n  9b ] Nassi ol thief
There are 12 visible players.
Highest total since last bootup: 38.

The reason it is important to me. Is because I want to make and array with all the names of the players online.
Use the array to see if another player is in the room with me.
If room is clear perform an action. If not clear leave the room and move onto another room.
But first I need figure out why i am getting the error and how to fix it.

I looked at this

4.2 - Local Variables and Blocks

Besides global variables, Lua supports local variables. We create local variables with the local statement:

    j = 10         -- global variable
    local i = 1    -- local variable

from:http://www.lua.org/pil/4.2.html
and I don't see the problem. Thanks for any help you can offer in fixing my issue.
Top

Posted by Daniel P   USA  (97 posts)  Bio
Date Reply #1 on Tue 27 Sep 2011 03:30 PM (UTC)

Amended on Tue 27 Sep 2011 03:33 PM (UTC) by Daniel P

Message
World variables (the kind you use with the @variable and expand_variables="y") can't be called with a simple variable=value. Those are local or global variables that die when the script finishes (ie. Trigger script completes, function finishes its task, world is closed). You have to use the SetVariable() and GetVariable() for world variables, and those will be listed in your World Properties/Variables list and will also be saved between sessions.

Example:

<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="Solo"
   match="[*] * *"
   send_to="12"
   sequence="100"
  >
  <send>SetVariable("wholistcounter",GetVariable("wholistcounter") + 1)

Send("Say there are @wholistcounter")
</send>
  </trigger>
</triggers>

...and the like.

Top

Posted by Warbit   USA  (47 posts)  Bio
Date Reply #2 on Tue 27 Sep 2011 10:04 PM (UTC)
Message
Thanks for fix. Tried it and still get same error. As far as I know wholistcounter is a variable I completely made up. If I am understanding world,global and local variables correctly. I copied the error message i got again,in case the change changed the error message in some way that I did not notice.

Error number: 0
Event:        Run-time error
Description:  [string "Trigger: "]:1: attempt to perform arithmetic on a string value

stack traceback:

	[string "Trigger: "]:1: in main chunk
Called by:    Immediate execution

Top

Posted by Nick Gammon   Australia  (23,057 posts)  Bio   Forum Administrator
Date Reply #3 on Wed 28 Sep 2011 06:34 AM (UTC)

Amended on Wed 28 Sep 2011 06:35 AM (UTC) by Nick Gammon

Message
The simple change is to make the counter zero if necessary. Change:


wholistcounter = wholistcounter + 1


to:


wholistcounter = (wholistcounter or 0) + 1


This idiom, which is mentioned in the Programming In Lua book, substitutes zero if wholistcounter is nil, which it will be the first time.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,057 posts)  Bio   Forum Administrator
Date Reply #4 on Wed 28 Sep 2011 06:36 AM (UTC)
Message
Or if you are going to do this:


SetVariable("wholistcounter",GetVariable("wholistcounter") + 1)


Make it a number, like this:


SetVariable("wholistcounter",
     (tonumber (GetVariable("wholistcounter")) or 0) + 1)

- Nick Gammon

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

Posted by Warbit   USA  (47 posts)  Bio
Date Reply #5 on Thu 29 Sep 2011 02:09 PM (UTC)
Message
Thanks Nick, changed it using my original script, with your fixes but it still is not working. I am getting no errors, but its not incrementing.

When I type who, the screen shows this.

<534/534hp 101/101ma 311/311mv>
who
*Immortals of SlothMUD* 
--------------------------------
[   *Creator*   ] Splork www.wintin.org New BETA available
Say there are wholistcounter = 0 
[     *God*     ] Tox -
Say there are wholistcounter = 0 

[pri sec ter qua qui hex sep oct]  Players
--------------------------------
[        *Seer*  Ma:7.38         ] Tuk !=kuT<<M.A.G.I>> join 4 chat @ Skype / ICQ
Say there are wholistcounter = 0 
[      *Myrmidon*  Wa:4.24       ] Wal <<M.A.G.I>> *Soul Knight*
Say there are wholistcounter = 0 
[      *Myrmidon*  Wa:4.24       ] Linx <<M.A.G.I>> ..
Say there are wholistcounter = 0 
[40d 40m 40c 33n| 2b  1o  1w  1t ] Suna the untitled
Say there are wholistcounter = 0 
[40t 40w 25c 12m|12n 12b  4d  2o ] Nookie  
Say there are wholistcounter = 0 
[40d 40m 22c 10n|10w  4b  1t  0  ] Garun 
Say there are wholistcounter = 0 
[32w 25n 20c 20m|12d 10b 10t 10o ] Roth ramping up the ladder
Say there are wholistcounter = 0 
[20m 20c 20n 20d|13w  8b  7t  6o ] Verm Icelli <<M.A.G.I>>
Say there are wholistcounter = 0 
[20w 20t 20o 17b|11d 12n  9c  8m ] Warbit ...
Say there are wholistcounter = 0 
[20w 20c 20m 10n|10b  6d  5t  4o ] Tonto saw winning as a good idea at the time.
Say there are wholistcounter = 0 
[ 9t  9o  9c  9m| 9w  9d  9n  9b ] Nassi ol thief
Say there are wholistcounter = 0 
[ 8w  2c  0   0 | 0   0   0   0  ] Shinnok the Untitled
Say there are wholistcounter = 0 
[ 2w  1d  1m  1c| 0   0   0   0  ] Colante .
Say there are wholistcounter = 0 
[ 1d  0   0   0 | 0   0   0   0  ] Vegas the Untitled
Say there are wholistcounter = 0 
There are 16 visible players.
Highest total since last bootup: 34.

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>
You say 'there are wholistcounter = 0 '

<534/534hp 101/101ma 311/311mv>


My code, to initialize variable is now this.

<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="Wholist"
   match="*[pri sec ter qua qui hex sep oct]*"
   send_to="9"
   sequence="100"
   variable="wholistcounter"
  >
  <send>wholistcounter = 0</send>
  </trigger>
</triggers>


And the trigger to increment is not this

<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="Wholist"
   match="[*] * *"
   send_to="12"
   sequence="100"
  >
  <send>wholistcounter = (wholistcounter or 0) + 1

Send("Say there are @wholistcounter ")
</send>
  </trigger>
</triggers>


I did try Daniels code, but not your fix for the code. When I saw you add fix for my code, I went back and tried to fix what I originally made. There are no error messages though.
Top

Posted by Nick Gammon   Australia  (23,057 posts)  Bio   Forum Administrator
Date Reply #6 on Fri 30 Sep 2011 12:16 PM (UTC)
Message
Your trigger that does:


wholistcounter = 0



... is called every time you see a prompt. So of course the counter never gets higher. Get rid of that one.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


19,579 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.