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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Adding/editing a new array key/value from a trigger

Adding/editing a new array key/value from a trigger

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


Posted by Batista   (15 posts)  Bio
Date Wed 23 Nov 2005 11:11 PM (UTC)
Message
Greetings,

I've been messing around with arrays for a little bit and wanted to create a little script that would hold the value of slain things. However, when I try to do it I get one of those "You're trying to add a number to a nil value, you dummy!" messages.

More or less, I'm trying for something like this:

Trigger: You have slain thingeh.

A script will look for an entry called "thingeh" in the array called "slain". If it is there already, the script will add 1 to it. If it isn't, the script will add a new entry to the array called "thingeh" and set the value to the number 0 (not a string).

I've been at this for hours. Help?
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 24 Nov 2005 05:11 AM (UTC)

Amended on Thu 24 Nov 2005 05:12 AM (UTC) by Nick Gammon

Message
This is easy enough. The trigger below will do that. I haven't addressed saving the table between sessions. There is a forum post about that:


http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4960


This discusses how you would save the table when MUSHclient saves its world, and reload it when you start it up again.

The trigger is:


<triggers>
  <trigger
   enabled="y"
   match="You have slain *"
   send_to="12"
   sequence="100"
  >
  <send>

if not slain then
  slain = {}  -- create table if necessary
end -- if

if slain ["%1"] then
  slain ["%1"] = slain ["%1"] + 1
else
  slain ["%1"] = 1  -- first one of these
end -- if

Note ("Now killed ", slain ["%1"], " %1")
</send>
  </trigger>
</triggers>



It creates the table if it doesn't exist. Then it looks for the item by key, and if it exists, adds 1 to it (ie. the count). Otherwise it sets it to 1, as it is the first time it is slain.



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


11,154 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.