[SOLVED] Converting LobbyGUI from .CS to .JS

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Post Reply
iceshaft07
Posts: 33
Joined: 28 Jan 2011, 20:10

[SOLVED] Converting LobbyGUI from .CS to .JS

Post by iceshaft07 »

So I have been trying to implement the LobbyGUI system into my code-- but that requires that I change it to a .JS file from a .CS file because .JS and .CS files don't get along.

Anyhow, I seem to have most of it done, except for two things.

1)
[code]
lock (messagesLocker)
{
messages.Add(user.Name + " joined room");
}
[/code]

How would I perform the above code using UnityScript? I assume that lock(messagesLocker) is a semaphore right? Is there something built in to Unityscript to do this for me? (Also, I really didn't understand why we would need a semaphore here since Lobby.GUI doens't appear to be multithreaded...)

2) I keep getting the following error, possibly related to the above code.
[code]
[SFS DEBUG] Error dispatching event connection :Argument cannot be null.
Parameter name: value
[/code]

I checked the fpsExtension.java, but I do not see where the OnDebugMessage handler get called-- So, I assume it is being called automatically somwhere. If I could figure out what routine is getting called and failing, that would help much more, but I do not see the anywhere in the above error message.

Thanks for your help again!
-Rob
Last edited by iceshaft07 on 11 Feb 2011, 15:15, edited 1 time in total.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

I cant help on UnityScript/JS - because I hate it and shy away from it like the pest. OK OK - not THAT bad, but still.

But to your questions
1)
Yes - its a semaphore.

The reason to add this is, that Unity calls OnGui several times a frame. And if it is redrawing the chat log (saved in messages array) while the API updates it - then you get nasty errors.

2)
New one to me. Could you try to take a look in the console log for me and see if there is more info there?

If you run it in the editor, you need to check editor.log - alternatively player.log

/T
iceshaft07
Posts: 33
Joined: 28 Jan 2011, 20:10

Post by iceshaft07 »

1)
Ok-- the lock doesn't seem to be particularly imperative to the code execution, since it just seems to add a message to the messages array, and it is displayed elsewhere in the code. As a result, I have temporarily commented it out.

2)
I am still tracking down the bug, but it seems to be part of "OnConnection". If I disable this, the bug seems to go away, but the login screen never switched to the lobby screen. I should have the exact line in a few minutes.

Although I have read through the entire documentation for the SFS2, I don't remember absolutely everything. Are the log files you mentioned part of Unity, or are they part of SFS2?

P.S. Thanks for your help, Having a particularly tough start to this week!
-Rob
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

1) Mono (also whats underneath UnityScript) doesnt like that you read (with iterators) from an array that is being updated at the same time.

If you are not going to show a chatlog, then no problem and you can safely remove it :-)

2) The log that the API produces comes from the environment executing it.

So if you run it inside Unity, then it will end up in Player.log or Editor.log. This does require that you setup the API to run in debug mode (true in the constructor). Then it should all dump nicely out there

/Thomas
iceshaft07
Posts: 33
Joined: 28 Jan 2011, 20:10

Post by iceshaft07 »

I found the bug-- it was not SFS, it was my fault. I thought doing

Code: Select all

success = Boolean.Parse((Evt.Params["Success"] as String));
would have successfully converted my code, but it did not. Instead I had to use ToString (I was getting a null reference exception, but because of the event handling, I just got the generic error mentioned above.

Back to 1) Is there some way to implement a semaphore to replace the missing functionality with UnityScript? I think I could go online and figure out how to write locks, but my fear is with multiple threads running the following code:

Code: Select all

static var isLocked : boolean = false;
function MyLockableFunc ()
{
   while (isLocked)
  {
  }
  isLocked = true;

  messages.Add("stuff");

 isLocked = false;
}
Would result in two threads checking

Code: Select all

while (isLocked),
seeing that it is false, and then progressing to

Code: Select all

isLocked = true;
I haven't done much work with semaphores and such, so I may need your help for that-- but for some reason the above code sticks out in my head. But I can't remember if my professor instructed me /not/ to do it that way, or whether it was correct.

Back to 2)
I am assuming that is inside the .cs files included with the demo, right?

Thanks again Thomas, you've been very helpful.
-Rob
iceshaft07
Posts: 33
Joined: 28 Jan 2011, 20:10

Post by iceshaft07 »

Just to let y'all know that it is possible to convert to .JS.

I'd have psoted it, but I didn't think of it 'til after I changed the code.

Anyhow, the code I was looking for to replace the locking mechanism was posted over on the Unity forums-- here's how to replace the lock function:

[code]
Monitor.Enter(messagesLocker);
messages.Add(user.Name + " left room");
Monitor.Exit(messagesLocker);
[/code]

Good luck to anyone else who tries it!
Post Reply