14 Replies - 486 Views - Last Post: 13 January 2014 - 02:15 AM Rate Topic: -----

#1 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 23-December 13

Class organization for my RPG game

Posted 11 January 2014 - 07:44 PM

Hello everyone.
I am making a RPG game, but today I have realized that my code is a mess, so I decided to create a new project, plan it correctly and then add the code I already have.
My question is about inheritance, I am thinking about the class organization, a diagram, here I have an image:

Posted Image

The idea is that:
- Game is a base class, the parent of all of them, will manage the system variables, game render and update
- The State class manages the game states, the menus are its children
- The are also some classes that inherits from Game.

Now my question is, based on my image and ideas, is this approach correct? I mean, is it logical what I am doing?
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Class organization for my RPG game

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,099
  • Joined: 12-June 08

Re: Class organization for my RPG game

Posted 11 January 2014 - 07:57 PM

that's not quite how I would have it.. I mean there's no good reason to have the 'game class' be the parent of a the player.. Personally I would have a 'generic game object' class that the player, item, npc all inherit from.. you know. .something to hold an ID, location, name, type, etc.
Was This Post Helpful? 0
  • +
  • -

#3 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 23-December 13

Re: Class organization for my RPG game

Posted 11 January 2014 - 08:18 PM

Yeah, I also thought about that, the Player really has nothing to do with the Game class.
I like the idea about the Game Object class, should it be the parent of all the possible objects in my game? I mean, the Player, NPC, Item and all that stuff?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,099
  • Joined: 12-June 08

Re: Class organization for my RPG game

Posted 11 January 2014 - 08:31 PM

That's how I've done it before..
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Class organization for my RPG game

Posted 12 January 2014 - 03:55 AM

I don't really care for the uber base game class. Rather than even think in classes, think in interfaces.

You have Item, given. What basic properties should an Item have? I'd think "usable" would be one of those, btw. How does the Item get interacted with?

If you have an NPC class, then it's probable that Player can be an extension of that. Or that NPC contains an object like Mob and so does Player.

I like to have a "Game State" object. This State object will essentially hold everything that you'd load or save in your game. The State object is passed to other objects that interact with it. If you have the concept of Player, then a Player instance will be in State.

Any game can be boiled down to:
init State
while not State.done
    doSomething with State



Consider your usable item. It might look something like:
Item:
    getName
    isUsable
    use(State)

HealthPotion implements Item:
    isEmpty
    potency
    use(State):
        if not isEmpty:
            State.heal(potency)
            setEmpty



Indeed, isUsable might also take State, if an item is only usable by a given player type, etc.

The idea behind any OO design is to make the objects know only as much about the rest of the program as they have to. Sometimes you see every object in a game storing a reference to state! Instead, every object should react to a state given to it, so it needn't be locked to some global mess.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#6 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 23-December 13

Re: Class organization for my RPG game

Posted 12 January 2014 - 09:13 AM

Thank you very much.

So for example the Game State class is the one that could manage the effects of the items? Also the state of the game, like In Menu, GameOver, Playing, etc...?
Thanks
Was This Post Helpful? 0
  • +
  • -

#7 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 23-December 13

Re: Class organization for my RPG game

Posted 12 January 2014 - 09:31 AM

By the way, here I have redesigned it a little, so It looks like that:

Posted Image
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,099
  • Joined: 12-June 08

Re: Class organization for my RPG game

Posted 12 January 2014 - 09:39 AM

I am a little fuzzy on why the 'game' class is the parent for game object and state? Wouldn't it just be in a class, hanging out, by itself?
Was This Post Helpful? 0
  • +
  • -

#9 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 79
  • View blog
  • Posts: 163
  • Joined: 27-May 13

Re: Class organization for my RPG game

Posted 12 January 2014 - 09:43 AM

Inheritance has to be seen as a "is a" relationship.
Is it logical that a Game Object is a Game?

A game usually "has a" game object (meaning it contains game objects)
Was This Post Helpful? 0
  • +
  • -

#10 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 23-December 13

Re: Class organization for my RPG game

Posted 12 January 2014 - 09:59 AM

Well, I made it like that because the "Game" class also loads some fonts so they can be used in derived classes... So I should make the "Game" class with no derived classes from it?

Thanks

Edit: And the "State" class should be the child of "Game" because "Game" manages some states like Quit()...

This post has been edited by Nicky24: 12 January 2014 - 10:04 AM

Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,099
  • Joined: 12-June 08

Re: Class organization for my RPG game

Posted 12 January 2014 - 10:02 AM

Yes.. that would make substantially more sense.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Class organization for my RPG game

Posted 12 January 2014 - 02:30 PM

View PostNicky24, on 12 January 2014 - 12:59 PM, said:

Well, I made it like that because the "Game" class also loads some fonts so they can be used in derived classes...


GUI and inheritance... shudder.

The objects that make up your game should be data objects. That is, they store information about the game being played. Mixing that with display will cause confusion, pain, suffering, and dead kittens.

If GUI controls need to share fonts and other resources, either pass them from the main GUI object or setup a singleton with shared resources.
Was This Post Helpful? 0
  • +
  • -

#13 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 23-December 13

Re: Class organization for my RPG game

Posted 12 January 2014 - 04:45 PM

Thanks for the reply.
Well yeah, I maybe will have to create a GUI class also, to store the fonts and loading some bitmaps, I think I got the idea now.

Thanks :)/>

This post has been edited by Nicky24: 12 January 2014 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#14 Night Stalker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 26-December 13

Re: Class organization for my RPG game

Posted 12 January 2014 - 11:17 PM

View Postbaavgai, on 12 January 2014 - 02:30 PM, said:

and dead kittens.


ROFL
Was This Post Helpful? 0
  • +
  • -

#15 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Class organization for my RPG game

Posted 13 January 2014 - 02:15 AM

You might want to take a look at the tutorials linked in my signature for more information on setting up your game classes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1