Unity How to add constructible rooms ? [UNITY 2D]

myppburns

Newbie
Apr 17, 2023
15
5
I am planning to create a management game with unity . TBH I don't have any significant prior experience and am fairly new to unity too . I wanted to ask that how can we add constructible rooms to expand the business , which will eventually get unlocked with some money or other requirements.

Any other tips will be appreciated too!
 

GNVE

Active Member
Jul 20, 2018
632
1,107
Not familiar with unity and can hardly program. I do know that Unity has quite a lot of tutorials on YouTube. Don't start with programming a management game (seems complex) but start off with simple things like a bricks clone or the like. Simple mechanics are easier to understand and translate into programming.
I know it adds a little time up front but allows you to prevent gamestopping bugs and wonkyness later on (as in having to scrap the project and start over). In the end it'll save you a lot of time, energy and frustration. Don't run before you can walk and all that.
 
  • Like
Reactions: myppburns

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,110
14,787
I wanted to ask that how can we add constructible rooms to expand the business , which will eventually get unlocked with some money or other requirements.
The easiest way is to hard define how the building will looks, and how many rooms can be added. Then you'll just need an array representing the building in its raw form.

Each one of its index corresponding to a position on the map.
Let's say that you have a building with x floors, each one hosting up to ten rooms (five on top, five on bottom).
The index 0 will correspond to the first room (top left) on the ground level floor. The index 4 will correspond to the top right room. The index 5 will be for the bottom left room, and the index 9 for the bottom right room.
Then you can add as many from as you want. Index 10 is top left room on first floor, index 39 the bottom right room on the third floor, and so on.

And the value at a given index will define what kind of room is at this position, the values themselves being a pointer to an object corresponding to the room.
Therefore you need a "room" class, that will serve as generic base for your rooms. Then you'll create a class (inheriting from "room") for each kind of rooms (spa, bedroom, bar, gym, etc.).

That way, when the player create a new room, you just have to create a new object from the right class, and place a pointer to it on the array representing the building, at the index corresponding to the created room. Everything else will then be automatically handled without the need for you to care about what room is where.

To display the current floor, you just need something like this (pseudo code) :
Code:
for( i = 0 ; i < 10 ; i++ )
{    building[ ( floor*10 ) + i ].display( i );   }
The "display" method being in charge to draw the correct room at the position corresponding to the passed index.


When the player right click somewhere, you just need to catch the click and have something like:
Code:
building[ (floor*10) + mousePosToIndex() ].menu();
The "menu" method being in charge to display the menu corresponding to the room and to handle the choice made by the player and what they imply.
Optionally you can pass the index to the method, in order to draw the menu right on top of the room.

When the player left click somewhere, you just need to catch the click and have something like:
Code:
building[ (floor*10) + mousePosToIndex() ].enter();
The "enter" method handling whatever happen when the player is inside the room.

And so on.


Be noted that in fact the same apply whatever the engine.
It's only the code to write that will change, but if it's what you are searching for, then you aren't ready yet for this kind of game mechanism. In this case, starts by learning what are classes and how objects works, then after you'll be able to write this mostly by yourself.
 
  • Like
Reactions: myppburns

aereton

Member
Mar 9, 2018
348
681
Depends entirely on how you want it to behave. But in principle it would work much like an inventory system on a 2D grid.

Everything you would need for that kind of management/building system can be found in this playlist from YT channel CodeMonkey

Might wanna brush up on your coding experience first if you are relatively new though, so when the time comes you have an easier time wrapping your head around two dimensional arrays and all that funny stuff.
 
  • Like
Reactions: myppburns

Tompte

Member
Dec 22, 2017
214
152
You're not being very specific and it makes it hard to answer the question. If you could point to an example that might help us out.

But from the sound of it, you should maybe look into how to spawn objects into the scene via code, instantiating prefabs and such. That will get you halfway there. The room would just be another object.
 
Last edited:
  • Like
Reactions: myppburns

myppburns

Newbie
Apr 17, 2023
15
5
Depends entirely on how you want it to behave. But in principle it would work much like an inventory system on a 2D grid.

Everything you would need for that kind of management/building system can be found in this playlist from YT channel CodeMonkey

Might wanna brush up on your coding experience first if you are relatively new though, so when the time comes you have an easier time wrapping your head around two dimensional arrays and all that funny stuff.
Hello , the playlist you mentioned helped a lot . Do you know the game Magus Lab . Am creating a similar game too . I hate to bother you but can tell me how can i make my npcs come to my shop for services? What script is needed?
Any other tips will be appreciated too!
 

myppburns

Newbie
Apr 17, 2023
15
5
You're not being very specific and it makes it hard to answer the question. If you could point to an example that might help us out.

But from the sound of it, you should maybe look into how to spawn objects into the scene via code, instantiating prefabs and such. That will get you halfway there. The room would just be another object.
Do you know the game Magus Lab . Am creating a similar game too. Do you know by any chance ,how I can make my npcs come to my shop for services? What script is needed?
Any other tips will be appreciated too!
 

aereton

Member
Mar 9, 2018
348
681
Hello , the playlist you mentioned helped a lot . Do you know the game Magus Lab . Am creating a similar game too . I hate to bother you but can tell me how can i make my npcs come to my shop for services? What script is needed?
Any other tips will be appreciated too!
This is way too general to help you in that regard. How are your scenes composed, what are the behaviours of your NPCs you already implemented? A lot of unknowns here to help you in any meaningful way. If it's just movement of NPC characters in the scene maybe have a look on youtube about exactly that:

As cliche as it sounds, I'd recommend you to learn coding and development in general with easier games first, maybe a Pong clone or something. I know it's not the game you want to make right now but it will really help you down the line. The most important skill of software development is problem solving: analyzing what the problem is, finding solutions for it, etc. The question you asked shows that you lack that which is completely normal for a beginner (not trying to be mean here).

Really, shelve the idea for now for maybe half a year or a year and learn the necessary skills with easier games that have been tried and true for beginners.