smart wolves among the sheep

I have a system that will spawn a number of sheep, and then a number of wolves. The wolves find the sheep closest to their location and path to it.

Problem is that some sheep have several wolves following them. How would I inform the controller the sheep in question is already being followed? I’ve tried casting to the sheep character blueprint and the sheep ai controller, but doing so causes a total engine crash on the first tick. It seems I can’t communicate with any other blueprints inside of controllers.

Any suggestions?

Hi,

its a bit difficult to say without actually seeing the blueprints involved.

As you are already pathing to the sheep you must have a reference to the Sheep Pawn / Character in your wolf Controller. How exactly did your casting fail then?

As soon as we get this to work its something simple as setting a boolean variable on the sheep to true (i.e. lockedByWolf=true) and then searching for the nearest sheep that does not have this variable set to true.

Cheers,

I would define a new variable in the sheep character class, either a boolean like bAlreadyPursued or, if you think you might want info on the wolf pursuing the sheep then a reference to the wolf character currently pursuing it.

When a wolf wants to pursue it first checks if bAlreadyPursued is false or that the pursuer reference is null (depending which method you use), and then chases it and modifies the flag or reference accordingly.

If the game is crashing, it’s probably because you’re casting incorrectly. Check online the correct way to retrieve a character or controller component and cast it to your custom class.

This is my set-up for finding the closest sheep. Sheep distance starts at 500m and is compared to every sheep. For each sheep that is closer than the current Sheep Distance variable value, that sheep is selected for the Sheep To Follow variable and its distance is set to the Sheep Distance variable. This is repeated for every sheep until all have been checked. When the loop is complete, the move order is given.

This is how I tried to check if the sheep is being followed. I tried this the same way with my “sheep_cont” sheep ai controller. This is supposed to check the sheep in question to see if the boolean is set to true. If it IS true, the sheep is ignored. If it is NOT true, the sheep is considered. If the sheep is closest, that particular sheep’s boolean should be set to true to prevent other wolves from selecting it. Attepting to test the game like this causes the engine to crash outright whether casting to sheep_char (a character blueprint) or sheep_cont (an AI controller blueprint).

Could you post your crash logs? I’m not sure whether or not this could be the source of the issue, but you should really avoid casting every tick with a loop, casts take up a lot of resources from my experience and it looks like you are casting so often that it cannot catch up (though this may not be the case). Something you can do is create another array, this array will hold the ‘As Sheep Char’ return value for each of the objects, create the array once with many casts, and then be done casting until you spawn another sheep.

Hi,

the warning when casting to sheep_char is simply that you actually dont need to cast, since the output of Array Element is already a sheep_char (since you did a “get all actors of class sheep_char” the output is already an array of sheep_char’s).

I am now not sure what the crash could be. I would make a breakpoint on the for each loop and step through one time, so you actually know which node really causes the crash. If its the cast then you are good, since you shouldnt need it.

Btw, which engine version are you using right now?

Edit: I’m with Jamendxman3 that you shouldnt do this in the Tick event, but rather in a custom event thats called once, or even in the BeginPlay Event. Though i am not with him regarding casting taking up resources, this actually shouldnt take up any resource, since casting is effectively just a “treat object as”. The reference to the object is already there, its just a question of which variables and methods are exposed to the caller. But this may be a seperate discussion that could last forever :wink:

Cheers,

SUCCESS!

I managed to make a working system.

This system allows every wolf to select a sheep that is not currently being followed and basically claim it as their own, even following the sheep around as it wonders.

Yay it works!~

Currently, these wolves will never change which sheep they’re pursuing but I believe this system will work perfectly for what I have in mind. Thank you indygoof and **** for your suggestions. You helped me think about the problem in a different way.

I’m using 4.8.2.

Just tried retrieving the “is being followed” boolean from the array element and it worked so I have removed the “cast to” with the warning. I remember it not working before. Again, thanks to you. :smiley: