Page 1 of 1

Awesomium Callbacks

Posted: Sun Aug 22, 2010 6:11 pm
by Morrog
I don't know if this question is appropriate here, but I figured it was worth asking. I am posting it on the forums in case others find this question and hopeful answer useful.

Inspired by your game, and in particular your honest exposition on the blog, I am attempting to integrate Awesomium into my own little hobby project. Setup and rendering were a breeze, and I was quite excited, until I found that there seemed to be no way to return results from onCallback. So when my JavaScript calls, for example, "Engine.GetSomeData()", my engine can't return anything back.

The Question
Since you are using Awesomium, how do you return results from onCallback in Overgrowth, so that your JavaScript code can get data from your engine?

Thank You,
~Morrog

EDIT:
Looking at Awesomium's source, it seems the callbacks are queued up as events. Since they're queued up, and only called later, it makes sense that they can't return a value. Hmmm ...

Re: Awesomium Callbacks

Posted: Sun Aug 22, 2010 10:43 pm
by SamW
Hmmm, after looking at the documentation for Awesomium, (and therefore destroying my previous assumptions and the forum reply that now will never exist) it looks like you receive a JavaScript object name for onCallback and you can set the object's properties using Awesomium::WebView::setObjectProperty(). Therefore, you probably want to write whatever information you get into your JavaScript by putting:
setObjectProperty(objectName, "NewReturnVariable", JSValue([Value]));
In your callback.
After your javascript runs your callback it can assume that "NewReturnVariable" has been created and populated/updated with your information by Awesomium.

Re: Awesomium Callbacks

Posted: Mon Aug 23, 2010 1:23 am
by Morrog
I had thought that as well, until I read the Awesomium code. Javascript sees the callback as an asynchronous call, from what I can tell, so doing:

Code: Select all

Window.GetPosition(); Window.SetPosition(Window.X + 5, Window.Y + 5);
will not work as intended. GetPosition is put into a queue, and physically called later, so Window.X isn't updated while the above code is running.

That is actually a hunk of code I tried yesterday, only to have strange behavior.

If Awesomium were to execute callbacks synchronously, I could easily put JS wrappers around each callback to get the desired syntax:

Code: Select all

 function GetPosition() { Window.GetPosition(); return Window.NewReturnVariable; }

Re: Awesomium Callbacks

Posted: Mon Aug 23, 2010 3:25 am
by SamW
Can you do polling in javascript?
Less being able to send interrupts back to javascript that is the only other thing I can think of right now. Have javascript set Window.X and Window.Y to something invalid like -1 and just poll until both are positive.

Window.GetPosition();
While( Window.X + Window.Y < 0);
Window.SetPosition(Window.X + 5, Window.Y + 5);

BUT... according to this
http://stackoverflow.com/questions/4366 ... t-callback
The above code might block out javascript entirely not allowing awesomium to write to your JS variables or something.

Hmm... looks like awesomium can asynchronously execute (interrupt) code in your JavaScript context Using this.
http://www.khrona.com/docs/awesomium_v1 ... 96db030f90
And put your set position javascript code into the engine rather than the javsascript. So you may try something like this.

JS:
Window.MoveByFives()

Awesomium:

Code: Select all

void MoveByFivesCallback [inheritance blah blah blah] (caller,objectName,callbackName,args)
{
  caller.executeJavascript(
    "Window.SetPosition(" +
    getXposition() + " + 5, " +
    getYposition() + " + 5);") ;
}
Hmm looks confusing without syntax highlights.
getXposition() and getYpositoin() of course, is whatever code you use in the engine to get the x and y values.
So then if your callback is asynchronous, then your setPositon is asynchronous.
And everybody is one asynchronous happy family or some stupid thing like that.
Except for your second code example, because that just wants to by synchronous but it simply cant be in the asynchronous world.