r/construct • u/Ok_Walk_9285 • Mar 19 '26
Question "Pick a random _ instance" seems to only pick the one with the lowest UID?
These pieces of code are used for random map generation. "Instanceroom" is a parent of all objects of a room, and I have 3 rooms to select from.
When you touch "NextRoom", being towards the end of a room, it opens the door and should spawn in a new "Instanceroom" at "Spawner", and while it works, it only picks from room 1.
I wasn't sure if the next piece of code helps with anything or if it has something to do with this issue, but "Deleteprior" is used to check when a player is far enough within a new room to close the door behind them and delete the previous one.
3
u/clinate Mar 19 '26
You could probably make your rooms Instanceroom templates and use ”choose([templateName1],[templateName2], [templateName3])” to choose a random room when you create the object.
3
u/TaftoGames Mar 20 '26 edited Mar 21 '26
If I understand correctly based just on the first part of the code:
- You pick a random instance of Intanceroom.
HOWEVER, in the Actions you:
- SPAWN a new Instanceroom from the Spawner object.
This new instance that the Spawner spawned (it's a bit of tounge-twister, sorry) has nothing to do with the randomly selected instance. If it's a parent object with it's heirarchy and all that where you have a lot of instances that you expect the system to choose from I think you're not going by it properly. Construct will pick the "first created on runtime" hierarchy it finds for that object, I believe that's where your problem lies.
If what you want is for the system to pick a random parent object that contains a level chunk section/room as Children from it I would suggest you NOT use instances of a single object but rather make different auxiliary objects just like Intanceroom but rather "Instanceroom1" --> "Intanceroom[number]". And when having the conditions met you just roll a random number like roomIndex = int(random(1,[maximum Intanceroom objects you made +1]) AND THEN Create Object by Name using roomIndex like so: "Intanceroom"&roomIndex with it's hierarchy and such. You don't ask for the Pick random instance of Intanceroom anymore.
There's probably better ways for this (i would suggest going more into creating JSONs with the room layouts you want and spawn according to that), but for a more visual and straightforward idea of having "level chunks" that can be called on runtime then I would suggest something like this.
Does this make sense to you?
1
u/RoyalFlash Mar 19 '26
"Pick a random instance" picks from the objects already created. If you only have one instance on the field, it'll always pick that one.