Sunday, July 1, 2012

Running and Debugging a Room Extension

This past week I've been coding and debugging the client classes in Unity that will connect to the Smart Fox Extension that I coded for my previous post. Throughout the process I realized how expansive, out of control, and volatile a project can get up until you sort out the problems. The Smart Fox forums were the best source of information, but the information you need is difficult to find and even more difficult to validate. I'd like to explain it here to keep you from having to trudge through the forums as much as I did.

The first thing to do is to decide which Unity classes from the First Person Shooter example would help accomplish one seemingly simple goal. I'll state the goal. "I want smart fox server to respond to a spawn Me request from a Unity client. The server will send a transform (location coordinates) and character prefab name for the client to load into the scene after I log into the room." That was the primary goal, but I also coded beyond that to broadcast a message to all other players in the room so that each client will render every player in the same room.

Previously I used Eclipse to create 3 classes: Connector, ChatWindow, and SmartFoxConnection. Now, to interact with my extension, I've improvised on these other classes from the FPS example: Network Manager, Player Manager, Network Transform, and Network Transform Receiver/Sender. After some trial and error, I decided that Net Beans with Java SE was a much better IDE for this kind of development. It is easier to create a Jar and to attach a debugger with Net Beans.

Once you have set up your project in Net Beans, you can edit the build.xml file to automatically copy your jar by adding these lines of code. (replace the ** items with the actual paths and folder names)



    Builds, tests, and runs the project MyExtension.
    
    
        
            
        
    



Now that you have a .jar use the Smart Fox administrator tool to load the file into your room. I've noticed that when you use a line like "ExtensionRequest request = new ExtensionRequest("spawnMe", new SFSObject(),smartFox.LastJoinedRoom)" it will only work if you add the extension to the room and NOT the zone. Look to the forums for guidance. Adding an extension to the zone is more efficient if you have several rooms. But, for now, you are only trying to get the extension to work within one room in a particular zone.

It's time to debug your extension. It is ALWAYS best to make sure the stuff works on your localhost first. Leave the extension's project open in NetBeans. Using the admin tool go to server configurator and add 127.0.0.1 TCP port 8787 to the allowed incoming connections. Then, you should save your sfs2x.bat in C:\Program Files\SmartFoxServer2X\SFS2X as sfs2x_debug. Using notepad++ or any text editor, the contens of sfs2x_debug should be:

@java -cp "./;lib/*;lib/Jetty/*;extensions/__lib__/*" -Xdebug -Xnoagent 
-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n 
-Dfile.encoding=UTF-8 com.smartfoxserver.v2.Main $1 $2 $3

You don't "have" to use 8787, that's just what was suggested in the forums. If you're running a smart fox server, then stop it, and run this .bat instead. It will load a smart fox server that is perfectly capable of debugging with Net Beans or Eclipse (though I suggest Net Beans). Click Debugger->Attach Debugger (after you've started the sfs2x_debug.bat. When you run the project in Unity (with Net Beans project open and attached) then at the break points you've set in net beans, you can see the values of your objects and enjoy the freedom given to you by a debugger. You can also run Unity in debug mode at the same time. A debugger has 3 important tools that look like arrows. Step Over (executes a line of code and all it's internal functions), Step Into (shows you the line of code that is being called: very good for stepping through code that you don't exactly understand), and Step Out (exit the complicated inner workings of code). Use the debugger as much as you can to help avoid hours of searching for errors. I also suggest that in your Unity .cs files you add to your Debug.log ("message") at points in the code you want to make sure are being executed. If you don't see the messages in your Unity console, then you know that that line of code has not been called properly.

These debuggers are the absolute best way to help you code your extensions. If you have any questions, contact me through www.GuitarRpg.com and I'll try to help you out.

4 comments:

  1. Thanks you so much!, it is very useful for beginer when study about SFS.

    ReplyDelete
  2. Hey Le Van Toan!! Thank you for the comment. That encourages me to write a new post soon. Is there any topic you would like me to write about?

    ReplyDelete
  3. Hey, I actually ran into a problem because of this. If you add the debug port to the list of IP Addresses then Eclipse can't connect to SmartFox. NetBeans still can. I believe that those IP Adresses are actually for client connections so you shouldn't have the debug server port in there.

    ReplyDelete
    Replies
    1. When you removed it, did it still work from NetBeans? I'll experiment some tonight and update the post if necessary.

      Delete