Simple subtraction, I'm stumped.

Posted by Nexas on Sat 24 Nov 2007 01:48 AM — 7 posts, 25,852 views.

#0
I play on Achaea and I'm trying to make a fairly simple but tedious alias. I have two triggers that set variables for "Full health" and "Current health" what I need to do now is figure out a way to set a variable for the difference of the two. My scripting is in Lua, but I'm not sure if this would be a Lua or MUSHclient command. Any help would be great, thanks!
Australia Forum Administrator #1
http://www.gammon.com.au/forum/?id=6030 shows examples of comparing trigger stuff in a script.
#2
I looked through that a little bit. But what I need is something like Print (5 - 4) --> 1 except I need 1 to be set as the variable content, and I'm not sure how to do that.
#3
local crh = tonumber(GetVariable("crh"))
local health = tonumber(GetVariable("health"))
local diffh = health - crh

This is what I have so far, I need another line that can either change the diffh variable into a MUSH variable so I can retrieve the number. Or a line that could send the number directly to the game, but not as a print or note.
#4
I think I figured out that I need to serialize the variable? But I'm not totally sure how I do that. I looked at this post http://www.gammon.com.au/forum/?id=4960, which probably is obvious to anyone who knows much about coding, heh. I'm just not sure what all I need to do to serialize this one variable?

Or if there's an easier/more obvious way to do this, I'll take that suggestion too!
Australia Forum Administrator #5
It isn't as complicated as all that. Lua knows that if you try to subtract two things they must be numbers, so you don't need "tonumber". First, look at this:


print (GetVariable ("crh") - GetVariable ("health"))


That prints the difference.

You can also use the "var" module to make a pseudo-table that lets you access MUSHclient variables easily:


require "var"
print (var.crh - var.health)


Quote:

I need another line that can either change the diffh variable into a MUSH variable so I can retrieve the number.


You don't really need that unless you want it for some reason, but this is how you could do it:


require "var"
diffh = var.crh - var.health  -- Lua variable diffh
var.diffh = diffh  -- MUSHclient variable diffh


Quote:

Or a line that could send the number directly to the game, but not as a print or note.


Just use "Send", like this:


require "var"

Send ("say My health has changed by " .. var.crh - var.health)

#6
It works! Thank you so much! I spent a lot of time figuring out the wrong things, hehe.