I’m working on a text adventure and I’ve made classes for objects like you would in a graphical game to try and get used to the editor. I have it working up until I inherited the BaseRoom class to try and make a MazeRoom that you can’t leave until you complete the maze. The MazeRoom methods are never called.
The MainScene.cs script stores your current room: private BaseRoom _CurrentRoom;
The _Input method checks the current room for input keys: _CurrentRoom.CheckInput(ieKey.Keycode);
MazeRoom “hides” the BaseRoom method: public new void CheckInput(Key ieKey)
And I change the logic around so you can’t use the exits and added some GD.Print messages in for good measure, but I’m not seeing any of my new stuff, it’s working just like a regular room and none of my prints are in the log. I’m still a bit new to inheritance, so it’s possible I have no idea what I’m doing and I missed a step. The project is fairly small, I’ll put a copy if anyone wants to look at it. InterfacedTextAdventure
Not sure what not working for You.
You can want make virtual method to override later instead “hides”
you can put code not whole project and project can be on any git.
I’m not sure either, and I wasn’t sure what parts of the code are most relevant to my problem. Suggestions are welcome. Normally the room stores the exits and input keys when the room is loaded and then when the game runs CheckInput() it loops through exits and items to see if the input key matches anything. if you hit an exit that is available it switches rooms. I tried to override that for the maze room, it shouldn’t switch rooms until the maze has been traversed in full. I thought overriding CheckInput with new was all I needed to do to get it to use the new version as long as the loaded room is derived from BaseRoom so it still fits in the _CurrentRoom slot. I can try a different approach if it’ll help… I’ll keep working on it.
I believe I have made some progress. As I feared, it’s not how inheritance works. I was finally able to use the right terms in my search and found someone with my same problem:
It seems I need to rethink how I’m doing it. I’ll post back when I have it working later.
Ok, I was able to get it working. Basically when using an inherited class you have to instantiate it as that type and not as the parent type. Since my container only holds the parent type I need to convert it for it to run the child code.
My global that holds the current room:
private BaseRoom _CurrentRoom;
The parent class
public partial class BaseRoom : Node, ITextInterface, IInputInterface
{
public void CheckInput(Key ieKey)
{
// Do stuff here
EmitSignal(SignalName.SwitchRooms, _MyExits[ieKey].Destination, (int)_MyExits[ieKey].ExitDirection);
}
}
The child class
public partial class MazeRoom : BaseRoom
{
public new void CheckInput(Key ieKey)
{
// Do new stuff
GD.Print("Navigating Maze: Correct Choice");
}
}
And edit the game manager code to check the room type
public partial class MainScene : Node2D
{
public override void _Input(InputEvent iEvent)
{
// Pass the keypress to the room and see if it can be used
if (_CurrentRoom.GetType() == typeof(MazeRoom))
{
// Child method isn't used unless cast as a child
MazeRoom mrCurrent = (MazeRoom)_CurrentRoom;
mrCurrent.CheckInput(ieKey.Keycode);
}
else
{
_CurrentRoom.CheckInput(ieKey.Keycode);
}
// UpdateDisplayText
UpdateDisplayText();
}
}
It might not be the most elegant way to handle it, but it works. Sorry the reply is a bit late, but it bugs me when I visit a forum and find an unanswered question that I need an answer to…