Stuck with arrays

Hi all,

After several failed attempts, I have to admit I need help !

  • I have an object BP I can spawn via a key (spawn action and key set in the level BP)

  • My player can move and scale it in game (via my character BP)

  • 10 objects max of that kind can be spawned

My unreachable aim :

  • I want to store each object spawned in an array (obj01, obj02,…obj10) and keep track of their location and scale (in order to save and load these variables on and from disk later)

So : how and where do I have to set an array ? I really don’t get it even after watching and reading stuff on the subject.

Thanks in advance !

Fausty

Edit : forgot a kind of start

UE4, at least in blueprints doesn’t really have a multidimensional array. What you need to use is a struct. In there, put the object reference, location, and whatever else you need to track. Then when you spawn the object have it store the information in the struct (make variable type of that struct) and have it update it during instances like its location change. In your character BP (or wherever you want to store the array) create a variable of the struct type you made then set it as array. Ensure back in the object, you have it add itself to the array in your character BP.

One thing I have noticed with doing this is if/when you update an individual struct, it is no longer the same as the one you stored in the array meaning you can’t find it or get it by comparison. Ensure that before you update your struct in the object, like location for instance, you find and remove the object from the array before updating the objects struct, then update the struct and put it back into the array.

Hopefully I didn’t confuse you too much with this. If I did, let me know and I’ll try to clarify.

If this helps, please accept the answer by clicking the check mark located under the arrows next to the answer. Thanks!

Thanks for you reply.
Ok, so I have to make a struct (transform) variable from the spawn point first (in the level BP) then store it in the struct array (placed in my Pawn BP).
Then I have to update it when I rotate (for instance) my BP object (through a rotate function within this BP object).
Is this correct ?
If so, how can I add the struct variable (Level BP) to the struct array (Pawn BP) ? I can’t find any reference of it…
Here is what I did (with a “make array” I don’t know what to do with if I have to use the one in my Pawn BP !)

Thanks,
Fausty

I’m not sure if I properly understand what you want to achieve. If you want to spawn single actor whenever you press G button and then add it to the array then you almost made it (level_bp.jpg from 1st post). You won’t need that make array function.

So the final setup (of spawning and adding actors) should look like this:

I also added there ‘protection’ that won’t allow you to add more than 10 objects to the array (because you said you wanted to have such thing).

As a transform you simply have to use whatever transform you want.

‘Actor’ class (in SpawnActor Actor node) has to be replaced with any class you want to spawn.

‘Actors’ is an array of objects that match to whatever class you choose above.

Print String NOPE will be executed when you’ll try to add 11th, 12th… (and so on) element to the array.

Once you’ll want to save Transforms of those Actors, you’ll have to iterate over them and get their Transforms at that very moment when you want to save them.

Yes, that is what I tried to achieve.
So this is it (just placed the Branch before the spawn to prevent spawning more)

2 more things : I just don’t get “to iterate over them” (language problem I guess) and : in which case do we have to use the struct ?
Many thanks,

Fausty

First off, I would highly suggest not using your level BP for anything you need references too, like what you are attempting to do here. As of 4.8.3 you can not get a reference to your Level BP anywhere else so getting any object or variable you store in there will be impossible. It is worth noting that in 4.9 they are implementing a way to get access to your “streaming level” so it will be slightly easier.

Reading your question further, you may not need a struct since you only want the location & scale which both can come from the Transform of the actor and is stored per instance. If you wanted to store more information than that per object, a struct would be what you want.

Like the other guy showed:

Just make sure that where I have “Make Transform” you have the item spawn wherever it is you want it to spawn. This spawns it at (0, 0, 0) every time which may not be acceptable.

To iterate over the array do this:

Obviously you do not want to iterate over an array on button press so insert the code where you need to do this at. The branch at the beginning ensures there is something in the array because you don’t want to run a loop unless there’s data to process. Then loop through it, get each elements Transform then “Split Pin” (right click on transform pin and select “split struct pin”) of the Transform so you have access to the location/scale which is what you say you need. Then pull off those pins and do what you need - in my example I simply print to screen. If you have more code you need to run after the looping, drag off the “Completed” execution pin and continue on with your code.

Hope this clarifies! If you need to learn more about structs, let me know.

You can loop over the array to handle each of its elements. You can use this to get or set properties for each of your actors or to call any other function/event that takes your Blueprint as input parameter.

For your problem, you can use such a loop both when saving (GetActorTransform for each actor) and loading (SetActorTransform).

Yes ! I’ve never been so close! Will try all of your advices tonight, will let you know.
Huge thanks for the support !
Fausty

Hello,

Many thanks to all of you, I could make it works like a charm !

Fausty