Download

How to create character references without casting to each one?

For my fighting game, I would like to set up a select screen. The problem is I can’t seem to create references to characters without casting. That in itself wouldn’t be an issue if I could just loop through and cast dynamically to the one currently selected. I’ve tried creating reference variables but they’re all empty unless you cast to them and then pass in the reference variable–like what? Seriously? I have to get a reference to my reference before I can use my reference?. What’s really annoying is I only seem to have to do this to blueprint actors. Everything else can be manually set into a variable reference without casting. So…what gives Unreal?

I’d also like to avoid situations like placing every character into the world just to get a reference to it. There’s gotta be a clean way to do this internally. For instance the “Spawn Actor From Class” node has an embedded list of all classes that it pulls from. Why can’t I do that too?

Thanks!

Hi @SeldinG

its not really clear,
If you have all your character on the “Select level” and so you can see them all ,
from any blueprint you can get-all-actor-of-class and have them in a list to order them or tag them.
if you make them spawn instead of having them already placed
you can save their reference on spawning and then you can call event and stuff on them
If you dont have the 3D but only a widget with images of chars
you cant have a reference to the object that dont exist, you can set a reference to the class of the char. to do things like spawn or check which char is

can you explain a bit more clearly?

Hi,

Object reference is just a pointer to an existing object. Not the object itself. Therefore an object reference without an existing object is meaningless (will be nullptr).

If you just create an object reference variable, then it will be a nullpointer by default. You need to actually get an object reference from the world and set the reference variable with that.
[HR][/HR]

You can only access functionality and variables of an object if you spawn it into the world. If you want to make information globally accessible, then you could use data tables. If you want functionality to be globally accessible, then you could use blueprint function libraries.

Get all actors of class only gets actors in the world. That means if I have 30 characters, I have to place all 30 in some level hidden off screen and then use get all actors of class to pull them in. To me this just seems asinine. Like, if you make a map. The asset exists in the content browser and using the open level function finds that asset and loads it into existence. I just want to do that for characters. To have to load all of them into memory seems silly. There’s got to be a way to select just the one reference, load that asset into memory, and then spawn it into the world.

Add a Spawn Actor from Class node in the BP that spawns characters.
Right-click on or over the Class pin and then click on Promote to Variable.

Make this variable an array (change the variable type in the details pane.)
Compile the Blueprint. Now you can set 1-100 character specific classes.

In the details pane pick / select actors in the Default Value drop down list…
Hit the + icon several times to add more default values for each character.

Note, no actors have to exist or be instanced in the world for this to work.
It just parameterizes Spawn Actor and its far easier than querying assets…:wink:

I’m not trying to access it’s functionality before it exists. I’m trying to make it exist. I have a hard time thinking in bubbles because everything is obfuscated away. I have no idea how these bubbles are communicating. From my perspective, dragging a character from the content browser and placing it so it spawns into the world is magic. Somewhere behind the scenes there’s a check if the mouse is inside the object’s icon, it then gets a reference of that object (or the object itself) from memory, and then spawns an instance of it in the world. Prior to that action it did not exist in the world right?

So what I’m trying to do – click on an icon and spawn the character when the level loads. I can do it by casting to one character object reference at a time but I’d much rather have a list and only load the one needed rather than having to have a level with all of them already spawned in just so I can get references to them to spawn them in. Unreal is the ONLY engine I’ve ever had to do that in.

Granted, I CAN…have the character select as it’s own “level” with all characters dropped in somewhere off screen, get all actors of class, pass ALL of them into an array on the game instance, then get who I want from there and load that one in as the game level loads. But that method seems really asinine to me. Especially since the icon in the content browser is literally the object’s reference in memory but not in the world.

Now that might just work. I’ll get back to ya!

Edit:
This seems to be the answer I was looking for. Thank you!

Thats is set a reference to the class of the char
see the @EntrpriseCustomr answer