- Home /
How do I access a specific component in a generic way?
So I'm mocking up a turned based rpg combat system. My plan is to store each of the members of the combat in an array and then using a sort function of the array based on speed to figure of each member's turn. Since each mob and PC will have unique attacks etc, each will have a unique animation script. The problem I'm running in to is that I can't find a way to reference each member's script in a generic way. I want to be able to call "attack #3" on the animation script and have that character's unique attack fire. I tried adding the name of each Animation script to the base class for each as a string, then tried to look up that name from the getcomponent("string") but I get errors.
//create mobs for testing
robo = new CharacterAttributes(Player.Robo, "Robo", CombatPosition.PlayerFrontCenter, "Combat Robo" , "RoboAnimationController" ,20,21,19,20,18,22,22,57000);
robo.AddWeapon(new Weapon("Ion Gun", 1, 100, false, Element.physical, 100, Player.Robo));
var monster : NPCAttributes = new NPCAttributes("Psy Auto", "Psy Auto", "PsyAutoAnimationController", 8, 200, 20, 30, 1, 1, 5, 1, 15, false, 50);
//populate the battle field
robo.myInstance = Instantiate(robo.model, Vector3(combatPositions[robo.combatPosition].x, (robo.model.collider as BoxCollider).size.y/2,combatPositions[robo.combatPosition].z), Quaternion.Euler(0,0,0));
monster.myInstance = Instantiate(monster.model, Vector3(combatPositions[8].x,(monster.model.collider as BoxCollider).size.y/2,combatPositions[8].z), Quaternion.Euler(0,180,0));
while(true){
//create a loop that loops through party and monsters and returns the highest initiative TBA
//Select next attacker based on return TBA
//need to call a menu here with options for player on party turn populated with options for each character individually TBA
yield robo.myInstance.GetComponent(robo.myAnimationScript).AttackTarget(robo, monster);// Here I get errors that .AttackTarget isn't a component of UnityEngine.Component
if(gameMasterController.inCombat == false) break;
Any ideas. I'm pretty new to OOP, but I'm learning fast.
Cheers
Answer by Chronos-L · Apr 27, 2013 at 01:16 AM
// Here I get errors that .AttackTarget isn't a component of UnityEngine.Component
Look at this, DataCheck.js line 15. There are few ways to write a proper GetComponent() in the same answer, pick the one you want to use.
You can try inheritance to improve the code, this is a C# example, I can't write in uJS anymore:
BasicAttack.cs
public abstract class AbstractAttack : MonoBehaviour {
public abstract void Attack( Enemy e );
}
SwordAttack.cs
public class SwordAttack: AbstractAttack {
public override void Attack( Enemy e ){
...
}
}
MagicAttack.cs
public class MagicAttack: AbstractAttack {
public override void Attack( Enemy e ){
...
}
}
Then, you can just use:
someone.GetComponent<BasicAttack>().Attack( targetedEnemy );
This will call the Attack() in the SwordAttack,MagicAttack, or any classes that extends the AbstractAttack, this depends on what you attach to someone. So you can have 30 attack scripts, but you can access them all using the AbstractAttack.
It will work in uJS, but I can't write in uJS anymore; you will have to figure that out yourself.
Reference link:
http://answers.unity3d.com/questions/438300/returning-variables-from-an-inherited-class.html
Thank you very much. I've been avoiding rewriting my code to embrace inheritance. This will give me the kick to convert over to C# and rework my logic.
Your answer
Follow this Question
Related Questions
Public List of Scripts? 1 Answer
Generate Chart with Data 0 Answers
Fixing a single call of an action (across 2 scripts) 1 Answer
i get errors but i have no idea why 0 Answers
Can I use generic objects as variables then cast them upon construction? 2 Answers