Pending kill issues

Hello, i’m struggle with a problem which i can’t find out.

What i’m doing:
i’m trying to spawn an actor then add it to an array. Then with game running, array elements will destroy and spawn a new one, somewhere will use array elements.

Issue:
Here is 2 class: Spawner, Gamestate.
Spawner will spawn actor and make an array, this array only using to send array to Gamestate.
Gamestate have an array varrible to store this array for other class to get.

If i set Gamestate array varrible straightly in Spawner, pending kill error will occur.

But if i use set array elements in Spawner, everything will be fine.

This really make me confused. They are doing the same thing, set an array of other class.
Why? Is there any differences?

you mention that you are tying to “add it to an array” what you are showing is a Set which replaces the data of what is currently at the specified location with the data you are passing it.

if you are tying to “Add” the element to the array then call Add.

Are you sure that this Spawned Item has been fully instantiated by the time you are accessing it, in some cases if you try to do tests on an actor within to short a time span after calling SpawnActor() you can physically catch it between Malloc and initialization. sometimes a validity test can return a false positive for “not finished initializing” as “destroyed”.

If you take an Actor right after SpawnActor() then add it to an array by the time the Add() is finished it will be instantiated.

1 Like

I want to add elements to specific index.

I don’t know how to test if them fully instantiated.
if i use set array elements to modify array of Gamestate, everything works well.
When i using a local varrible of Spawner store array, then pass whole array to Gamestate, somehow, few elements in the array of Gamestate become “Unknow”. But array of Spawner is completely normal.

Because of the way TArray has been optimized, the order of the array should not be trusted, nor assumed, unless you sort it directly and intentionally.

if you want to force a specific order you should:

  • create a sort for the array based on something about each element (How to sort an array? - #3 by ClockworkOcean) (in C++ you could define a sort predicate, but that would require defining the object type in C++ to be absolutely relevant)
  • have the “collection of elements” be a struct (though this would require knowing the maximum amount in advance)
  • having each “element” being a different reference on a given object, but this has similar limitations to the previous

even with or without a sort to get a specific element of an array you would still want to search, because if one of these elements gets removed somehow, or re-arranged (sometimes when the array is asked to expand all elements of the array are copied, and sometimes the order of the copies does not preserve the previous order)

we are also talking about an array of pointers which should not be manually manipulated as it could break GC, or cause loss of reference count resulting in early GC, and the Index definitely should not be manually entered per node (you set at index 0 shown in the screen shot). using Set() on integral types (int, float, bool, enum) is fine (assuming the change is rational to begin with)

the order of instantiation of objects of Actors can not be guaranteed nor enforced (without modifying the engine’s core serialization system) the order your Spawners get instantiated, and start functioning might be different from run to run.

how are you “passing the whole array”? how soon after modifying the array are you passing it? try looping through the array to output like their names both before the pass (in the spawner), and after (in the receiver).

there is also the consideration that for some arrays based on optimizations for “data packing”, and expected interactions; the array might actually have more slots then the number of elements in it (for example an array of int32 with 1 element might be allocated with 4 indexes available but only the first one is used), this should be hidden away behind checking the size. we should kind of assume that the order elements are added to the array is the order they will be, but that is not always the case.