smartfoxserver


Getting caught out by c# in Unity3d and Smartfox

Warning a moderately techie post with no pictures.
I have been a programmer since I was 14 and I have used an awful lot of programming languages and danced around many a syntax error. When you do spend all day everyday in a particular environment the basics tend to just flow, when you chop and change from code, to architecting to explaining and sharing with people, sometimes the context switch gets more tricky. Todays tech world is also full of many flavours of component that we wrangle and combine. There were two in particular that were causing me hassle and I needed to look up more than once so I figured if I am having the problem then so will someone else 🙂

Up to now most of the things I have done in Unity3d with various experiments use the basic examples and provided scripts or variations on them. These are all in Javascript or the .js deriviative that Unity uses.
However, now that I am talking to the Smartfox server pieces it’s become more useful to to do .cs (C#).

The first is calling a script in a function on another object in the scene.
In .js its straight forward
GetObject("RelevantObject").GetComponent("AScript").DoWork("passingingvalue");

Where there is a .js file called AScript with a function DoWork in it accepting a string. Attached to the object “RelevantObject”
It is just regular dot notation method access
(I know that it is no efficient to go and find the objects and components everytime but it is easier to illustrate the point in a single line)

In .cs its nearly the same but….
BScript someScript = GameObject.Find("RelevantObject").GetComponent();
someScript.DoWork("passingingvalue");

Where there is a BScript.cs file attached to “RelevantObject” but being c# is is not just the file name but a proper class definition.
So going from the quick lets just get this done of .js you have to be a bit more rigorous though it all makes sense 🙂

using UnityEngine;
using System.Collections;

public class BScript : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

public void DoWork(string sval){

//do something with sval
}
}

The other problem I had was more related to using Smartfox server.

I was attempting to set Room Variables. These are server side containers of information that when they change will generate an event to clients that have registered an interest. i.e. very handy to preserve the state of something and send it to multiple clients. The main smartfox Unity Island multiplayer demo uses more direct message to send position transforms for players.

It worked setting the variables, and I could see them in the Admin client. I also got the change message and the name of the variable(s) that had altered, but I kept getting exceptions thrown in the getting of the variables.

After a lot of hacking around it seemed that
room.GetVariable("myname"); //doesn't work
room.GetVariables()["myname"]; //does work

i.e. Get the whole hashtable with GetVariables and reference it directly versus getting the GetVariable function to do that indexing for you.
Now this may just be a wrong version of an imported DLL somewhere. I have not built the entire Smartfox Unity3d plugin again. It does work, so this may be a work around for anyone who is having problems with Room variables. It works enough for me not to worry about it anymore and get on with what I needed to do

Other than that its all gone very smoothly. Smartfox Pro (demo) does what I need and then lots more. I have had conversations and meetings with people in my own Amazon cloud based instantiations of unity powered clients. Now I am over any brain hiccups of syntax again its onwards and upwards.