This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-09-25 08:17:23

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

C# Help!

I don't normally do C#, but I need help with it now. I have this code (error marked in red):

using UnityEngine;
using System;
using System.Collections;         // for using hash tables
using System.Security.Permissions;  // for getting the socket policy
using SmartFoxClientAPI;            // to setup SmartFox connection
using SmartFoxClientAPI.Data;      // necessary to access the room resource

public class gui_Login : MonoBehaviour {
   
    // smartFox variables
    private SmartFoxClient smartFox;
    private string serverIP = "127.0.0.1";
    private int serverPort = 9339;        // default = 9339
    public string zone = "city";
    public bool debug = true;
   
    // variables used in script
    private string statusMessage = "";
    private string username = "";
   
    void Awake() {
        Application.runInBackground = true;     // Let the application be running while the window is not active.

        // Create SmartFox connection if not already available
        if ( SmartFox.IsInitialized() ) {
            Debug.Log("SmartFox is already initialized, reusing connection");
            smartFox = SmartFox.Connection;
        } else {
            if( Application.platform == RuntimePlatform.WindowsWebPlayer ) {
              // Only set this for the webplayer, it breaks pc standalone
                // See http://answers.unity3d.com/questions/25122/ for details
                Security.PrefetchSocketPolicy(serverIP, serverPort);
            }
            try {
                Debug.Log("Starting new SmartFoxClient");
                smartFox = new SmartFoxClient(debug);
                smartFox.runInQueueMode = true;
            } catch ( Exception e ) {
                Debug.Log(e.ToString());
            }
        }
   
        // Register callback delegates, before callling Connect()
        SFSEvent.onConnection += OnConnection;
        SFSEvent.onConnectionLost += OnConnectionLost;
        SFSEvent.onLogin += OnLogin;
        SFSEvent.onRoomListUpdate += OnRoomList;
        SFSEvent.onDebugMessage += OnDebugMessage;
        //SFSEvent.onJoinRoom += OnJoinRoom;        // We will not join a room in this level

        Debug.Log("Attempting to connect to SmartFoxServer");
        smartFox.Connect(serverIP, serverPort); 
    }
   
    void FixedUpdate() {
        smartFox.ProcessEventQueue();
    }

    void OnGUI() {
       
        // server IP in bottom left corner
        GUI.Label(new Rect(10, Screen.height-25, 200, 24), "Server: " + serverIP);
       
        // quit button in bottom right corner
        if ( Application.platform != RuntimePlatform.WindowsWebPlayer ) {         
            if ( GUI.Button(new Rect(Screen.width-150, Screen.height - 50, 100, 24), "Quit") ) {
                smartFox.Disconnect();
                UnregisterSFSSceneCallbacks();
                Application.Quit();
            }
        }

        // Show login fields if connected and reconnect button if disconnect
        if (smartFox.IsConnected()) {
            GUI.Label(new Rect(10, 116, 100, 100), "Username: ");
            username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);
            if ( GUI.Button(new Rect(100, 166, 100, 24), "Login")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) {
                smartFox.Login(zone, username, "");
            }
        } else {
            if ( GUI.Button(new Rect(100, 166, 100, 24), "Reconnect")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) {
            Application.LoadLevel("sc_City");
            }
        }
       
        // Draw box for status messages, if one is given
        // Contains some logic to parse message of multiple lines if necessary
        if (statusMessage.Length > 0)
        {
            int boxLength = 61;       // define length of status box
            int messageLength = statusMessage.Length;   // get length of status message
            string originalMessage = statusMessage;  // copy message in to work string
            string formattedMessage = "";            // define output message string
            int i = 0;
            while (i + boxLength < messageLength)      // iterate and add newline until over length
            {
                formattedMessage = formattedMessage + originalMessage.Substring(i,boxLength) + "\n";
                i = i + boxLength;
            }
            // add last piece of original message
            formattedMessage = formattedMessage + originalMessage.Substring(i,  boxLength - (i + boxLength - messageLength));
            // draw status box with message
            GUI.Box (new Rect (Screen.width - 420,10,400,48), formattedMessage);
        }
       
    }

    private void UnregisterSFSSceneCallbacks() {
        // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
        SFSEvent.onConnection -= OnConnection;
        SFSEvent.onConnectionLost -= OnConnectionLost;
        SFSEvent.onLogin -= OnLogin;
        SFSEvent.onRoomListUpdate -= OnRoomList;
        SFSEvent.onDebugMessage -= OnDebugMessage;
        //SFSEvent.onJoinRoom -= OnJoinRoom;
    }

    void OnConnection(bool success, string error) {
        if ( success ) {
            SmartFox.Connection = smartFox;
            statusMessage = "Connected to SmartFox Server";
            Debug.Log(statusMessage);
        } else {
            statusMessage = "Can't connect! " + error;
            Debug.Log(statusMessage);
        }
    }

    void OnConnectionLost() {
        statusMessage = "Connection lost / no connection to server";
    }

    public void OnDebugMessage(string message) {
        Debug.Log("[SFS DEBUG] " + message);
    }

    public void OnLogin(bool success, string name, string error) {
        if ( success ) {
            statusMessage = "Login for user \"" + name +  "\" successful.";
            // Lets wait for the room list
        } else {
            // Login failed - lets display the error message sent to us
            statusMessage = "Login error: " + error;
        }
    }

/* 
    // We will not join a room in this level, the NetworkController in the next scene will take care of that
    void OnJoinRoom(Room room)
    {
        Debug.Log("Room " + room.GetName() + " joined successfully");
        smartFox.SendPublicMessage(smartFox.myUserName + " has joined");
        // We can now move on to the next level
        UnregisterSFSSceneCallbacks();
        Application.LoadLevel("sc_City");
    }

/
   
    void OnRoomList(Hashtable roomList) {
        try {
            foreach (int roomId in roomList.Keys)   {         
                Room room = (Room)roomList[roomId];
                if (room.IsPrivate()) {
                    Debug.Log("Room id: " + roomId + " has name: " + room.GetName() + "(private)");
                }
                Debug.Log("Room id: " + roomId + " has name: " + room.GetName());
            }
            // Users always have to be in a room, but we'll do that in the next level
            /*
            if (smartFox.GetActiveRoom() == null) {
                smartFox.JoinRoom("Central Square");
            }*/
            UnregisterSFSSceneCallbacks();
            Application.LoadLevel("sc_City");
        }
        catch (Exception e) {
            Debug.Log("Room list error: "+e.Message+" "+e.StackTrace);
        }
    }
}

Thank you!

Last edited by WindowsExplorer (2011-09-25 08:17:59)


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#2 2011-09-25 13:47:59

roijac_test
Scratcher
Registered: 2011-08-31
Posts: 49

Re: C# Help!

error massage received?


http://gigabyte.50webs.com/funnyerrormessages/work013.jpg

Offline

 

#3 2011-09-25 13:52:19

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: C# Help!

something like "class, struct, or interface method must have a return type".


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#4 2011-09-25 16:37:34

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: C# Help!

Sorry, I would help, but I know very little about c# I only use it for XNA


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#5 2011-09-25 16:41:30

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: C# Help!

is the website open source done yet, and what's the website address so I an see teh progress?


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#6 2011-09-25 16:57:52

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: C# Help!

I've never coded in C# before, but I'd of thought you would change the word 'Application' to 'smartFox' in the highlighted line, but that's just from looking at what the rest of the objects are called, I will probably be wrong.


http://i.imgur.com/zeIZW.png

Offline

 

#7 2011-09-26 06:50:42

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: C# Help!

are you sure you didn't forget a 'void' somewhere?

Offline

 

#8 2011-09-26 10:38:53

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: C# Help!

Can you explain to us how you fixed it?


http://i.imgur.com/zeIZW.png

Offline

 

#9 2011-09-26 22:38:34

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: C# Help!

WindowsExplorer wrote:

is the website open source done yet, and what's the website address so I an see teh progress?

I haven't made the website address, and i actually haven't actually done it
For a long time, I've been working on my game in XNA. I'll start working on it again, it should be done by Monday of next week.


I am currently http://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=imagehttp://blocks.scratchr.org/API.php?user=GP1&amp;action=onlineStatus&amp;type=text and I finally got over 1000 posts.

Offline

 

#10 2011-09-27 02:47:10

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: C# Help!

GP1 wrote:

WindowsExplorer wrote:

is the website open source done yet, and what's the website address so I an see teh progress?

I haven't made the website address, and i actually haven't actually done it
For a long time, I've been working on my game in XNA. I'll start working on it again, it should be done by Monday of next week.

But I can't wait that long! Can you just give me the link to all the html and php source files and I can work on it? (don't forget to *** your database password  wink  )

Last edited by WindowsExplorer (2011-09-27 02:49:10)


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#11 2011-09-27 12:21:09

markyparky56
Scratcher
Registered: 2008-03-20
Posts: 1000+

Re: C# Help!

Maybe you'll get more help on the Unity community forums: http://forum.unity3d.com/

Unity is quite advanced and I doubt a lot of people here a capable of using it.

Btw, I thought you use Javascript to code Unity, or is that something else I'm thinking of?


http://j.mp/jgVnTq
Check out my game engine development site: NewDawn I'm a Level 171 Scratcher.I am http://bit.ly/nkvLNT

Offline

 

Board footer