How to spawn random item at random location?

guys i wanna spawn couple of items at multiple diffrent locations. it should only spawn it once but the location and the item should be random

this is what I’ve come up with so far but it’s giving me errors at run time saying access none blablabla… does anyone know what’s wrong or a better way of doing this?

Try the same, but instead of Construction Script, get all actors on Begin Play. Get All Actors needs a created world, which doesn’t exist yet when the Construction Script is running.

1 Like

still gives me a run time error

also the code doesnt seem to be working correctly either cuz it sometimes spawns only one item and sometimes spawns it 3 times, but I want each item to be spawned just once. and 2 items shouldn’t be spawned in the same positions

that resolved the error thanks but there’s one more problem, it’s spawning the same item twice and sometimes it spawns 2 items in the same position, can u help me on that pls? thanks

im not sure if i misunderstood u but this is what I did it and somehow its still behaving the same? is it because its not looping maybe I should use a for loop, idk.

the execution pin to the get all actors of class is actually beginplay(i couldn’t fit it in screenshot). do u mean i should put it it a sequence?

Oh, right;

Instead of Length, use Last Index or Length - 1. Because in your case Length is 2, but array elements are 0 and 1;

First, on begin play, save the created arrays to variables once.

Second, save random ints to variables before using them in every loop of your spawning process.

Last, after spawning one item in one place, remove the item and the place from their respective saved arrays, using the saved ints as indices.

That’s why I said you had to save arrays only once. In your case, you re-save then every time you want to spawn something, that’s why nothing changes. Save them on Begin Play ONCE, but spawn your actors by some custom function.

And you don’t have to make a literal loop, I just meant your spawn function, since you call it several times.

Variant 1: Spawn random actor at random location. If TimesToExecute is less than the length of your ActorArray, every Actor will only be used once. Otherwise multiple actors (here 50:50 which actor) at random locations.

Variant 2: Spawn each actor once at random location, actors are sorted, executes for length of Array.

Variant 3: Spawn at each location a random actor, locations are sorted, executes 2 times (only 2 actors) in this example but could execute 8 times because of 8 SpawnLocations (length of SpawnLocations).

i tried you first one and its still spawning the same item twice. why?

after some debugging, it looks like the function is being called based on how many spawn positions there are in the world, even tho i haven’t set up such thing, i want to spawn based on how many items i have not the number of spawn positions…

You could try variant 2 which will spawn every actor only once. Variant 1 spawns for all locations if your TimesToExecute is larger than the length of your Array. If it is not it will also just spawn every actor once. TimesToExecute gives you only the possibility to spawn like 5 actors when you actually have like 20 actors.

Create a blueprint of type actor, place the nodes in its EventGraph, place the blueprint in your level.

Which blueprint is supposed to run this? When i run it inside the cabinet blueprint itself each instance is calling their own beginplay thats why it’s being called 3 times (cuz there’s 3 instance )

spawn couple of items at
multiple diffrent locations. it should
only spawn it once but the location
and the item should be random

We have 10 cabinets that will contain random items of various kind.

  • each cabinet can only have 1 item?
  • there can be only 1 item of each kind ever?
  • must there be at least 1 item of each kind?
  • must every cabinet have an item?

Could you clarify the above?

yes each cabinet or drawer can only contain 1 item.

yes once an item is spanwed it should be removed from the array so that it doesn’t get spawned again.

yes all items should be spawned at least once.

no I put 2 more cabinets than the number of item I have. so if there’s 4 items to be spawned inside drawers, I put 6 drawers in my level and 2 of them must be empty(randomly).

i did what u said now its only spawning 1 item… uhhhhhhh I’m so frustrated with this can take a look at this code and figure whats going on please, cuz when I print out the class name that was spawned its spawning only 1 item so it looks like its not looping.

Either you add 1 to your LastIndex or you save your LastIndex as an Integer and plug this into the Loop. Because when you RemoveIndex after the SpawnActor the LastIndex changes to 0 I think.