Compile error - player1.%resist
by Nicolai Dutka · in Torque Game Engine · 10/14/2007 (9:51 pm) · 10 replies
This causes a compile error in the console.
$spellDam = $spellDam * (player1.%resist/100);
Why? What could I do to fix that?
Here is a snippet of the rest of the code:
The reason for the %resist variable is that this is a "base attack" type spell that effects everyone alive. I have a fire spell that can do this, ice, lightning, water, and earth. I want to be able to change the amount of damage being dealt to each target based on that target's resistance to the appropriate element.
$spellDam = $spellDam * (player1.%resist/100);
Why? What could I do to fix that?
Here is a snippet of the rest of the code:
if( $leader.faction == 1 )
{
if( player1.isDead == 0 )
{
$spellDam = $spellDam * (player1.%resist/100);
player1.health -= $spellDam;
if( player1.health < 1 ) { kill(player1); }
showDamage($spellDam)
}
if( player2.isDead == 0 )
{
$spellDam = $spellDam * (player2.%resist/100);
player2.health -= $spellDam;
if( player2.health < 1 ) { kill(player2); }
}
if( player3.isDead == 0 )
{
$spellDam = $spellDam * (player3.%resist/100);
player3.health -= $spellDam;
if( player3.health < 1 ) { kill(player3); }
}The reason for the %resist variable is that this is a "base attack" type spell that effects everyone alive. I have a fire spell that can do this, ice, lightning, water, and earth. I want to be able to change the amount of damage being dealt to each target based on that target's resistance to the appropriate element.
#2
I realize I cannot use a variable in that location which is why I am getting the error, but I am trying to think of a good way around it without having to write 100 lines of code per spell.
At this point in the spell, the only thing that is going to change is which resistance to use and how much that resistance is for each player.
10/14/2007 (10:05 pm)
The players have 7 possible resistances. If I am casting a fire spell, it should use each individual player's resistance to fire. Player1 might have 'player1.fire = 90' but player5 might be 'player5.fire = 100'. So at the beginning, right before the snippet I included, %resist = fire; or %resist = ice, etc.I realize I cannot use a variable in that location which is why I am getting the error, but I am trying to think of a good way around it without having to write 100 lines of code per spell.
At this point in the spell, the only thing that is going to change is which resistance to use and how much that resistance is for each player.
#3
Faction 0 = players
Faction 1 = enemies
Faction 2 = NPCS
Faction 3 = specialNPC
Here it is:
That is for casting an attack spell that damages all of your enemies regardless of your faction. I need the $spellDam (spell damage) to be modified based on each individual's resistances. Here are a couple of the individual spells that uses this function:
When the players are created, they get the resistances set to them: %newBot.fire = 100; %newBot.ice = 80; etc
10/14/2007 (10:26 pm)
Here is the full function. $leader is the person casting the spell (could be an enemy OR and ally). There are some sim groups( characters, enemies, que, grave ) which i am using for battle maintenance because this is a turn based RPG. $spellMag is the casting cost of the spell and $spellDam is the amoutn of damage it does. These numbers are determined before getting to this point. Faction 0 = players
Faction 1 = enemies
Faction 2 = NPCS
Faction 3 = specialNPC
Here it is:
function castAttackAllSpell(%resist)
{
$leader.magic -= $spellMag;
if( $leader.faction == 1 )
{
if( player1.isDead == 0 )
{
$spellDam = $spellDam * (player1.%resist/100);
player1.health -= $spellDam;
if( player1.health < 1 ) { kill(player1); }
showDamage($spellDam)
}
if( player2.isDead == 0 )
{
$spellDam = $spellDam * (player2.%resist/100);
player2.health -= $spellDam;
if( player2.health < 1 ) { kill(player2); }
}
if( player3.isDead == 0 )
{
$spellDam = $spellDam * (player3.%resist/100);
player3.health -= $spellDam;
if( player3.health < 1 ) { kill(player3); }
}
if( player4.isDead == 0 )
{
$spellDam = $spellDam * (player4.%resist/100);
player4.health -= $spellDam;
if( player4.health < 1 ) { kill(player4); }
}
if( player5.isDead == 0 )
{
$spellDam = $spellDam * (player5.%resist/100);
player5.health -= $spellDam;
if( player5.health < 1 ) { kill(player5); }
}
}
if( $leader.faction == 0 || $leader.faction == 2 || $leader.faction == 3 )
{
%enemies = enemies.getCount();
%count = 0;
%enemy = enemies.getWord(%count);
while( %enemies > 0 )
{
$spellDam = $spellDam * (%enemy.%resist/100);
%enemy.health -= $spellDam;
showDamage($spellDam);
if( %enemy.health < 1 ) { kill(%enemy); }
%enemies--;
%count++;
%enemy = enemies.getObject(%count);
}
%enemies = que.getCount();
%count = 0;
%enemy = que.getWord(%count);
while( %enemies > 0 )
{
if( %enemy.faction == 1 )
{
$spellDam = $spellDam * (%enemy.%resist/100);
%enemy.health -= $spellDam;
showDamage($spellDam);
if( %enemy.health < 1 ) { kill(%enemy); }
}
%enemies--;
%count++;
%enemy = que.getObject(%count);
}
}
schedule( 1000, 0, battleControls );
}That is for casting an attack spell that damages all of your enemies regardless of your faction. I need the $spellDam (spell damage) to be modified based on each individual's resistances. Here are a couple of the individual spells that uses this function:
function castComet()
{
castAttackAllSpell(fire);
}
function castBlizzard()
{
castAttackAllSpell(ice);
}When the players are created, they get the resistances set to them: %newBot.fire = 100; %newBot.ice = 80; etc
#4
10/14/2007 (10:37 pm)
What do you think about this:if( player1.isDead == 0 )
{
%resist = "player1."@ %resist;[b]//added this line[/b]
$spellDam = $spellDam * (%resist/100);
player1.health -= $spellDam;
if( player1.health < 1 ) { kill(player1); }
showDamage($spellDam)
}
#5
I need to set the target's "%effect" to 5 so that I can apply damage to them over the next 5 turns.
Here is a spell that uses it:
So basically, I need to be able to go:
echo(player2.blaze);
And if player2 had been hit with blaze, it would return a 5.
10/14/2007 (11:02 pm)
Ok, now I have a new problem that is entirely related to this:function castDOTSpell(%resist, %effect)
{
if( $selected.mirror == 0 ) { %target = $selected; } else { %target = $leader; }
if( $casting == 1 )
{
$leader.magic -= $spellMag;
}
%resist = %target @"."@ %resist;
$spellDam = $spellDam * (%resist/100);
%target.health -= $spellDam;
showDamage($spellDam);
if( %target.health < 1 )
{
deSelect();
kill($lastSelected);
}
[b]%target.%effect = 5;[/b]
if( %target == $leader ) {playerStats();} else {targetStats();}
schedule( 1000, 0, battleControls );
}I need to set the target's "%effect" to 5 so that I can apply damage to them over the next 5 turns.
Here is a spell that uses it:
function castHolyFire()
{
castDOTSpell(holy, holyFire);
}So basically, I need to be able to go:
echo(player2.blaze);
And if player2 had been hit with blaze, it would return a 5.
#6
function castScorch()
{
castAttackSpell(fire);
}
function castFireball()
{
castAttackSpell(fire);
}
function castBlaze()
{
castDOTSpell(fire, blaze);
}
function castComet()
{
castAttackAllSpell(fire);
}
10/14/2007 (11:24 pm)
I have like 40 or so of these where the only thing that changes is the resistance of the target being processed. The $spellDam and $spellMag are declared before this point.function castScorch()
{
castAttackSpell(fire);
}
function castFireball()
{
castAttackSpell(fire);
}
function castBlaze()
{
castDOTSpell(fire, blaze);
}
function castComet()
{
castAttackAllSpell(fire);
}
#7
Eval takes a string and evaluates it as an engine function. So if you have a function called doFoo that takes one argument, you could write this in a script to call the function:
So have a look at this:
So for your particular problem, you want to evaluate different fields on a player, say they're %player.ice and %player.fire. "ice" or "fire" is passed into %resist like you're doing. So %resist now contains the string "ice" or "fire". What you need to do is make a string expression that evaluates the player's field.
Then you would call the function this way:
10/15/2007 (1:19 pm)
I think your first problem was due to improper use of a string. I'm assuming you're passing strings into %resist? Basically, you can't do %object."string". What you can do, however, is use eval to make the engine do that.Eval takes a string and evaluates it as an engine function. So if you have a function called doFoo that takes one argument, you could write this in a script to call the function:
doFoo(%someVariable);However, say you also had another function called doBar which also has one argument. You want to use one of these functions, but you don't know which - it will depend on the input which function should be used.
So have a look at this:
if(%input = "foo")
{
doFoo(%parameter);
}
else if(%input = "bar")
{
doBar(%parameter);
}However, this can be done more simply theough eval. If you know your %input variable is going to contain "foo" or "bar", you can simply say:%function = "do" @ %input @ "(";
eval(%function @ %parameter @ ");" );This will do the equivalent of calling 'doFoo' or 'doBar' with the parameter you want. You're essentially putting together a statement from strings, and then making the engine think it's a script statement.So for your particular problem, you want to evaluate different fields on a player, say they're %player.ice and %player.fire. "ice" or "fire" is passed into %resist like you're doing. So %resist now contains the string "ice" or "fire". What you need to do is make a string expression that evaluates the player's field.
if( player1.isDead == 0 )
{
%playerResistFactor = eval("player1." @ %resist @ ";" );
$spellDam = $spellDam * (%playerResistFactor/100);
player1.health -= $spellDam;
if( player1.health < 1 ) { kill(player1); }
showDamage($spellDam)
}So what happes there is that you're eval'ing a string which is "player1." and the %resist string ("ice", "fire", or whatever), and finally a semicolon to be gramatically correct.Then you would call the function this way:
castAttackAllSpell("fire");
#8
10/15/2007 (1:29 pm)
YES!! Golden! TY!! I was never taught "Eval" and really had no idea such a thing existed! This is perfect! Thanks!
#9
10/15/2007 (6:10 pm)
Another little known trick that could be useful in this circumstance: TorqueScript Arrays can use strings as indexes:player1.resists["Fire"] = 50; player1.resists["Ice"] = 10; %resist = "Fire"; %playerResistFactor = player1.resists[%resist]; echo(%playerResistFactor); ==> 50;
#10
10/15/2007 (6:12 pm)
Oh TYVM! That is REALLY good to know too!
Torque 3D Owner Stephen Zepp
You don't use it anywhere else for fields (which is correct), so I'm curious as to why you put it in for just that one field.