#Help with Coroutines and Data Structure Design

1 messages · Page 1 of 1 (latest)

eager oxide
#

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!

#

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?

late orbit
eager oxide
#

omg that's such a great point lol

late orbit
#

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

eager oxide
#

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

late orbit
#

maybe step through the debugger and see whats happening, i dont exactly see why Scout wouldnt run

eager oxide
#

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

late orbit
#

Well your script has to be on something yes

eager oxide
#

But i'm sort of at a loss how to programmatically add the pheromone sublcasses to the Ant objects

eager oxide
#

currently, Scout is a standalone script. So I'm guessing coroutines won't work as expected since they're a unity function?

late orbit
#

ive honestly never tried this so i cant say for certain 🤔

eager oxide
#

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.

late orbit
eager oxide
#

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

late orbit
#

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

eager oxide
eager oxide
#

I want the ants to have a reference to their own pheromoneSequence

#

I think it's necessary but idk

#

i'm such a novice

late orbit
#

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

blazing widget
#

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

eager oxide
eager oxide
late orbit
eager oxide
#

correct

late orbit
#

oh I now realize Pheromone also isnt a monobehaviour, got confused myself with the Start thing.
Though still this should run UnityChanThink 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

eager oxide
#

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

eager oxide
#

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.

late orbit
eager oxide
#

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

late orbit
#

if you want something to exist on an object, itll have to be a monobehaviour

eager oxide
#

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??

late orbit
#

you can just set it up in the inspector

eager oxide
#

sorry, i'm conflating monobehaviours and gameobjects. got it.