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.

5 thoughts on “Getting caught out by c# in Unity3d and Smartfox

  1. Sounds like you are having fun with Unity! What you’ve been tweeting plus the Jibe demo are close to persuading me to jump in too. Two immediate questions should you be happy to answer – have you found any high level api or modular support for avatar behaviour/interaction in Unity, and are you doing any Voice integration? Voice is the essential for me, and the news of Vivox releasing sensible pricing structures for OpenSim etc is encouraging, but everyone is rolling their own on top of Unity right now I guess

  2. The unity objects dont really make any distinction as such between an avatar or a box or wall. The avatars used tend to be rigged and boned meshes. Unity knows about the animations stored in those and is able to apply things to them. Any object can me made to collision detect or be interacted with via the pointer etc.
    As this is a total toolkit for games you can make it do anything. There are libraries to help though such as the ones used in this demo http://unity3d.com/gallery/live-demos/index.html#3rd-person-shooter
    It is fairly easy to trigger walk and run etc.
    Voice integration. Well unity does do spatial sound though there is not a direct voice proximity service demoed. Jibe use a java voip solution and something like freeswitch would work too. As Opensim can use freeswitch and is mono based I am sure that unity clients can to the same. Thats on my list of things to look at.
    So it depends on the type of voice that you need. Just having any voice channel on to go with the overall avatars on broadcast you might aswell use skype at the same time.
    In fact we have had a few meetings on the demo unity island i had running using unity and avatars but with skype open.
    I am not sure which solution the guys at aworldforus use though. Worth seeing their stuff.

  3. Thanks for that. I believe in the value of spatial voice and the formation of group chats on the fly, in the way that SL allows. Ultimately I’d be interested in voice gestures/lip sync too, which is tricky with a lot of solutions. I know someone doing opensim/mumble integration, so that might be a reasonable way.
    I’ve lookeds at Assemblive from aworldforus, had a couple of meetings. Thought the ease of access and use was great, the voice was pretty good, though not spatial, but the gestures and media integration were poor. Several of us had been in an SL meeting, kept the SL going and hopped into Assemblive using a widget on our own meeting page, which was nice. Once all there we swapped voice from SL to Assemblive 🙂 Kinda slow-motion channel at a time teleport between platforms. Fun. I also looked at VenueGen which is great for gestures and the way it forces the avatar to indicate to eveeryone else where you are looking, that’s pretty good. (Like SL can if you know what you are doing)

  4. I am about to dive into some voice related projects with some opensim and potentially unity3d too. So I will know a whole lot more very soon and keep you posted 🙂
    Proximity audio clearly makes sense and when that needs to be voice then its good to have. The murmur effect of a conversation in the distance can add to atmosphere and also to cross conversational insight. Though many times voice needs to be group chat and the same level for everyone. Otherwise you end up having to hand virtual microphones around to help people ask one another questions in a large environment.
    I must admit that for many things I find voice to be less effective than multiple channels of text and being able to keep up and catchup with threads and context switching. However for more direct things like classes and lectures voice and the directness of it makes sense.
    It is always interesting in games, typically military ones, how voice is or isn’t used. Guild or squad conversation works well for groups that know one another and play together a lot, very often ad hoc groups though don’t talk. The only shared vocabulary is the game mechanic and it amazing how quickly small teams form and work with one another to deal with the known objective.

  5. I must say, I enjoy reading your article. Maybe you could let me know how I can bookmark it ? Also just thought I would tell you I found your website through google.

Leave a Reply

Your email address will not be published. Required fields are marked *

fM5pGMNsM H

Please type the text above:

This site uses Akismet to reduce spam. Learn how your comment data is processed.