#Arrays of Subclasses

1 messages · Page 1 of 1 (latest)

dusk kindle
#

Hello, I am wanting to model ants following pheromones, which represent specific behavior patterns, which the ant will switch between in a user-defined sequence. Here is the relevant portion of my Ant class:

public class Ant : MonoBehaviour
{
    public Colony colony;
    public Caste caste;

    public int id;


    public Vector3 direction;

    public AntState antState;
    public PheromoneState pheromoneState;
    private Pheromone[] pheromoneSequence;
    private Pheromone currentPheromone;

    ...

    private IEnumerator Controller()
    {
      ...
      Debug.Log("Ant" + id + " changes pheromones and begins again: " + currentPheromone.name);
      ...
    }
#
public class Pheromone
{
    public PheromoneName name;
    
    public Pheromone()
    {
        name = PheromoneName.Pheromone;
    }
    public virtual Vector3 GetDestination(Ant ant)
    {
        Debug.Log("WARNING: Default Pheromone behavior GetDestination() called, which  does nothing!");
        return ant.transform.position;
    }

    public virtual float GetDelay(Ant ant)
    {
        return 1f;
    }
    
    public virtual void UpdateStates(Ant ant, Collision collision)
    {

    }
}

public class Scout : Pheromone
{
    public string target;
    public bool redirect = false;

    private new PheromoneName name;

    public Scout(string target)
    {
        name = PheromoneName.Scout;

        this.target = target;
        Debug.Log("Scout pheromone created!");
    }

    public override Vector3 GetDestination(Ant ant)
    {
      // unique Scouting behavior defined here
    }
}
#

So the problem is I've got a Pheromone[] pheromoneSequence that an ant will iterate over, but when I Debug to get the currentPheromone.name, I get Pheromone not Scout. I assume it's because the array is of type Pheromone. But then why does calling currentPheromone.GetDestination() work as intended??

chrome wren
dusk kindle
#

Pheromones don't have a transform

#

Not a gameobject

chrome wren
#

why did u chose name as a variable

dusk kindle
chrome wren
#

i imagine that could interfere.. b/c unity uses name in various places

#

i was always told not to use name as a variable

dusk kindle
#

hm

#

I'll try that

chrome wren
#

dont hold me to that..

#

but i do know my ide likes to tell me name isnt a good variable

dusk kindle
#

god i'm a fool i think it's a public/private thing

chrome wren
#

quiet possible.. idk for sure.. just spitballing

dusk kindle
#

got it working. thanks for rubber ducking with me 🙂