Download

Inserting Blueprint Class objects into Arrays inside Blueprint Classes?

What I want to do is create an array inside a Blueprint class that contains objects of another Blueprint class, then add newly created objects (with whatever initialization parameters) to this array. I’ve looked around but some sources seem to be out of date and I’m encountering some problems.

I have 2 classes: WorldMap and City. I want World Map to manage a list of City objects, which will consist of a name, coordinates, probably other Blueprint objects etc.

When I create an array, am I supposed to select “Reference” or “Class”? I now assume Reference, but I wasn’t sure initially. Either way, I give WorldMap an array of References to the City class, then in the Event Graph after BeginPlay I create an Add node from my array. Then I place Construct Object from Class node, set it to City, drag the return to the Add node and set the Outer input to the WorldMap class (I assume that part makes sense… given the object will be managed by the WorldMap object).

When I hit compile, it gives me an error saying “Wrong class to spawn ‘[path]/City.City_C’ in Construct Object from Class”. What does that mean? The types seem consistent, and there isn’t any problem when I’m setting it up, it just gives an error once I compile… and I can’t see what it could be referring to by “wrong class” when there only seems to be 1 type of class involved.

What is the City’s parent class? You can’t construct an actor like that, only objects.

Yeah, it’s Actor alright.

So I’ve just been using the complete wrong function because it’s an Actor, not an Object? That would explain things.

I did notice there was a different function mentioned in the documentation but I swear I looked several times, turned off Context Sensitive, but couldn’t find it somehow… Spawn Actor from Class, that must be it. Strange I didn’t notice it before…

So how do Objects work? I can’t seem to make a Blueprint for an Object… It says Actors are Objects that can be placed or spawned in the world,. so are Objects just like data structures with no position?

I must have skipped over/forgotten the distinction when I was reading the documentation.

EDIT: Yep, seems to be working now. Thanks for that.

On a similar line… I have 2 quick questions about spawning actors, though I assume I can probably guess the answers:

  1. Unlike C++ for instance, am I right to assume that when you spawn an actor there’s no way of instantiating any of its variables and you simply have to spawn, then individually set its variables?
  2. Right now I’m using Spawn Actor, then putting the return value into an Add function. Let’s say if I didn’t add it to anything and just kind of left it floating there: would it be garbage collected or something? I just can’t see how it could be referenced again by the developer since it has no name and doesn’t seem like it would be stored anywhere with an obvious name, and so it makes me think of Java objects with no reference and its garbage collection…
    Unless I’m missing something important?

Actually, if I run the game that way, a City object is created… I think I need to go research how this works…

When you spawn an actor all it’s variables will be the default values that you set when you created them. There’s also a Get Class Defaults node, which lets you get default values without spawning them. If your objects are never being modified you could probably just use that. If your city objects are data only, you could have an array of classes, and never spawn them.

Generally you would promote the spawned actor to a variable or add it to an array, but you wouldn’t always need to for certain things too. It is still possible to get a reference to it again though, using Get All Actors of Class, Get All Actors with Interface, etc. I’m not sure how garbage collection is handled in Unreal, but it won’t be garbage collected while its still relevant just because you aren’t directly referencing it anywhere.
Though yes, I’d presume there is a difference between how GC is handled for an actor that is explicitly referenced and one that isn’t, in that the referenced one will not be garbage collected while the actor referencing it still exists (But I don’t actually know).

Also, you can’t use Spawn Actor from Class in a construction script, not sure if that was the problem there.

Interesting… that’s useful to know. I’ll probably spawn them since I want to draw an icon on the screen representing their positions. In my earlier non-Unreal C++ attempt, I just had the world map object get their positions and draw them itself, which I guess I could still do, but perhaps spawning would be better here…

I forgot about Promote to Variable, that makes sense.

Also useful…

Thanks for the info, I should be able to move ahead now until something else crops up.