Reference constructor inside of itself?

Hi! Is this thing possible?

public class Room
{
    public Room myRoom;

    public Room(Room r)
    {
        this.myRoom = r;
    }
}

If yes, then it doesn’t seem to keep the variables.
Lets add a string in it:

public class Room
{
    public Room myRoom;
    public string text;

    public Room(Room r, string txt)
    {
        this.myRoom = r;
        this.text = txt;
    }
}

So, I have another script where i create those objects

private Room[] _rooms = new Room[5];
public Room[] data = new Room[5]; // Set values in inspector

private void CreateRooms()
{
   _rooms[0] = new Room(data[0], "123");
   _rooms[1] = new Room(data[1], "321");
  /...
}

And then i have a simple method to display the rooms

public Text someText;

private void DisplayRoom()
{
   someText.text = _rooms[0].text;
}

This works fine BUT if i access the room inside of the constructor and print the text…

public Text someText;

private void DisplayRoom()
{
   someText.text = _rooms[0].myRoom.text[;
   // myRoom.text is data.text that was set in inspector
}

It prints a blank string. Idk what’s wrong, I can’t figure it out from the last night.

I think you may be misunderstanding constructors.

The purpose of a constructor is to be able to create an instance of an object and initialize it with values set through the parameters (if provided). The constructor gets called automatically when you use the new keyword to assign a reference. Consider this somewhat contrived example:

public class Room {
    private string name;
    private Vector2 roomDimensions;
    private float floorSquareFootage;

    public Room(int width, int length, string name = "Unnamed") {
        roomDimensions = new Vector2(width, length);
        this.name = name;
    }
}

public class ConstructorExample: MonoBehavior {
    private void Start() {
        Room livingRoom = new Room(9f, 12f, "Living Room");
        Room extraRoom = new Room(4f, 7f);
    }
}

The constructor allows me to create a new room and set some properties for that room in one place. It wouldn’t make sense for the Room object to have a reference to another Room inside it, especially if that reference is pointing at itself!

I would say just drop the part out of your Room class that has any reference to Room and go from there.

public class Room
{
    public string text;
    public Room(string txt)
    {
        this.text = txt;
    }
}
1 Like

Yeah i think i understand how they work, but its not exactly what im looking for. Im making a simple game where there you choose option one or two and then you go to another room where you make other choices and so on.
This is the class of the Constructor

[System.Serializable]
public class Room
{
    public string story; // this is what happend in the room
    public string text1; // this is what you will do
    public string text2;
    public Room option1; // this is the room you will go to
    public Room option2;

    public Room(string story, string text1, string text2, Room opt1, Room opt2)
    {
        this.story = story;
        this.text1 = text1;
        this.text2 = text2;
        this.option1 = opt1;
        this.option2 = opt2;
    }

I thought of creating another Constructor for the Option like this:

public class Option
{
    public string text;
    public Room room;

    public Option(string text, Room room)
    {
        this.text = text;
        this.room = room;
    }
}

But the problem is that if I want to make for example 20 rooms, I need 20 lines for the rooms and 40 lines for the options. It will be impossible to understand what’s going on with the code or the logic.

I’m a bit embarrassed because im asking you nearly all of the game logic but it would very much appreciated.
Thanks for your time!

1 Like

Ah, I gotcha. I think the piece of the puzzle you’re missing here is what are collectively referred to as Collections.

Collections are things like arrays, Lists, Dictionaries, and my personal favorite, Hashsets. When combined with custom data types, they can be extremely powerful.

public class Room {
    // Room-specific details here
    public string storyText;
    public List<RoomOption> roomOptions;
}

public struct RoomOption {
    public Room gotoRoom;
    public string optionText;
}

public class RoomMovement: MonoBehavior {
    public Room roomData;

    private void Start() {
        // Display the room's storyText to the player

        // Loop through room's roomOptions and create buttons for each
        // Set the button's text to each RoomOption's optionText value

        // OnClick, execute GoToRoom(gotoRoom) for that option
    }

    public void GoToRoom(Room room) {
        // Load room
    }
}

Please keep in mind this is barely above pseudocode level, and I can’t say I’d structure things this way myself, but it should get you moving in the right direction, at least. It’s worth looking into ScriptableObjects and using instances of those instead of plain C# classes, as it provides a number of advantages like Inspector reference assignments which might be particularly useful for you.

1 Like

Big thanks for you man. You really pointed me at the right direction, I haven’t look about ScriptableObjects, but i sure will. Thanks!

1 Like