Forum C# UserJoined - no suitable method to override

UserJoined - no suitable method to override

Postby bohdan » October 20th, 2011, 5:10 am

Hi guys,

My UserJoined function is not being called when a user joins the room. My only hint is that I'm getting an error in it's declaration when I write "override". It says there is no suitable method to override, so I take it that it is somehow not correlating my UserJoined function to the function that it calls when a user joins. I hope that makes sense. Here's my code:

Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using PlayerIO.GameLibrary;
using System.Drawing;
namespace FridgeMagnets
{

    public class Player : BasePlayer
    {
        public int x;
        public int y;
    }

    public class Room
    {
        public int roomWidth;
        public int roomHeight;
    }

    public class Utils
    {
        public static int getRandomNumberFrom(int maxValue)
        {
            Random random = new Random();
            return random.Next(0, maxValue);
        }
    }

    [RoomType("ConnectTest")]
    public class GameCode :Game<BasePlayer>{

        private Room room = new Room();
        private Player[] playerArray = new Player[45];
        private int numPlayers = 0;

        public override void GameStarted()
        {
            Console.WriteLine("GAME STARTED");
        }

        public override void GotMessage(BasePlayer player, Message message)
        {
            Console.WriteLine("GOT MESSAGE");

        }
//THE PROBLEM METHOD - IT ISN'T CALLED
        public override void UserJoined(Player player)
        {
            sendInitInfo(player);
            addNewPlayer(player);
           
            Console.WriteLine("USER JOINED");
           
            //set player location

        }



To reiterate, my UserJoined is not being called when users join. Any ideas why?
bohdan
 
Posts: 30
Joined: June 24th, 2011, 8:18 am

Re: UserJoined - no suitable method to override

Postby bohdan » October 20th, 2011, 5:17 am

Hmm, I got it to work by changing the parameters in UserJoined from Player to BasePlayer.

I swear I tried that before and it didn't work. Oh well, cheers.
bohdan
 
Posts: 30
Joined: June 24th, 2011, 8:18 am

Re: UserJoined - no suitable method to override

Postby Henrik » October 20th, 2011, 4:58 pm

You should change your class GameCode so it inherits Game<Player> instead of Game<BasePlayer>. Like this:

Code: Select all
[RoomType("ConnectTest")]
public class GameCode :Game<Player> {
    //...
}

Here's the longer explanation: Game is a generic class with generic methods. It's declared something like:

Code: Select all
public abstract class Game<P> {
    public virtual void UserJoined(P player) {}
    public virtual void UserLeft(P player) {}
}

When you made your GameCode class inherit Game<BasePlayer>, that P type argument is substituted with BasePlayer everywhere, which means that your class has the method

Code: Select all
public virtual void UserJoined(BasePlayer player) {}

And when you declared

Code: Select all
public override void UserJoined(Player player) {
    //...
}

The compiler gave you an error saying there was no method to override, which is correct, because those two method signatures are not the same.

I hope that helps. :-)
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: UserJoined - no suitable method to override

Postby bohdan » October 20th, 2011, 7:08 pm

Hey Henrik,

I still don't know most of the C# notation, so I wasn't even sure what that line was doing. Now it works.

Thanks for the speedy response!

Bo
bohdan
 
Posts: 30
Joined: June 24th, 2011, 8:18 am


Return to C#