Casting to a character

I have a character in my Game who is not my main player character which means I can not cast it using “GetplayerController” etc. How can I successfully cast this character if it is not present in the scene and not my main player character?

If it’s not in the scene you can’t do anything with it :slight_smile:

If you have one in the scene ( at runtime ), you can use ‘get actor of class’.

PS: You don’t need to cast if you use ‘get actor’

1 Like

GetAllActorsofClass iterates through all actors in the level and returns all actors of specified class in an array, really inefficient way of doing things and it also suggests you aren’t properly managing your references anyway.

I suggest taking really long and hard look at object oriented programming and understanding the concepts, because if you don’t understand the structure of the data you are dealing with, you are going to have some major problems in the long run.

You can either store the reference of your character in your controller, i.e. even if the character isn’t yet spawned you can write a small code to have that character get the player controller and store its own reference on begin play.

btw casting a character with controller will always fail, you can use get player character/Pawn instead.

1 Like

Get actor of class is ok for a one off, once you have the reference, you’re done.

And, yes, you could make the code that spawns the player, put a copy of the reference in the game instance and yadayadad…

But this person just wants to get going, they don’t need subtlety yet :slight_smile:

Thank you, everyone. What I did was that I put an instance of the character in a level and put it somewhere away(Not to get captured in the Camera) and get a reference to it by “GetActorofClass”.

@ClockworkOcean What does last line mean? :persevere:

1 Like

If you’ve put them in the level, then you have a reference, you don’t really need getactor of class.

The last line just means, it’s not very helpful for us to come storming in with the kind of solution you might use after working with the engine for 6-12 months, when you’re starting out ( it sounds like you are…, excuse me if I have that wrong ).

I would create an event in a the GameMode or GameState(depending on what you are doing) to spawn an actor and pass the reference to that character to the current Controller so that your controller can handle the possession and destruction of the new and old character.

If the character is already in the world, you can probably get him with all actors of class and by some preset id or get the first one if there is only one.

1 Like

@ClockworkOcean While using blueprint you simply drage the actor from level to blueprint and you get the reference while how would one perform the same thing in C++? And I told that the actor is not in the level so then?
About the last line, that’s fine no worries. You’re helpful.

1 Like

@xintoc Okay, That’s great, I’ll try that too…

If the player is in the level, the reference can be a ‘instance editable’ variable, which means you can set it in the level with the eye dropper

image

image

1 Like