Nevermind, found the way to do it…I always spend hours searching the forum for the solution, don’t find it, post a message, and then give the search one more try and immediately find my answer:
This is bad programming practice, you should not keep an array of different types of objects if you need to do run-time checking on them. Store them in seperate structures instead.
This seemed the most efficient method given the actual usage.
Here’s what I’m doing, let me know if you think I should still try to do it differently. I am definately a coding novice.
I have 8 different arrays(arrayCombatSession00 - 07). Each represents a potential “combat session” between 1-4 attackers and 1-4 defenders. There may be up to 8 different sessions between different attackers/defenders.
each arrayCombatSession contains 20 fields
0 - # of attackers
1 - # of defenders
2 - primary attackerGO
3 - primary defenderGO
4/5 - 10/11 : attacker1GO/attacker1Script - attacker4GO/attacker4Script
12/13 - 18/19 : as above but defenders
attackers and defenders may come and go/die during each combat round.
Just an example. You can extend it for your own purposes.
class CombatSession{
public var primaryAttacker : AttackerScript;
public var primaryDefender: DefenderScript;
private var attackers : ArrayList;
private var defenders : ArrayList;
public function CombatSession(){
attackers = new ArrayList();
defenders = new ArrayList();
}
/**
* returns number of Attackers
*/
public function AttackerCount(): int{
return attackers.Count;
}
/**
* Add an Attacker to this Combat. isPrimary designates the
*/
public function AddAttacker(attacker : AttackerScript, isPrimary : boolean){
attackers.Add(attacker);
if(isPrimary)
primaryAttacker = attacker;
}
/**
* Remove an Attacker
*/
public function RemoveAttacker(attacker : AttackerScript){
if(primaryAttacker == attacker){
primaryAttacker = null;
}
attackers.Remove(attacker);
}
public function GetAttackerScript(index : int): AttackerScript{
if(attackers.Count - 1 < index || index < 0 ){
Debug.LogError("Requested Attacker that does not exist");
return;
}
return attackers[index];
}
public function GetAttackerObject(index : int): GameObject{
if(attackers.Count - 1 < index || index < 0 ){
Debug.LogError("Requested Attacker that does not exist");
return;
}
return attackers[index].gameObject;
}
// As Above for defenders.
}
/**
EXAMPLE USE OF CombatSession
*/
var combatSession01 : CombatSession;
combatSession01.AddAttacker(someAttacker, false);
combatSession01.AddAttacker(otherAttacker, true);
Debug.Log(combatSession01.primaryAttacker.name); // "otherAttacker"
Debug.Log(combatSession01.DefenderCount()); // "0"
Debug.Log(combatSession01.GetAttackerScript(0).name); // "someAttacker"