{"id":521,"date":"2010-02-12T10:07:43","date_gmt":"2010-02-12T09:07:43","guid":{"rendered":"http:\/\/www.feedingedge.co.uk\/blog\/?p=521"},"modified":"2010-02-12T10:07:43","modified_gmt":"2010-02-12T09:07:43","slug":"getting-caught-out-by-c-in-unity3d-and-smartfox","status":"publish","type":"post","link":"https:\/\/www.feedingedge.co.uk\/blog\/2010\/02\/12\/getting-caught-out-by-c-in-unity3d-and-smartfox\/","title":{"rendered":"Getting caught out by c# in Unity3d and Smartfox"},"content":{"rendered":"\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=epredator\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n<p>Warning a moderately techie post with no pictures.<br \/>\nI 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 \ud83d\ude42 <\/p>\n<p>Up to now most of the things I have done in <a href=\"http:\/\/www.unity3d.com\">Unity3d<\/a> 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.<br \/>\nHowever, now that I am talking to the<a href=\"http:\/\/www.smartfoxserver.com\"> Smartfox serve<\/a>r pieces it&#8217;s become more useful to to do .cs (C#). <\/p>\n<p>The first is calling a script in a function on another object in the scene.<br \/>\nIn .js its straight forward<br \/>\n<code>GetObject(\"RelevantObject\").GetComponent(\"AScript\").DoWork(\"passingingvalue\");<br \/>\n<\/code><br \/>\nWhere there is a .js file called AScript with a function DoWork in it accepting a string. Attached to the object &#8220;RelevantObject&#8221;<br \/>\nIt is just regular dot notation method access<br \/>\n(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)<\/p>\n<p>In .cs its nearly the same but&#8230;.<br \/>\n<code>BScript someScript = GameObject.Find(\"RelevantObject\").GetComponent<BScript>();<br \/>\nsomeScript.DoWork(\"passingingvalue\");<\/code><\/p>\n<p>Where there is a BScript.cs file attached to &#8220;RelevantObject&#8221; but being c# is is not just the file name but a proper class definition.<br \/>\nSo going from the quick lets just get this done of .js you have to be a bit more rigorous though it all makes sense \ud83d\ude42 <\/p>\n<p><code>using UnityEngine;<br \/>\nusing System.Collections;<\/p>\n<p>public class BScript : MonoBehaviour {<\/p>\n<p>\t\/\/ Use this for initialization<br \/>\n\tvoid Start () {<\/p>\n<p>\t}<\/p>\n<p>\t\/\/ Update is called once per frame<br \/>\n\tvoid Update () {<\/p>\n<p>\t}<\/p>\n<p>\tpublic void DoWork(string sval){<\/p>\n<p>\t\t\/\/do something with sval<br \/>\n\t}<br \/>\n}<\/code><\/p>\n<p>The other problem I had was more related to using Smartfox server.<\/p>\n<p>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. <\/p>\n<p>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.<\/p>\n<p>After a lot of hacking around it seemed that<br \/>\n<code>room.GetVariable(\"myname\"); \/\/doesn't work<br \/>\nroom.GetVariables()[\"myname\"]; \/\/does work  <\/code><br \/>\ni.e. Get the whole hashtable with GetVariables and reference it directly versus getting the GetVariable function to do that indexing for you.<br \/>\nNow 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<\/p>\n<p>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. <\/p>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.feedingedge.co.uk%2Fblog%2F2010%2F02%2F12%2Fgetting-caught-out-by-c-in-unity3d-and-smartfox%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:450px; height:80px;\" allowTransparency=\"true\"><\/iframe><div name=\"googleone_share_1\" style=\"position:relative;z-index:5;float: right; margin-left: 10px;\"><g:plusone size=\"tall\" count=\"1\" href=\"https:\/\/www.feedingedge.co.uk\/blog\/2010\/02\/12\/getting-caught-out-by-c-in-unity3d-and-smartfox\/\"><\/g:plusone><\/div>","protected":false},"excerpt":{"rendered":"<p>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<br \/>\n            <span class=\"excerpt-readmore\"><a class=\"more-link\" href=\"https:\/\/www.feedingedge.co.uk\/blog\/2010\/02\/12\/getting-caught-out-by-c-in-unity3d-and-smartfox\/\"><\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[176,415,175],"class_list":["post-521","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-cloud","tag-code","tag-smartfoxserver"],"_links":{"self":[{"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/posts\/521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=521"}],"version-history":[{"count":2,"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/posts\/521\/revisions"}],"predecessor-version":[{"id":523,"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/posts\/521\/revisions\/523"}],"wp:attachment":[{"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.feedingedge.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}