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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Script firing, will not add to Variable

Script firing, will not add to Variable

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


Posted by Jakaiya   (4 posts)  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?
Top

Posted by Ked   Russia  (524 posts)  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)
Top

Posted by Nick Gammon   Australia  (23,133 posts)  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
Top

Posted by Jakaiya   (4 posts)  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.
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.


16,360 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.