Casting from AI controller to Spawner

Hello guys,

So, I am using a spawner actor to populate my scene with rabbits.
I want the rabbits to randomly walk around within a territory defined by the Spawner actor.
To do so, in this Spawner, I implemented a TerritoryRadius Variable.
And BP_Rabbit is driven with an AI controller (BluePrint).

My idea is to to utilize casting to use the spawner location and it’s TerritoryRadius value in the AI controller, to limit the Rabbit movements.
But the casting is not working, (Cast Failed is working though)
I use the CastToSpawner node with an ActorObjectReference variable pinned at the Object Input.

Does this sound wrong to you ?
What could be the cause of this not working ?

Thank’s for your time!

Hey @GhostInTheCommu!

It sounds pretty doable, you say the casting is not working- Can we get visuals?
Post your code! :slight_smile:

Get that for us and I think we can figure out why that cast isn’t working- and don’t ONLY show the cast, the most important thing is context! :slight_smile:

Hey @Mind-Brain ,
I can’t hide the truth anymore, I lied, it’s not about rabbits :stuck_out_tongue:

So here are some screens of my mess:
The casting is in the AI controller.
The Spawner is spawning a BluePrint driven by the AI controller.

The “New Var” in the AI controller is set to the spawner’s parent object class.

As you probably guessed, I’m pretty new to BluePrints.
I’m not sure I understand fully the concept of Casting …

I hope I gathered enough informations, tell me if you need more infos :slight_smile:

When you spawn the actor, maybe plug in a self reference from the spawner into the owner input on the spawn node, from there you could try to get owner and access that move random function from there. Or better yet set a variable on the rabbit for a reference to an object of the spawner class, that way it has direct access to the spawner that spawned it

1 Like

Hey @doctorpepperdan,

-----“When you spawn the actor, maybe plug in a self reference from the spawner into the owner input on the spawn node, from there you could try to get owner and access that move random function from there.”-----

I don’t really get the idea. Why would I get ref to itself ?
What is that move random function ?
Are you saying that I could use a function from the AI ctrl into the Spawner to drive my
character ?

Is that what you are talking about ?

-----“Or better yet set a variable on the rabbit for a reference to an object of the spawner class, that way it has direct access to the spawner that spawned it”-----

Is this what you suggested ?
like so, the return values I get from the ActorLocation and the TerritoryRadius are 0.
Is it because I’m trying to get values from an object ref and not a spawned actor ?


If I can get a direct access to an actor with an object ref, what is the point of Casting ? What is the difference ?

I probably sound very confused ! Well I am :grimacing:

I may have misread your BP, I thought the randomroam function you had setup was in the spawner. Really any AI behavior is best setup in the controller under your behavior tree, in a behavior tree task. As far as the self reference, I’m talking about when you call the spawn actor node, get a reference to self(for the spawner) and plug it into the owner input on the actor spawn node, then in your actor BP call get owner may be able to access the location of the spawner then. Or place a vector variable on the bunny rabbit class, make it instance editable and expose on spawn, then plug in your spawners location when you call that spawn actor node

Casting essentially tries to treat an object as if it’s another like say you had 2 enemy character classes each with different attacks, you could try to cast one to the other to allow one character to perform an attack they usually wouldn’t, in my experience witch isn’t a ton yet, it’s better to just get a reference to exactly what you need, casting often fails and in this case probably wouldn’t work for getting the spawners location

1 Like

I think I made it :smiley: [actually it’s not working]

So I was trying to experiment with your recommendations I could not fully understand and it led me to the Get Actor of Class node :


With my Spawner as the Actor Class, I was able to access its variables I needed in my AI controller !

But I am very confused now as the Get Actor Of Class is doing exactly what I was expecting from Casting :thinking:
While searching on forums, its seems a lot of beginners like myself are struggling to find a way to communicate variables from one BP to another. I’ve red about blue print interfaces, casting, game instances and so on…
How is this working that easily ??

Actually, it’s only working with one spawner.
If I add other spawner, every “rabbits” random location will directly lead to the first spawner’s territory…
Actor of class, it makes sense.

Hey, let me just pipe back up here for a second, @doctorpepperdan was saying everything I had planned to say when I read your reply post :smiley: until we got to “Get Actor of Class”.

Get Actor of Class I believe only returns the first of an actor of that class in the world outliner. No good when you need a SPECIFIC one.

Also, you do NOT have to use a behavior tree/blackboard. Especially if they’re simple pawns, all a blackboard/behavior tree is is a different UI to do the same things, when it gets complicated they’re incredibly more useful, but for things like this they’d probably be overkill.

What they were saying, @GhostInTheCommu, is you put the “GET OWNER” node on the creature. If you plug in “Self” on the spawner into the spawned actor, that registers the spawner as “Owner” to the creature.
So, if you do that, “Get Owner” gets the Owner, which would be the spawner. But there’s a better way.

On your creature, make a variable “Spawner Ref” of your spawner class.(1) Then make that “Instance editable” and “Expose on Spawn”.(2)

Now, when you go to spawn the creature, there will be that variable, exposed on spawn, see: (3)
Pass in “Self” which here would mean “ME, I AM THE SPAWNER” and auto-sets that value on the spawned creature.

Get actor of class would work if there is only one spawner, this is why I recommend setting a location variable on your character, and setting it to the spawners location

That is correct, now in your AI controller, you can get that reference from the controlled actor, and access it’s location

This look very promising, I’m starting to get it.
Importunely I will be away a couples of days but ill come back here with some results and probably some more questions!
Thank you for your help so far :slightly_smiling_face:

1 Like

Okay so I’m back and I got to play a little bit with what you told me !

So, the SpawnActor node, through the “Spawner Ref” variable, labels the spawned creatures as coming from itself.
Like a farmer would use an ear tag to label his cows as coming from his herd right ?

And as you said, I can also set other references such as “Spawner Location Ref”, “Spawner TerritoryRadius Ref”.
So now I have everything I need within my creature pawn. But how do I access those variables in the AI controller ?

I thought I could use the Controlled Pawn node but I cant find anything in the return value.

Hey, that’s awesome! Great analogy with the ear tag, but not only does it state who owns it but where it would belong (spawner territoryRadius ref).

So to get those values you would need to use the Get Controlled Pawn node as you were, and then cast to “BP_Animal”. This is why inheritance is useful! Because if you have all of those things you did as part of “BP_Animal” (instead of on every animal individually), you could cast to animal and know that all of those things are going to be retrievable.

This would make the AI controller reusable on every child of “BP_Animal” :slight_smile:

Is that how I’m supposed to use casting ? I think everything is starting to make sense to me!
The Controlled Pawn had to go through class identification first, then if identified correctly, I can talk to it and ask for what I need.


Is that right ?
I see that I can also cast to the class, what is the difference ?
Since I need to set the object, isn’t casting by default to class ?

I will definitely take a look at child parent inheritance next!
I wanted to make it work with a simple actor class first but I will need to use parent class as I want my Spawner to spawn different classes of animals.

You talked about behaviour trees earlier. I first started working with behaviour tree but I went back to BP as it seems easier for creating many different “simple” behaviours as I’m working with many different animal classes.
And as a matter of learning, I need to develop some basic knowledge with BP before playing with fancy tools :face_with_monocle:

Yup, looks great!

You would cast to class when what you are passing in is a class reference and you need a more specific version of that class. It’s used a good deal less often, overall.

Inheritance is going to be very, very important for you moving forward as you will want all of your animals to have certain things. Think of it like Darwin’s evolution model, with Family, Class, Genus, etc.

Blueprint will do you just fine for a long while. Think about all of the actions an enemy in something like Rainbow Six could take or do… That would be a great use of a behavior tree. For this, simple movement and event-based attacks/abilities will let you go really far. :slight_smile:

Thank you for the good advices, now my basic spawning system is set up, I can play with my characters AI !
You guys helped me a great deal with better understanding the basics of blueprint and casting !

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.