Memory management question

If a pawn has a cast and creates a reference to some other object, my understanding is that other object then gets loaded into memory. If multiple copies of that pawn are then spawned in, does a copy of the reference object get loaded for each one or do they all reference the same copy?

Thanks

The cast doesn’t make the reference, its the variable that holds the data that is the reference (or the spawned actor itself you could say), and they will reference each individual copy and not all of them. So each copy will have its own reference and be loaded separately into memory. That’s my understanding of it anyway so if another dev would like to correct me go ahead. (edit: if you are creating a reference to some other object through a cast its still the same, each on will be seperate).

If you mean multiple references from different actors to the same object then i wouldn’t be able to say anything with confidence as im not 100% sure.

Thanks for the response.

I think you’re right that the cast loads the object into memory and the variable creates the reference. I may have worded that poorly in the OP. I’m not so worried about the reference. I think that the reference is the equivalent of a C++ reference (maybe the exact same thing), which is 4 bytes in C++. I don’t know how big it is in blueprints, but unless I have millions of those references, I don’t think they are going to be a memory issue. What I’m concerned about is whether each cast from each spawned pawn will load it’s own copy of the cast to object into memory, (as that might quickly become a problem.) I wouldn’t think so, but I’d like I’d to be sure.

Blueprint scripting works exactly the same as C++ (in fact it is C++ shown using a visual wrapper). When you define a class its definition is compiled into the code. When you spawn an instance (create a clone) of that class, space is reserved (loaded) in memory to store all the variables needed for that class. Each time you spawn a new instance (object) new space needs to be reserved. A reference is a single one word variable (similar to an integer or float) that can be set to point to an instance (think of it as a piece of string that you can follow to find the actual instance/object). Casting is just a way to set and check that the reference is actually pointing to an actual instance that has been spawned in memory (ie has been initialised and isn’t pointing to some random space).
So spawning or creating a new instance is the only way to create a new class object, a reference is just a one word pointer to a spawned instance, and casting doesn’t use any memory it’s just a way to set a reference to point to an instance and check that the reference is now correct and valid.

2 Likes

Thanks

Just to confirm, means the cast, even if multiplied over multiple copies of the pawn, they are all pointing to one instance loaded in memory?