[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]  Adding/editing a new array key/value from a trigger
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?

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] Refresh page

Posted by Batista   (15 posts)  [Biography] bio
Date Wed 23 Nov 2005 11:11 PM (UTC)

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?
[Go to top] top

Posted by Nick Gammon   Australia  (21,609 posts)  [Biography] 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

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:


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:

   match="You have slain *"

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

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

Note ("Now killed ", slain ["%1"], " %1")

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