I am developing a game mechanic where I have an actor class (lets call it Bob) which reacts to the presence of another class (Mick) by turning around and running away. I have multiple instances of Bob which are spawned into the level and form small crowds around the level.
The running away mechanic is working well but I now want to add the ability for an instance of Bob to tell other instances of Bob that it’s seen Mick and the location of Mick. This information will be passed from one instance of Bob to another instance of Bob depending upon the distance between the two instances.
The effect I am after is that of a crowd reacting to a hostile actor. some will see the hostile actor directly and runaway and others will “learn” of the hostile actor when they are “told” by another member of the crowd.
I have setup a bool variable for flagging that Mick has been seen and a Vector to store the known location of Mick at the time it was seen. What I lack is the understanding of how to pass this information from one instance of Bob to another instance of Bob.
If you’re using the Unreal AI controller subsystem, that supports behavior trees, perceptions, and so on, you’d create a special kind of perception for “target spotted” and have the spotter emit this stimuli so that nearby agent can perceive it.
If you’re rolling your own, then you’ll typically do a “find actors of class in volume” for some sphere centered on the perceiver. I think the easiest way is to call SphereOverlapActors and filter for your Mick actors.
Interesting, never thought about the actor in volume approach. I may use that for something else I have in mind.
The actual issue I am facing is more to do with how to reference the variables between the two instances of the same actor which has be stumped at the moment.
Iterate over the actors returned by the sweep.
Select out yourself (because yourself is an actor!)
Now, use the instance you’re finding as the referent for the variable you want.
Or call a function on it.
So quick update. Thanks to @jwatte I’ve finally implements the crowd behaviour I was looking for. As soon as I was able to “communicate” between the instances of my actor I was able to get them talking and sending location information between them!
Now I have a crowd that will react to the presence of a hostile actor. If one of the crowd sees the hostile actor it will update a variable in other actors giving the destination towards which it will run away. I’ve added line traces in to show where the hostile actor is, where the crown will run towards, and where the crowd is communicating between them.