storing a variable name inside another variable

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

storing a variable name inside another variable

Post by Caled »

I tried to find an answer to this through google, but I perhaps don't understand the terminology well enough to be searching for the correct phrase. Or maybe the answers were just beyond me. Anyway.

a = "tom"
b = a
echo( b )

I would like to be able to store a variable name inside another variable, and then control whether "tom" is displayed, or "a" is displayed as a string denoting the name of the variable.

How deep can such recursion go, am I using "recursion" correctly, and how do I control how many levels down it goes?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: variable recursion in lua

Post by Heiko »

Caled wrote:I would like to be able to store a variable name inside another variable, and then control whether "tom" is displayed, or "a" is displayed as a string denoting the name of the variable.
Use a Lua table as a dictionary and use the variable names "a" or "b" as keys.

Example:
Make an alias "attack choser"
pattern: ^attack (.*)

Code: Select all

myAttacks = {}
myAttacks["a"] = "knife attack"
myAttacks["b"] = "spell 98"
myAttacks["c"] = "spell lightning 3"
myAttacks["d"] = "sword attack"

currentAttack = matches[2]
echo( "my current attack is " .. myAttacks[ currentAttack ] .. "\n" )
Now you can chose your attack with:

INPUT red, OUTPUT blue:
attack b
my current attack is spell 98
attack c
my current attack is spell lightning 3
attack a
my current attack is knife attack

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: storing a variable name inside another variable

Post by Caled »

In the example you gave, you actually came quite close to what I am particularly interested in. The class I play use left and right arm balances for many attacks, allowing me to chain two together into a combo. There are a lot of combinations, too many in fact to create an alias for every combination. I have gotten around this in CMUD by listing each separate arm attack in a table, with the shorthand version (what would otherwise be the alias pattern) as the key, and the full command for the attack as the value.

Code: Select all

target = "ramiel"
attackTable = {}
attackTable ["cla"] = "claw left arm of target"
attackTable ["cra"] = "claw right arm of target"
attackTable ["cll"] = "claw left leg of target"
attackTable ["crl"] = "claw right leg of target"
attackTable ["sla"] = "slash left arm of target"
attackTable ["sra"] = "slash right arm of target"
attackTable ["sll"] = "slash left leg of target"
attackTable ["srl"] = "slash right leg of target"
attackTable ["hll"] = "hamstring target left"
attackTable ["hrl"] = "hamstring target right"
attackTable ["rla"] = "rend target left"
attackTable ["rra"] = "rend target right"
etc
That is just listing 3 groups of attacks (rend/hamstring go together, as rend is for arms and hamstring for legs). There are quite a few more groups, and all can be done in combination with each other. The order is important too: sometimes I wish to rend left arm, claw right leg. Other times I like to claw right leg first, then rend the left arm.

The problem is, that 'target' needs to be stored in the table as well, because its position in the ability syntax is not always at the end, so I cannot just append the target after retrieving the attack syntax from the table.

Code: Select all

Alias pattern: ^att$
Script:
echo ( "Target is: " .. target .. "\n" )
echo( "Attack is: " .. attackTable.cla )
Running the above alias returns the following:

Target is: ramiel
Attack is: claw left arm of target

Is it possible for me to make it return "claw left arm of ramiel" instead?

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: storing a variable name inside another variable

Post by Caled »

I think have actually worked out another way to work my attack combo script, without using a table like that (an alternative not really possible in cmud). I'm still interested to know if that kind of thing would be possible though.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: storing a variable name inside another variable

Post by Heiko »

I'll give you a better response tonight when I have some time as this topic is quite involved and interesting for many users in the future.

The simplest solution would be to use the tables above and then substitute all occurences of the substring "target" by the value of the variable target before issueing the command.

Code: Select all

echo ( "Target is: " .. target .. "\n" )
attack = attackTable.cla
attack = string.gsub( attack, "target", target )
echo( "Attack is: " .. attack )
In my above example you'd get:

Code: Select all

myAttacks = {}
myAttacks["a"] = "knife attack"
myAttacks["b"] = "spell 98 target"
myAttacks["c"] = "spell target lightning 3"
myAttacks["d"] = "target sword target attack target kill"

target = "rob"
echo ( "Target is: " .. target .. "\n" )
attack = myAttacks[ matches[2] ];
echo("attack="..attack.."\n")
attack = string.gsub( attack, "target", target ) ;
echo( "Attack is: " .. attack .. "\n");
output:
attack d
Target is: rob
attack=target sword target attack target kill
Attack is: rob sword rob attack rob kill
attack a
Target is: rob
attack=knife attack
Attack is: knife attack
attack c
Target is: rob
attack=spell target lightning 3
Attack is: spell rob lightning 3
attack d
Target is: rob
attack=target sword target attack target kill
Attack is: rob sword rob attack rob kill

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: storing a variable name inside another variable

Post by Heiko »

For your combinations I would make one table with your arm attacks and one table with your leg attacks.
Then I would randomly chose combinations of both tables or make a little combat manager that inspects your ennemy and issues the "right" combinations.

Code: Select all

myArmAttacks = { "box target", "hit target", "wrestle target" .... }
numberOfArmAttackMoves = 25;
myLegAttacks = { "kick target", .... }
(...)
numberOfLegAttackMoves = 50;
-- chose random combination of arm and leg attack:
legMove = myLegAttacks[math.random( 0, numberOfLegAttackMoves )]
armMove = myArmAttacks[math.random(0, numberOfArmAttackMoves )]
legMove = string.gsub( .... ) -- replace target with target name
armMove = string.gsub( ... ) 
randomAttackCombination = legMove .. armMove
send( randomAttackCombination)

Post Reply