Use Multiple points from Target Point Array

Time for my second noob question…
I’m trying to make treasures spawn semi-randomly. By that I mean, I want to be able to set… lets say 20 target points on the map, and then have my treasures use 10 of those. I want to be able to set how many spawn, but I want the spawn locations to be random. How can I do this?
(Ignore the Widget and Viewport thing, that’s used for the score keeping in the actual level)

Thanks in advance for anyone who attempts to help with this!

Update: I added some target points in. And thanks to Klawd3 I have been able to figure out how to make it spawn multiple treasures. However, now I need a bit of help figuring out how to make it so that when a target point is used, it won’t use it a second time. (At the moment, I sometimes get 2 treasures that will spawn in the same spot, and I would prefer to have them spawn in their own spots).

You want to spawn 2 objects ?
You need a forloop going from 0 to 1 (two executions), and to avoid using the same spawn point twice, you can copy the array in a temporary variable, and remove the used location from the array at each execution.

Can you give a quick example of what that might look like? I’m looking up for loops on youtube now. But a visual of the blueprint would be amazing for trying to learn this.

Especially the part about copying the array to a temporary variable and removing the one that was used.

Okay… I got a working loop. The tutorial on youtube was actually quite helpful with that. Now I just need to figure out how to make it so it wont use the same target point more than once.

Just to give you an idea.
Create the temp array as an empty duplicate of the original array containing the intended locations (vectors).

I tried using that setup and it isn’t working. I mean, it compiles, but it runs the same way as it did before. I have 10 target spawn locations, and to test if it works I make the loop run 10 times so 1 treasure should spawn on each location. However, when I play, it will sometimes have 2 or 3 or one time there was even 5 on a single spot, so I know it isn’t spawning only 1 per spawn point. I’m going to attach my blueprint, can you tell me if I’m missing something?

The thing attaching to the “set” box is just my even begin thing and the viewport boxes seen in my other screenshot above.

Wow I’m all confused now. What happens to all that stuff I just added in? I’m going to guess that the “true” part of the branch would plug into the “remove” box. I remove the “forloop” box and then… do I get rid of my temp array? Or do I plug the “set” box into the “spawn actor” box directly?

I know, I’m a pain to deal with because I’m so new at this. Please bear with me a little longer. =-b

two things first:

fundamental rule: never use level blueprint unless you absolutely have to.
second: you can store the transforms/vectors inside an array in the blueprint without using the persistent actor references like you did. Like this:

Probably the problem is that the forloop executes all in one tick (it’s not a proper for loop), so it doesn’t really care for the objects we are removing from the array. You could simulate a step-by-step loop with something like this:

you set the index default to 1. and the other variable to the number of actors you want to spawn, after the decrement int node remove the location as you did before

It’s ok, it takes time to figure things out.
Remove the forloop because, as I said, it executes all of its “loops” in one single action, so it doesn’t really care if you remove the items from the temp array, because it uses it only once. Keep the get actor transform part and attach it to the new. And lastly, just after the spawn actor node, put the remove from index you did before. Yes the temp array is still needed.

PS: remember not to use Level Blueprint, just move everything to an actor blueprint. LB is really never needed, and it can cause different problems. When moving things away you’ll need to figure out how to make the array how I showed you since you can’t use the references to persistent level outside LB.
Bye. (and remember to mark the question as answered if we are done :))

I think I screwed up… it only spawns one item now.
I turned it into a vector array (I’m still using the persistent actors, just getting the transform. I don’t have their locations set in the world yet, once I do i’ll set them instead of using targetpoint references, I’m using this to test to make this function work and then I’ll polish it once the basis works).
So, here is what is going on now… Any thoughts on where I screwed up?

It only executes once because when the branch is false it doesn’t go back into the spawn actor node, you forgot a piece :slight_smile:

I connected the branch false to the spawn actor node, and now it’s back to doing what it did with the ForLoop… it spawns 10 treasures but not 1 per spawn point. Some spawn points wont have any and some will have 2 or 3.
Any other ideas?

Sorry, missed something, you have to promote the Get node to a variable. before the spawn node. then use a get on that variable for the remove. It should work like that.

sorry, missed something, you have to promote the Get node to variable. put it before the spawn node. then use a get on that variable for the remove node. It should work then

I’m not sure what you meant by “node” so I right-clicked on the circle in the get box and made that a variable, and then got the variable to plug it in to the remove box. It still does the same thing. I’ve been up all night though watching tutorials and reading and trying your ideas, so I’m gonna pass out for a while. When I get up I’ll transfer everything to an actor blueprint and use n actual transform array with locations.
This is what it currently looks like though, if you see any glaring mistakes: (I drew a red line from the circle i promoted to variable, to the variable get part)

Nope, undo what you just did.
Right click on the pin on the right in the Get node, and select “promote to variable”. It will automatically create a new variable and pop a Set node for it. Plug that set node before the spawn actor (also move the return from the branch to this set node instead). Then from your blueprint variables (big panel on the left) drag the new variable that was just created by the “promote to variable” into the graph and select get, to get a get node (lol) for that variable. Plug this into the remove, and try again.

You know what… it works. The only thing I had to change was make the “NumberToSpawn” variable 12 in order to spawn 10 treasures. Index is 1 so I’m not entirely sure what is going on there… but once I made that variable 12 it worked flawlessly. I’m going to mark this as answered since it now works. If you have any feedback on the variable needing to be 12 in order to spawn 10 nodes I’d love to hear it, but for now, it technically works.

Thank you so much for sticking with me through this. I’ve actually learned quite a bit from all this, even if it was just using your ideas. This is my first UE4 project, and people like you make learning this stuff tolerable. =-b

Update: It isn’t necessarily the number 12… the “NumberToSpawn” variable needs to be set 2 higher than the amount I want to spawn. If I set that variable to 9, I get 7. If I set 5, then 3 spawn. Etc etc. But oh well, I’m not complaining since this actually works now. xD

Show the code please.
Btw, I suggest to never leave things like that if they work but you don’t understand why. Reason is they could give you unexpected problems later on, possibly making a gigantic mess. Think it like this, if you create a building on bad foundations the higher you go the greater the damage will be when it falls.

It should normally only be 1+ the value you set in the variable- That’s because you increment it AFTER spawning (if you do the math it checks out).
The problem with your code, that requires +2, is that after the Set Bronze Spawn you should enter in the Set New Var. It’s like picking the first random transform. Then you pass that transform to the spawn actor, then you remove it from the array, then you check if you reached the desired amount, and if not you go back to the Set New Var, that’s equal to picking another random item from the remaining items in the array, etc.