Referencing instances of scripts based on the GameObject they are attached to.
Hi. I am fairly new to Unity and I am trying to code the State Machine for a turn-based RPG battle system. I'm trying to make it so I can use it in basically every battle throughout the game, so I'm keeping it as generic as possible. The StartBattle() function is called by a OnTriggerEnter() function when the player touches the monster. The battle system code is as follows:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BattleSystem : MonoBehaviour {
List<GameObject> heroes = new List<GameObject>();
List<GameObject> enemies = new List<GameObject>();
public enum turns{hero, middle1, enemy, middle2};
private turns currentTurn;
public int i = 0;
public Dictionary<string, System.Action> battleDictionary = new Dictionary<string, System.Action>(){
{"Player", PlayerFight.Fight}
, {"Imp", ImpFight.Fight}
, {"Rat", RatFight.Fight}
};
// Use this for initialization
void Start () {
currentTurn = turns.hero;
}
// Update is called once per frame
void Update () {
}
public void StartBattle(List<GameObject> friends, List<GameObject> monsters){
foreach (GameObject friend in friends) {
heroes.Add(friend);
}
foreach (GameObject enemy in monsters) {
enemies.Add(enemy);
}
heroes.Add (null);
enemies.Add (null);
RealBattle ();
}
public void RealBattle(){
//=========================Heroes turn==============================
if (currentTurn == turns.hero) {
if (heroes [i] != null) {
//call fight function for every hero using the dictionary (the function has to end with BattleSystem.i++; BattleSystem.RealBattle();)
battleDictionary[heroes[i].tag]();
} else {
i = 0;
currentTurn = turns.middle1;
RealBattle ();
}
}
//=========================Middle1 turn==========================================
else if (currentTurn == turns.middle1) {
//do atks, calculate damage, check if all monsters are dead. If not:
currentTurn = turns.enemy;
RealBattle();
}
//=========================Enemies turn==========================================
else if(currentTurn == turns.enemy){
if (enemies [i] != null) {
//call fight function for every enemy using the dictionary (the function has to end with BattleSystem.i++; BattleSystem.RealBattle();)
battleDictionary[enemies[i].tag]();
} else {
i = 0;
currentTurn = turns.middle2;
RealBattle ();
}
}
//=========================Middle2 turn==========================================
else if (currentTurn == turns.middle2) {
//do atks, calculate damage, check if all heroes are dead. If not:
currentTurn = turns.hero;
//RealBattle();
}
}
}
This code works, but poorly. That is, the Fight() functions need to be static, because I can only reference the class and not the specific instance of the script attached to that GameObject (e.g. I'm not able to reference the ImpFight attached to Imp1, only the whole ImpFight class). That, of course, is really problematic because I can't have more than one monster that uses a script from that class on the same battle, as the commands would apply to both. What I'm looking for is a way to reference the instance of that script specifically but with the code still being generic so I can use it throughout the whole game. Thanks and sorry for broken english.
Your answer
Follow this Question
Related Questions
RPG instantiate problem 3 Answers
RPG Class Selection Menu 2 Answers
Instatiated object not being referenced in Start function? 2 Answers
Getting Int value from script to other script 2 Answers
Hi everyone.need help with this. 1 Answer