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

Posted by Winddancer on Mon 13 Feb 2023 10:11 AM — 3 posts, 9,271 views.

#0
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?
Amended on Mon 13 Feb 2023 12:14 PM by Winddancer
Australia Forum Administrator #1
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.
#2
Thanks