How to get a variable from actor in a list of objects?

Hi,
I have an actor with a list of actors as a variable. All the listed actors inherit from the same class with 2 variables (Name, texture). As I understand, I can’t use the “actor” type for the list if I want to edit the “default” list, instead I have to use the type “object”. But once listed as objects I can’t get the actor’s info from them.

The use of objects to set elements in a default list is necessary??

Can anyone help me with this process?

  • Make a variable: list of actors. (All actors from different classes but all inherit from the same parent)
  • Set the different elements of the “default” list in the editor.
  • Access a variable from an actor in the list at runtime.

Thank you!

You can cast to the the actor you want the default variables from. Have a look at this → Casting Quick Start Guide | Unreal Engine Documentation

Get All Actors of Class (the child actor class type)
Get (0) and drag off Get Parent Actor.

Cast that into the Class type of the parent.

You should then be able to drag out the variables stored in the Parent class.

(seems like this is a long way to get there, but no simpler chain comes to mind that would net a valid cast result)

Hi!, thanks for the response ! I forgot to mention that in my question!
I have tried on the objects in the list the “cast to actor” and “cast to ParentClassName” without success. The cast always fail.
Is there any exception involving objects casted to actors?

Thanks!
The problem with this approach is that the actors(as objects in the list) aren’t instantiated. As I understand the node “Get All actors of Class” searches for actors alive in the world, Isn’t it?
My list with actors is a library of objects so I can spawn them at demand.
Maybe I should reference them in some other way?

If you aren’t placing them in the map it won’t work, true.

If that is the case, you may want to consider using a Game Instance to store the data.

It persists throughout play under all events, and can be referenced from any type of blueprint directly by casting to it with a drawn [Get Game Instance].

.

Another advantage to using them is that because it never gets “replaced” during play – you can set a reference to it on BeginPlay in any BPs that will cast to it, and just use that rather than casting each time.

Thanks again! You’re right, I should use a Game Instance to do that. But I still got the same problem. Now the game instance has a list of actors. I want to set the default elements of this list. It seems that the only way to do that is using the object type otherwise I have to fill the list at “beginplay”. It looks like if I have the actors listed as objects I can’t get the data from their variables. The only solution is filling the list at runtime? Is impossible to get the variables data from an actor if is listed as an object and not instantiated?

I think I am missing what it is you’re trying to do exactly.

If you are using actor blueprints for data storage, without placing them into the map, you are going a very long way to do something simple.

If you are storing lists of dissimilar data types such as the various child classes you have for all actor types set from a single parent - you can just use a Structure to store all of it as a single variable.

Something of an example would help clarify.

I’ll try to explain what I’m doing. (excuse my English)

The player has access to a “Lego”-like building system.
I have a parent class named “Brick” with a couple of variables (name and icon) and a couple of functions to handle the attachment and detachment between bricks. Then I have some classes that inherit from Brick (like “BodyBrick” or “BuildingBrick”) that have some other variables and functions for the specific behaviors of this “bricks”.
And then I have different actors of type “BodyBrick” for each different BodyPart pieces that I create. Same with “BuildingBrick”

The classes layout is ActualBrick->TypeofBrick->Brick.

In the construction menu the player can select pieces and spawn them in the world. To organize all the pieces and ease the searches the players can select which type of pieces are shown to them (BodyPart, BuildingPart).

I want to be able to have a list with all the bricks in the game(or a different list for each type of brick). I need to be able to set the default elements of this list in the editor and at runtime I want to get the name and icon variables of the parent class “Brick” to fill the different buttons of the selection menu with the appropiate names and textures.
From this list I also want to get the actor to spawn when the player makes the selection in the menu.

I will try what you said and use a structure of “Bricks” instead of a list in the Game Instance.
Do you think this is the proper way to handle this system?
Thanks again!

I think I understand better what you are trying to do.

A structure is just an array that can store different Types of data. A blueprint, a float, a text variable…

That is why I suggested it - since you are asking about references to Actors/Pawns and various variables associated to them.

You can make an

. In the child blueprint you can set the default variables of the parent. If you don’t see these in the child make sure Show inherited variables is checked →

121478-untitled2.png

.

At the end I used a list of classes in MyGameInstance and a function to return the appropiate class. The function inputs an integer and outputs a class type. The menu bluprint then can “spawn actor of class”.

The problem now, unrelated or not, is that the project crash when loading and I can’t continue. The crash points to the Menu blueprint so, perhaps it is related. I’m on 4.14.3.

Thanks for the help!

Edit: Here is my crash and a workaround to resolve it:
https://answers.unrealengine.com/questions/528455/child-actor-component-keeps-corrupting-its-parent.html