Setting values in components best practice
Nubby question. So let's say I have an HP component, a Shield component, and a Damage component for a unique character "Bob".
What would be the best way to configure that character's specific values for each of the components?
A single configuration component like "BobConfig" that has access to and changes the values of each of them? That seems like that defeats the purpose of the modularity of component based architecture when there is a component that accesses all the other components.
For example with the HP and Shield components:
using UnityEngine;
using System.Collections;
public class HP : MonoBehaviour {
public float hp = 100f;
void Start () {
}
void Update () {
//Functions for HP
}
}
public class Shield : MonoBehaviour {
public float shield = 100f;
void Start () {
}
void Update () {
//Functions for Shield
}
}
Then we have the BobConfig that alters the values in those components:
using UnityEngine;
using System.Collections;
public class BobConfig : MonoBehaviour {
private HP _HP;
private Shield _Shield
void Awake () {
_HP = GetComponent<HP>();
_Shield = GetComponent<Shield>();
}
void Start () {
_HP.hp = 150f;
_Shield.shield = 200f;
}
}
Answer by meb111 · Oct 24, 2016 at 07:29 AM
I think the general way most people deal with this type of thing would have a class or if you want SLIGHTLY more advanced you could use inheritance. Basic example would be
using UnityEngine;
using System.Collections;
public class Character
{
//here we have our stats and other goodies could add more
public int health;
public int shield;
public int damage;
//this is our constructor so when we what to make a Character we can set the stats at that point
public Character(int health, int shield, int damage)
{
health = health;
shield = shield;
damage = damage;
}
//this is a simple function that will more explicitly modify our health/shield stat based on
//a hit we may take. if we want since health and shield are both public variables we can
//modify them directly
public void Damage(int damageToCharacter)
{
if(shield>0)
{shield-=damageToCharacter;}
else
{health-=damageToCharacter;}
//you can get much smarter here where if you have 5 shield and 10
//health and take a 10 hit shot you would make shield 0 and hp 5 etc.
}
}
then you would have your 'Bob' prefab contain a script that instantiates this class as itself. For example using UnityEngine; using System.Collections.Generic;
public class CharacterController : MonoBehavior
{
Character Bob;
void Start()
{
//this is where we create a character we call it Bob and set it's
//starting values. and so long as the gameObject exhist in the game
//this exact version of Bob will exist and you can change values
//call Character specific functions on Bob etc.
Bob = new Character(50, 100, 15);
}
void Update()
{
//add control and hit checking here
//if we were hit we would call Bob.Damage(15) (15 or whatever damage value you want)
//that would automatically do the function in our Character class called Damage
//we can also change values directly such as if we pick up a health pack
//Bob.health += 50;
}
}
To further that you can look into Inheritence.
Thanks for the reply!
I'm looking for more of a component based answer ins$$anonymous$$d of inheritance based.
So "HP", "Shield", and "Damage" would be their own classes that handle their individual responsibilities.
Or do you think inheritance would be the best option in this situation?
From what I've been reading, it seems like everyone is saying components are typically the best answer for most situations.
I'll edit the topic to be more clear.
Your answer
