[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]  Script firing, will not add to Variable
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Script firing, will not add to Variable

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


Posted by Jakaiya   (4 posts)  [Biography] bio
Date Sat 29 Mar 2008 09:12 AM (UTC)
Message
I am trying to set a trigger that adds to a variable but I cannot get it to add

Trigger line
^\[Level Gain\] You gain a level\.$

Script
NewLevel = @NewLevel + 1
Note ("[SYSTEM]: New Level! You are now Level @NewLevel")


Variable
NewLevel

It fires but does not add 1 to the NewLevel variable.
It keeps displaying the old level.

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

Posted by Ked   Russia  (524 posts)  [Biography] bio
Date Reply #1 on Sat 29 Mar 2008 02:21 PM (UTC)
Message
You are confusing two different types of variables: script and Mushclient. You are assigning the value of a Mushclient variable plus 1 to a script variable and then displaying the Mushclient variable, which never changes. Here's what you actually want to do:


SetVariable("NewLevel", @NewLevel + 1)
[Go to top] top

Posted by Nick Gammon   Australia  (21,674 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sat 29 Mar 2008 08:22 PM (UTC)

Amended on Sat 29 Mar 2008 08:23 PM (UTC) by Nick Gammon

Message
Ked is correct, but his reply won't totally solve your problem. To understand why, you need to realize that MUSHclient substitutes variables in the "Send" box at the start, before sending to the MUD, or the script engine.

Say, for example, you are level 20. Thus your script now reads (before it executes):


NewLevel = 20 + 1
Note ("[SYSTEM]: New Level! You are now Level 20")


You can see this will always display the current level.

Also, as Ked says you have altered a Lua variable, not a MUSHclient variable. The answer is to use scripted access to variables, in both places:


SetVariable ("NewLevel", GetVariable("NewLevel") + 1)
Note ("[SYSTEM]: New Level! You are now Level " .. GetVariable("NewLevel"))


Now the second line retrieves the incremented level number.

You can make it neater by using the "var" module which comes with MUSHclient, like this:


require "var"

var.NewLevel = var.NewLevel + 1
Note ("[SYSTEM]: New Level! You are now Level " .. var.NewLevel)


The "var.<something>" syntax gives you access to MUSHclient variables, by using a special Lua table with a metatable on it. This is described here:

http://www.gammon.com.au/forum/?id=4904


On a new world the above script will fail initially because var.NewLevel will be nil (that is, the variable won't exist).

You can fix this by doing this:


require "var"

var.NewLevel = (var.NewLevel or 0) + 1
Note ("[SYSTEM]: New Level! You are now Level " .. var.NewLevel)


The "or 0" part says that if the variable doesn't exist, default it to zero. (Maybe make that 1 if that is your starting level).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Jakaiya   (4 posts)  [Biography] bio
Date Reply #3 on Sun 30 Mar 2008 04:11 AM (UTC)
Message
Thanks that worked perfectly. I will learn Lua and Mushclient if it kills me.
[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.


4,589 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 FutureQuest]