Forum Scripting Solved - Placing Player.IO codes into a separate class

Post your problems and discussions relating to scripting in Unity here.

Solved - Placing Player.IO codes into a separate class

Postby MayaArcana » July 16th, 2012, 12:43 pm

Hello! Thank you for taking time to read (and hopefully give clear answers to) this thread.

Here is my problem: I am in the midst of creating a game with Unity3D and I am more or less in-charge of the backend systems, hence usage of Player.IO

I need to put the codes from the PIO API into separate classes. So like, I would then have Login.cs and Register.cs and Chat.cs etc

My question is: If the login script for Unity/PIO is returning a connection token, how do I throw that token back to the class which is calling the Login function from the Login.cs class?

i.e. GameManager.cs calls login.LoginUser(username, password). What should LoginUser(username, password) return? A Connection? Or a bool ? I'm currently using bool as the return type, but will the Connection be passed along (logically speaking, I doubt that the connection token is being passed back to GameManager.cs)


Thank you =)
Last edited by MayaArcana on July 17th, 2012, 5:33 am, edited 1 time in total.
MayaArcana
 
Posts: 5
Joined: June 25th, 2012, 11:23 am

Re: Placing Player.IO codes into a separate class

Postby Henrik » July 16th, 2012, 4:37 pm

MayaArcana wrote:i.e. GameManager.cs calls login.LoginUser(username, password). What should LoginUser(username, password) return? A Connection? Or a bool ? I'm currently using bool as the return type, but will the Connection be passed along (logically speaking, I doubt that the connection token is being passed back to GameManager.cs)

It's incredibly hard to answer your question without knowing what it is you want your classes to do. Start writing code or thinking things through, and you'll see that the answers will fall in place.

You call that LoginUser method. Ok. Then what? Maybe you want to load the player object? Ok. Then you need the Client object you got back from calling the Player.IO Connect method. Where did you put that? Did you return it in the LoginUser method? Great, then you have it in your GameManager, right? So put it in a private field there. And now your method (in GameManager) that loads the player object and grabs some data from it can use the Client stored in the GameManager to load the object and grab the properties, and return the data in whatever format you want to expose out. Perhaps like this:

Code: Select all
public class GameManager {
    private Client client;

    public bool Login(string username, string password) {
        client = Login.LoginUser(username, password);
        return (client != null);
    }

    public Something LoadData() {
        if (client == null) { throw new ApplicationException("HALP! MUST CONNECT FIRST!!!");

        var obj = client.BigDB.LoadMyPlayerObject();
        //Grab stuff from obj and return as Something
    }
}
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Placing Player.IO codes into a separate class

Postby MayaArcana » July 17th, 2012, 5:33 am

Hi Henrik!

Thanks for answering my question. I guess my basic question was really: "What should I be throwing back from the Login class if I want to log a user into my game" and yes, after some thought I realised it ought to be the Client object if I wanted multiplayer to really work.

Once again, thanks for the solution =)
MayaArcana
 
Posts: 5
Joined: June 25th, 2012, 11:23 am


Return to Scripting