How to spawn items at random location

every single tutorial i watch is about how to spawn random cubes in a big as*s area where it doesn’t even matter where it spawns but in my case i want spawn items inside a small drawer or a cupboard or a box and also each item should be spawned in an appropriate location,
for example, i don’t wanna spawn a car battery inside a drawer or a screwdriver inside cupboard, bcz thats not gonna fit.
also, all of my items inherit from one class called itemparent and that’s where I’ve done the code, i was wondering maybe i can use the parent class to spawn the child actors?

also, the drawers and cupboards are in a blueprint as well, i was wondering maybe i can put a scene component inside the blueprint and get the world transform of that actor to spawn my items instead of going through the instances one by one.

When you spawn, you get to say the location. That’s all you need.

Naturally, the objects can’t spawn themselves, so it would make sense for a drawer ( for example ) to spawn the screwdriver inside itself.

1 Like

how do i get the location of each instances tho? there’s like 6 drawers in my game which are just instances of a single class, just getting the location isnt gonna work cause 2 items might spawn in a signle drawer, so i need to know which drawer has already spawned something.

i need to add its not just a single item there’s 4 item that should be spawned inside 6 drawers, so 2 drawers should always be empty and the rest should constain different item everytime the game is restarted.

this is what I’ve come up with so far, but there’s a problem cuz the item is being spawned twice in different locations.

Probably you can add sockets to your drawer mesh inside UE editor. Each socket could play a role of a spawn coordinates. Then just get random socket from current instance of a drawer and attach your item.

Additionally you should save info about what sockets are already taken to prevent spawning multiple items in same location.

im not really worried about where it spawns inside the drawer cuz the mesh is simulating physics so it will adjust itself to fit. my problem here is that the random integer in range is returning 3 random numbers instead of one.
is that normal cuz i thought that function is supposed to return something random between max and min.

Theres probably multiple of it, calling it multiple times.

in the screenshot above that i have sent, what am i doing wrong that its spawning it three times is it becasue there’s three instances in the world?

one more thing i need to know is how to save which instance has already spawned an item so that it doesn’t spawn it twice.

Make an array and save the spawned item in. You can use add unique. Before spawn just ask in an IF if already spawned.
When you make SpawnActor you have a return value, save that in the array

there you go guys i made a spaghetti for u :stuck_out_tongue:

you can already tell im not good with programming :d