Placing props on an randmly generated map

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Lothrinn

Hello everyone !

I’m making a rogue-like dungeon crawler and I would like to place various props randomly in the different rooms. I made a place_props() function for that and here’s what it looks like so far :

func place_props():
randomize()
var x = randi() % Props.size()
var y = randi() % Props.size()
var w = min_props + randi() % (max_props - min_props)
var h = min_props + randi() % (max_props - min_props)
for room in $Rooms.get_children():
	location.x = rand_range(room.position.x, x)
	location.y = rand_range(room.position.y, y)
	var props_placed = Props[x].instance()
	props_placed.position = location
	add_child(props_placed)

However when I generate the map, the props are generated everywhere, not only in the rooms. And some of them that should only be sticking to the walls (like the bookshelves I made for example) are not. Any idea on what I should do ?

Here’s an image of what happens :

enter image description here

:bust_in_silhouette: Reply From: jtarallo

I couldn’t understand the operations in your function honestly. If I were you, I’d add some logic to your rooms when you generate them to store prop spawn points in them doing some calculations based on prop size and room size. There you’d determine which walls should have props sticked to them like your bookshelves, and which ones wouldn’t fit for example because they are open (for example have an entrance to another room, etc).

You’ll want really good control of where you’ll spawn the different types of props, if they have collisions, etc. You’d want the rooms to feel natural, and not just random squares with a bunch of stuff thrown in there.

To be honest with you, the randomized approach will never yield very good believable results. Games like enter the gungeon have very well thought out prefabs to generate their dungeons. Here is a good read for you to take into account what goes on behind the scenes in that game:

Whay I recommend is that you do something basic along those lines. Have rooms premade with different sizes or room parts which you can put together programatically so you have good control of the balance of objects/obstacles in your scene, and playability + visuals don’t suffer for bad positioning or spamming of props.

First of all, thank you for your answer !

I see, my rooms are generated using what I learned from KidsCandCode’s tutorial on procedural dungeon generation but he never went as far as placing props randomly so I tried to do it myself by applying the same principle he used for the rooms. I actually suspected that the rooms in Enter the Gungeon were generated using prefabs because I know that’s how The Binding of Isaac’s rooms are generated but I wasn’t sure (and had no idea how to do it). Now that I think about it, it actually makes sense and seems like it will save me a lot of trouble !
Thank you for the link, I’ll read it carefully :slight_smile:

One more question though (sorry, I’m still a big noob) : Do you think creating the rooms in separate scenes with the props already placed by hand and using Prim’s algorithm to connect them would be a good way to do it ?

Lothrinn | 2020-06-09 02:45

Yeah, I think that’d be a simple way which would yield good results. The other thing you can do is make sized sections of rooms which you could then combine to form different variations. For example think of a big room of lets say 16x16 tiles. You could create 12x2 walls, 2x2 corners and 12x12 center which could maybe be comprised of 4 6x6 sections (which could in turn be used to make smaller rooms using the same or different borders and corners).

It’s up to you how much time you want to put into this, but making a good amount of different sizes to combine and play with is the best way to go. It’s a good time investment to yield a nice result, and the more time you put into it, the better your dungeons will feel.

jtarallo | 2020-06-09 03:04

I see, that’s really enlightening, thank you !

This project is a part of a 1 month challenge I gave to myself (since the One Game a Month jam doesn’t exist anymore I’m kinda making personal 1 month jams because I’m not comfortable enough to do shorter ones yet) so I only have until July 8th to complete it but I love those types of games and level design is my favorite part of game design so I might work a bit longer on that :slight_smile:

Lothrinn | 2020-06-09 03:13

That’s good, having a personal challenge and not getting too carried away with the time spent is the best way to start and learn properly. I’d advice you stick with the month timeframe or don’t overextend for more than one extra month before you move on to the next challenge (unless you feel like you can still learn stuff from it). Good luck with it! Hope you have fun and learn as well!

jtarallo | 2020-06-09 03:20

Thanks, will do ! :slight_smile:

Lothrinn | 2020-06-09 03:27

Hello, I know it has been a long time, but I came crossed the same problem of trying to connect prefabs (rooms) to make a random dungeon map. Have you successfully achieved the result you want? Could you share some tips of how to do it? I also went through the same 3 part tutorials and still haven’t find a way to connect prefabs. It would be really helpful if you could share some experience, thank you!

Idleman | 2022-02-26 12:59