0
\$\begingroup\$

Hi all :) I'm making a 2D roguelike platformer and I want to make a load of hand crafted rooms and like spelunky place them around. When the player enters a room a script will go off. The script will know what exits the current room has and what entrance was used (Don't need to place anything where the player came from). I then have 2 options for choosing what rooms will work:

I can either have all the prefabs contain a script with a list of all the exits they have, then check that list for if the right one is there. I.E; Entered room from the left. Room has left, right and top exits. I can ignore the left as that's where I came from, I can ignore the bottom as there isn't an exit there but I need to check the arrays on the prefabs to see if they have right and top in them. After that I'd place the top one x units above and the bottom one x units below (I'd also check something wasn't there before placing over it).

The other option is to have 4 lists of prefabs. A list for top exits, bottom exits, left exits and right exits. Then look through those for what I need. Then I would place the blocks where they needed to be from there.

So, as the title says, what of those options would you use for picking and how can I then get the prefab I need and place it into the world. I'll use a "Centre" item for it and then place the centre of the prefab a certain amount to the side of the current rooms centre.

Thanks in advance for any help you can give :)

\$\endgroup\$
  • \$\begingroup\$ Unity has a rogue tutorial that seems to answer the core of this question. \$\endgroup\$ – Gnemlock Jan 13 '17 at 19:13
1
\$\begingroup\$

Having four lists of rooms and searching in the right list will be slightly faster than having one list and determining which rooms are eligible again and again.

But the four lists will have duplicates among each other, so they will take up slightly more memory (By the way: I hope you don't want to place actual copies of the rooms in these lists. References would do).

But both of these will be really, really minor points, unless your number or rooms is somewhere in the thousands. You are very likely optimizing prematurely here. Just do what's easier and fits better into your overall architecture.

Personally I would go for the first solution, simply because I might want to plan ahead for adding more exclusion criteria than just having the right exists. For example not having any exits leading to somewhere where I already have something else, fitting more than one already placed room, avoiding repetitions, avoiding placing rooms which depend on content which is still locked, etc. etc.

\$\endgroup\$
  • \$\begingroup\$ Thanks, I wasn't going to do actual rooms there, just names to draw. I couldn't find anything about getting prefabs from the referances online. Could you give me a link to the documentation (or just the name) of where I could find that. Thanks for the help though :) \$\endgroup\$ – crabcrabcam Jan 13 '17 at 16:13
  • 2
    \$\begingroup\$ @crabcrabcam That's something you should ask as a new question, but the answer would likely be Resources.Load. \$\endgroup\$ – Philipp Jan 13 '17 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.