Forum C# Extend PlayerIO Game Class?

Extend PlayerIO Game Class?

Postby AniMoney » October 20th, 2013, 2:17 am

This is probably a very stupid question but I can't figure it out.

My game so far works just fine, but most of my serverside code is in one class, the "Game.cs" class I started with It's getting long and unweildy, so I want to refactor my code and split it up into seperate classes. Like "BattleSystem.cs" for example, which will have it's own GotMessage function to recieve messages related to the battle system (attacks and so forth).

So I tried to create this class like so, extending from my GameCode class to pass in all the protected variables I had already made...

Code: Select all
namespace MyGameTitle
{
   [RoomType("Battle")]

   public class BattleSystem : GameCode // GameCode is the name of my base class
   {
        }
}


and then create an instance of it from my Game.cs class when I need it.

Code: Select all
battleSystem = new BattleSystem ();


This is working, and the constructor for battleSystem is firing. But the GotMessage function inside doesn't work, it doesn't seem to be receiving any messages. Am I going about this the wrong way?
AniMoney
 
Posts: 13
Joined: November 26th, 2012, 9:21 am

Re: Extend PlayerIO Game Class?

Postby jbrettob » October 20th, 2013, 1:29 pm

Hey AniMoney,

To clear it up, from what I understand you have the following:
- Game.cs

And you want to clean it up in seperated classes like:
- BattleSystem.cs
- etc.cs


What you can do is the following:
* Note: I'm not sure if this are the right functions or parameters, you should double check it when implementing.
** Note: Do not forget the "using" imports at the top of the class, when creating new .cs classes.

BattleSystem.cs
Code: Select all
namespace MyGameTitle
{
   public class BattleSystem
   {
      void OnGetMessage(MyPlayer player, Message message)
      {
         // do your OnGetMessage logic.
      }
}


In your main class, where you create an instance of new BattleSystem();:

Game.cs
Code: Select all
namespace MyGameTitle
{
   [RoomType("Battle")]
   public class Game:GameCode // GameCode is the name of my base class
   {
      private BattleSystem _battleSystem;

      public override void GameStarted()
      {
         _battleSystem = new BattleSystem();
      }

      public override void GotMessage(MyPlayer player, Message message)
      {
         if (_battleSystem != null)
         {
            _battleSystem.OnGetMessage(player, message);
         }
         else
         {
            Console.Write("_battleSystem is null!");
         }
      )
   }
}



I hope this helps to create separate classes and clean up your project.
Game Designer & Developer | Flash | AS3 | Unity3D | C#
w: jbrettob.com | blog: blog.jbrettob.com | @jbrettob
User avatar
jbrettob
 
Posts: 19
Joined: April 9th, 2012, 3:51 pm
Location: The Netherlands, Europe

Re: Extend PlayerIO Game Class?

Postby AniMoney » October 21st, 2013, 10:45 pm

Oh my gosh thank you! I kept trying to override the GotMessage function or something from inside the BattleSystem class and it wasn't working.

I just did a quick test of your idea and I think it works. This is a simple and relatively elegant solution, thank you very much jbrettob! I'll let you know if I have any problems.

If any PlayerIO staff see this, feel free to chime in with other answers, I couldn't find an answer to this anywhere in the help or documentation.
AniMoney
 
Posts: 13
Joined: November 26th, 2012, 9:21 am

Re: Extend PlayerIO Game Class?

Postby jbrettob » October 22nd, 2013, 8:36 pm

Cool, glad I could help!
Game Designer & Developer | Flash | AS3 | Unity3D | C#
w: jbrettob.com | blog: blog.jbrettob.com | @jbrettob
User avatar
jbrettob
 
Posts: 19
Joined: April 9th, 2012, 3:51 pm
Location: The Netherlands, Europe


Return to C#



cron