4 Replies - 276 Views - Last Post: 02 April 2019 - 02:02 PM Rate Topic: -----

#1 NantucketSleighride   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 119
  • Joined: 13-February 11

Programming different possible actions based on object?

Posted 31 March 2019 - 05:16 PM

Not sure how to better title that... But I'm fiddling around with C#, trying to get better.

I want to ask a question based on a specific scenario I'm facing, but I want to explain that I'm a novice when it comes to programming (I fiddle with it as a hobby back and forth) so anything that's advanced, I probably wont get. So instead of trying to help with something too complicated that I just will glaze over on, I'd say to just tell me to do what I'm doing. Just to save you the trouble of trying to give an example that I'm just not going to get.

So - I'm fiddling with the idea of a roguelike sort of thing; text based... Now Rooms are for the most part rooms - but some will have special things you can do while in them. So let's say maybe in one room you can Sleep(), in another you can Pray(), in another you might Dig()... Whatever.

The way I'm tackling that right now is by having interfaces and inheriting from a basic Room class. So I would have something like
Class SleepRoom: Room, ISleep


or
Class PrayerRoom: Room, IPray



So then I can have all my rooms in some sort of list, connect them together, etc, etc. And when a player types in "Sleep" I would simply take the current room and do:
if (currentRoom is ISleep sleepRoom)
     sleepRoom.Sleep();



The idea being that I'm just taking what is typed - trying to convert that room into the type of room that that action can be done in, and calling that method if it can be.

I'm wondering if there's another way to approach this that I don't know of yet and if it would be in my realm of understanding. I feel like I'm sort of brute forcing the logic here and that there must be some better, intended way of doing something like this.

Again - if the answer is fairly complicated and I'm better off just doing what I've got, just let me know! Don't waste your time with an advanced answer that I won't grasp! Sometimes I get the answers that just make my eyes glaze over, and trying to google them only confuses me more and I feel bad that the person spent time explaining it when I just am too dumb to get it right now.

Is This A Good Question/Topic? 0
  • +

Replies To: Programming different possible actions based on object?

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,671
  • Joined: 05-May 12

Re: Programming different possible actions based on object?

Posted 31 March 2019 - 05:26 PM

You could make full use of polymorphism:
class Room
{
    public virtual void DoRoomFunction()
    {
        // default room does nothing special
    }
}

class PrayRoom : Room
{
    public override void DoRoomFunction()
    {
        // code for praying here
    }
}

class SleepRoom : Room
{
    public override void DoRoomFunction()
    {
        // code for sleeping here
    }
}



so that later in your code you would simply have room.DoRoomFunction() without having to distinguish on the type of specialized room.
Was This Post Helpful? 3
  • +
  • -

#3 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1399
  • View blog
  • Posts: 1,988
  • Joined: 24-August 11

Re: Programming different possible actions based on object?

Posted 01 April 2019 - 03:54 PM

The polymorphic classes in Skydiver's example don't have to be aware of the text captured by the UI - they just need to worry about how to implement DoRoomFunction(). You could keep the same basic evaluation model and just change a few details. In that case, instead of:

if (currentRoom is ISleep sleepRoom)
     sleepRoom.Sleep();


you'd use:
if (currentRoom is SleepRoom )
     currentRoom.DoRoomFunction();



And that's fine.

However, this also gives you the power to manage your text-mapping-to-functionality in a reusable fashion. For example, if you used a config file to map text commands to valid functions on your classes, then you could create a generic command text parsing method completely unaware of the specifics of your object tree that compares input strings to valid commands on your object instances. That way, you can simply check the text against valid commands, and update the UI and game state accordingly, without having to create a nightmarish if/else if chain for each valid command.

In this model, if you need to add a command, you'd simple add the method to your class and then add the text command mapping to the config file, and you wouldn't have to change the command evaluation code at the UI level.

This post has been edited by h4nnib4l: 01 April 2019 - 03:55 PM

Was This Post Helpful? 1
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,671
  • Joined: 05-May 12

Re: Programming different possible actions based on object?

Posted 02 April 2019 - 01:44 PM

Of if you don't use config files, polymorphism comes to the rescue again...
class Room
{
    public virtual bool CanUnderstandVerb(string verb)
    {
        return false;    // a normal room knows nothing
    }

    public virtual void DoRoomFunction()
    {
        // default room does nothing special
    }
}

class PrayRoom : Room
{
    public override bool CanUnderstandVerb(string verb)
    {
        verb = verb.ToLower();
        return verb == "pray" || verb == "worship";
    }

    public override void DoRoomFunction()
    {
        // code for praying here
    }
}

class SleepRoom : Room
{
    List<string> _verbs = new List<string>() { "sleep", "doze", "nap", "snooze", "catnap" };

    public override bool CanUnderstandVerb(string verb)
    {
        return _verbs.Contains(verb.ToLower());
    }

    public override void DoRoomFunction()
    {
        // code for sleeping here
    }
}



And so at some point in your parser you would simply have:
if (room.CanUnderstandVerb(verb))
    room.DoRoomFunction();


Was This Post Helpful? 0
  • +
  • -

#5 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1399
  • View blog
  • Posts: 1,988
  • Joined: 24-August 11

Re: Programming different possible actions based on object?

Posted 02 April 2019 - 02:02 PM

Agreed - that's actually where I started, but ended up going down the config file path to avoid keeping magic strings in the classes themselves and at some point in my editing I dropped the part about mapping the verbs from config back to a collection on the classes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1