[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Alias needs to be called twice, to call script and set correct values?

Alias needs to be called twice, to call script and set correct values?

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


Posted by Winddancer   (26 posts)  [Biography] bio
Date Mon 13 Feb 2023 10:11 AM (UTC)

Amended on Mon 13 Feb 2023 12:14 PM (UTC) by Winddancer

Message
In my mud, you get rewarded for scalping and handing in humanoid mobs. Depending on their difficulty, these scalps are worth different amounts of currency.
As there are different denominations, to the base of 16, calculating the total sum isn't that easy. Gold coins, silver coins, bronze coins and copper coins are used.

I did set up triggers that add the amount of copper coins a scalp is worth to a global variable, bounty_amount.
What I now wish to do, is get the amount in gold, silver, bronze and coppers based on the value of that global variable.

The lua script looks as follows:

function CoppersToCoinage(x)
	local i=0
	SetVariable("gold_coins",0)
	SetVariable("silver_coins",0)
	SetVariable("bronze_coins",0)
	SetVariable("copper_coins",0)
	SetVariable("rest_for_sc",0)
	SetVariable("rest_for_bc",0)
	Note("x =" .. x .. " i=" .. i)
	i = x % 4096
	SetVariable("rest_for_sc",i)
	i = (x - i) / 4096
	SetVariable("gold_coins",i)
	i = GetVariable("rest_for_sc") % 256
	SetVariable("rest_for_bc",i)
	i = (GetVariable("rest_for_sc") - i ) / 256
	SetVariable("silver_coins",i)
	i = GetVariable("rest_for_bc") % 16
	SetVariable("copper_coins", i)
	i = (GetVariable("rest_for_bc") -i ) / 16
	SetVariable("bronze_coins",i)
end -- CoppersToCoinage


When I change the global value and call the alias that converts the alias, I get the correct value for the global variable bounty_amount, but the values for the different coin types reflect the value of the time when I last called the alias.

E.g.
I set the global variable to 4097 and call the alias twice, just to overwrite everything that might have been stored in the variables and I get the following output:

x =4097 i=0
Expected bounty       - 1 gc 0 sc 0 bc 1 cc

When I now change the value for x to 8375 and call the alias _once_, I get this output:

x =8375 i=0
Expected bounty       - 1 gc 0 sc 0 bc 1 cc

Calling the alias a second time, the output changes to:

x =8375 i=0
Expected bounty       - 2 gc 0 sc 11 bc 7 cc


The alias I use to call looks like this:

  <alias
   match="MyBounty"
   enabled="y"
   expand_variables="y"
   group="statistics"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
<send>CoppersToCoinage(@bounty_amount)
ColourNote ("#0AFF0A", "#1E1E1E", "Expected bounty       - " .. "@gold_coins" .. " gc " .. "@silver_coins" .. " sc " .. "@bronze_coins" .. " bc "
.. "@copper_coins" .. " cc")</send>
  </alias>


What do I need to change in order to have to call the alias only once to show the correct values?
[Go to top] top

Posted by Nick Gammon   Australia  (22,928 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Mon 13 Feb 2023 11:00 AM (UTC)
Message
Variables used in an alias like you have (@copper_coins) are evaluated once before the start of execution of the script. Calling CoppersToCoinage inside the alias is too late.

You need to use GetVariable instead, rather than inlining the variables like you have.

ie. Instead of: "@gold_coins", use GetVariable ("gold_coins") and so on for the other ones.

- Nick Gammon

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

Posted by Winddancer   (26 posts)  [Biography] bio
Date Reply #2 on Mon 13 Feb 2023 12:27 PM (UTC)
Message
Thanks
[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.


2,195 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 HostDash]