When Adding an Actor to a Map

When I add an actor to a map, am I adding the reference of the actor to the map, or am I adding a copy? Or put this another way, after adding the original actor to a map, when I destroy the original actor, would a copy still exist in the map? Or if I don`t do anything to the original actor, but instead delete the actor in the map, would the original actor still exist?

The magic word here is: Instancing!

There is no actor that exists by default, even less so for every actor that exists within your game.

However, there is a class of your actor which always exists! You can use that to spawn an instance of your actor.

All instances will behave independently from one another. If one is deleted / destroyed it’s gone for good. You can spawn as many instances as you want or need but besides offering the same functionality they have nothing in common.

So: Let’s assume you have made an “BP_ExplodingBarrel” actor. You drag & drop it into your level. Now a new instance of “BP_ExplodingBarrel” exists at the location you dropped it to. The location is variable that will be set per instance. If we drag and drop another barrel in, it will be at a different location and both of the barrels know nothing about each other. Not where they are located, not whether they are burning / exploding and if one explodes (and is destroyed) it will simply cease existing while the other barrel will keep doing it’s job as programmed.

Does that explain it?

So, I get it after reading your answer that different actors of the same class are essentially different instances, like a pile of A4 papers where each piece of paper can be considered an instance and A4 be the name for the class.

But let`s say that I have a map variable, and should I add an instance of a class of actors to the map, am I actually adding a copy of the original instance which is in fact a new instance, or am I refering to the original one? Like putting a piece of paper into a bag, am I actually making a new piece of paper exactly the same as the original one or am I simply picking up the original one and putting it into the bag?

What is this “original instance” you speak of?

You create a class, which is basically a template. A blueprint so to speak. Once you add it into your level, unreal creates an instance of this class. To continue that metaphor, it constructs the object or building or whatever according to the blueprint.

PS: I would highly recommend reading up on object oriented programming. You will need that a lot if you intend to program games with any form of complexity.

The “Original Instance” is the one that I first created with a Spawn node, and after its creation I connect the return value of the Spawn node to an Add node, but am I adding the reference of the one that I first created to the map, or between adding and spawning there is a copying of the actor that spawned first which in fact created a copy of the first? So basically what I try to express is that if the first actor that I first spawned in the level and the one that is added to the map refer to the same instance?

And thank you for your suggestion, I`ll surely go study OOP when free.

Oooh! I totally misunderstood you!

To a map as in the map container! Not as in level! Silly me!

That’s another concept, copy by value vs copy by reference. The short version is, for every function call or the like, all the parameters (the colored pins) need to be copied somehow in order for the function to… well… have these variables!

This can happen either by value or reference, which are fairly descriptive names. If it’s copied by value all the ram it occupies is copied over and you have effectively a second version of the same thing. On the other hand if it’s copied by reference only a small variable is shared which points to where the object is.

Now, when exactly copy by value and when copy by reference is used depends on the language and in quite a few languages on you yourself. However, a general rule of thumb is, if it’s not a basic datatype it’s usually copied by reference (aka no new version). Basic datatypes are numbers (float, int, etc), bools, single characters, etc.

While more complex types, such as classes (objects, actors), containers (arrays, maps, sets) are generally copied by reference.

In between there are a few types such as strings, transforms or generally structs for which both methods can make sense so there’s no generally applicable rule here and I’m not entirely sure how UE4 handles them.

To answer your question directly: When you add an actor to a map it just points to the actor. No new version is created.

This is why I was confused by “original instance”.

A really good question to ask by the way! Questioning how these things work under the hood is a brilliant sign you’re starting to really understand how code works under the hood!

Oh, now I see. Thank you very much:)