Hi! I'm making a little game that involves ants obeying pheromones to model their behaviors. There is an Ant game object with the Ant script attached (is it bad to name your script the same as your object??), which has a pheromoneSequence attribute. The ant listens to each pheromone in turn, performing that pheromones actions until the pheromone tells the ant to stop. I modeled the pheromones as a superclass Pheromone with subclasses for specific pheromones (currently, Scout is the only subclass). I want the ants to obey a Start() coroutine associated with the specific pheromone they're supposed to follow, but I'm having trouble. For some reason, the coroutines aren't getting called correctly. I'm sure it has something to do with attaching scripts to game objects correctly, but I'm beyond my own capabilities at this point. Googling and videos have only gotten me so far (like informing me that my scripts need to somehow be attached to my game objects). Open to advice on any front, but especially on how to get my ants to activate the appropriate coroutines at the appropriate times!
#Help with Coroutines and Data Structure Design
1 messages · Page 1 of 1 (latest)
Here is my code so far: https://gdl.space/qamogaqice.cs
I am interested in opinions/information on these things: designing the Pheromone objects well; appropriately linking my Pheromone subclasses eg Scout to my ants so the coroutines will run; anything else
I have lots of experience coding but only very little in Unity or game design. I'm still a little confused on how game objects communicate with scripts and vice versa.
I'm a tad bit married to this approach, since I want to have a single Ant class, all of which can potentially obey user-specified pheromones or pheromone sequences. So I think I really do need to have the behavior coroutines located outside the Ant script. I can attach the specific pheromones/sequences to the Ant object, but I'm not really sure how?
I would probably rename that IEnumerator Start() function, because Start is a unity message that happens at some point before Update
https://docs.unity3d.com/Manual/ExecutionOrder.html
Although I dont exactly see why your coroutine wouldnt be called, is there any errors? If any errors happen in awake as well, your script will be disabled
omg that's such a great point lol
I also wouldnt exactly use a coroutine for every single ant, because its kinda unneccesary. I think the approach is fine, but I would have a list inside Scout where ants can add themselves. Then Scout can modify every single ant by looping through
oh interesting
no, i'm not seeing any errors otherwise
they're obeying coroutines stored in the Ant script, just not the ones in the Scout script
importantly, the debug in the Scout's coroutine isn't being printed
maybe step through the debugger and see whats happening, i dont exactly see why Scout wouldnt run
do you have a moment? I want to try attaching the Scout pheromone to the Ant gameobject, but I'm not sure the best approach or even how to do it really.
I have a hunch coroutines in scripts need to be formally associated with game objects
Well your script has to be on something yes
But i'm sort of at a loss how to programmatically add the pheromone sublcasses to the Ant objects
well then this is the problem
currently, Scout is a standalone script. So I'm guessing coroutines won't work as expected since they're a unity function?
ive honestly never tried this so i cant say for certain 🤔
So an Ant should have a pheromoneSequence, and I will gladly programmatically add the appropriate scripts to the Ant game object, but that's a tad beyond me.
I assumed your pheromones would probably exist in the environment, then when ants go near one they add subscribe themselves to that behaviour. How you do this really depends on what the game mechanics are
Oh, I understand. No, the ants sort of "inherit" the pheromones on their creation
And it defines their behavior. Once they hear back from the current pheromone that it is finished, they move on to the next one.
Basically you can imagine it like this: the Ant should conceptually have an Activate() coroutine, but the code of that coroutine changes depending on what pheromone the ant is following
it probably wouldnt be neccessary to have all the pheromones on the ants if the pheromone itself doesnt really need a transform, which is what im seeing from the script
eventually, the user should be able to "update" the pheromone sequence
hm, but the Scout pheromone does need the ant's own transform
I want the ants to have a reference to their own pheromoneSequence
I think it's necessary but idk
i'm such a novice
yes but it doesnt need a transform itself, you might not need this to be a monobehaviour at all tbh. Ants can just have some coroutine that runs saying when they want to calculate their next move, reference the current phermone and call functionality on it.
Rather than having the pheromone itself use a coroutine
FWIW, you can declare IEnumerators on any script, but you have to start executing them from a monobehaviour, becaus monobehaviours are where the StartCoroutine function is
oh good point. Move the coroutine up to the Ant level and use the pheromone to check conditions. I'll think about this.
This is my current problem. I have coroutines in a non-monobehaviour object, but I am calling them from a mono class (Ant)
they are calling StartCoroutine on the ant script itself though
correct
oh I now realize Pheromone also isnt a monobehaviour, got confused myself with the Start thing.
Though still this should run
unless i am missing something obvious
I would really just check if the currentPheromone is what its supposed to be by start, because i dont know where you call AssignColony or AssignCaste, it might be running too late and something is null
or a different value from whats expected
ok I'll check that too, but I'm pretty sure just redesigning things so that a small number of "universal" coroutines are at the Ant level and the Ant just asks the Pheromone for certain signals is the correct move.
thanks, sibling
lol currentPheromone is None
whoops
Ok I have a new question, because I think I'm not handling C# objects vs MonoBehaviour objects very competently: what does new Caste(0.01f, pheromoneSequence); do vs Instantiate()
My experience with programming is in using Python as a sophisticated calculator/simulator. I am not well-trained in OOD.
new is to make a new instance of a class, and provide it values through the constructor, similar to __init__ in python. Unity allows you to new a gameobject and it will spawn a gameobject in the world with a transform only.
Instantiate is what you must use with monobehaviours if you want it to spawn like from a prefab. You can also addComponent to add monobehaviours to your gameobject
ok, yes, I'm much more familiar with using object constructors. My novice-ness comes from creating new objects that are monobehaviours. Like, the Caste attribute of the Colony wasn't even showing up in Unity until I let it inherit from Monobehaviour
if you want something to exist on an object, itll have to be a monobehaviour
that helps!
in other words, if I want to attach a Caste object to the Colony object, they'll both need to be GameObjects inheriting from Monobehaviour, and I'll have to use Instantiate??
you just need 1 game object, you can attach both your monobehaviours to it
you can just set it up in the inspector
sorry, i'm conflating monobehaviours and gameobjects. got it.