Jump to content


Photo

EXTENSION (With Tutorial) Fake Callback


  • Please log in to reply
2 replies to this topic

#1 aiacono

aiacono

    GMC Member

  • New Member
  • 13 posts

Posted 25 September 2011 - 08:56 PM

Hey guys,

Yesterday I posted this: http://gmc.yoyogames...howtopic=520156

Since the functionality wasn't implemented (and from what I can tell, can't be simply) I decided to write a simple solution.

Extension file: http://imkcreative.c...ke_callback.gex
It's called Fake Callback. The reason being, it's not really a callback. It's more like a message bus.
Anyway, let's say you are writing a game that uses async HTTP calls. Infact, you are using jQuery to do so. But you need to communicate the response text to a function / script / object within Game Maker. No problem!
// Your javascript
$.ajax({
    type: 'GET',
    url: '/something.php',
    complete: function(xhr) {
        fake_callback('something_async', ['HTTP Code: ' + xhr.status, xhr.responseText]);
    }
});

Now, make an object in Game Maker. Give it a step action, and have it execute the following code:
// Did any callbacks get called since the last step?
id = fake_callback_query();
if(id == -1) {
    exit; // I guess nothing has been called.
}
show_message(fake_callback_get_name(id) + " called with " + string(fake_callback_get_param_count(id)) + " parameters");
show_message("Parameter 0 was: " + fake_callback_get_param(id, 0));
show_message("Parameter 1 was: " + fake_callback_get_param(id, 1));

See how easy that was?

Edited by aiacono, 25 September 2011 - 08:57 PM.

  • 0

#2 OMGCarlos

OMGCarlos

    GMC Member

  • GMC Member
  • 329 posts
  • Version:GM:HTML5

Posted 15 October 2011 - 05:09 PM

Can you create an example? I have no idea where I'm supposed to put the javascript, what "/something.php" is, or anything else really. This seems like it's exactly what I need...
  • 0

#3 aiacono

aiacono

    GMC Member

  • New Member
  • 13 posts

Posted 24 October 2011 - 01:56 AM

Can you create an example? I have no idea where I'm supposed to put the javascript, what "/something.php" is, or anything else really. This seems like it's exactly what I need...


Hey. Let's say you have two php scripts, one called send_message.php and one called get_messages.php. First things first. You would need to write the javascript that would send an Asyncronous request. This is how I would do it (since jQuery is awesome):
var getMessages = function() {
    $.get('/get_messages.php', function(response) {
        fake_callback('get_messages_async', [response]);
    });
};

var sendMessage = function(message) {
    $.get('/send_message.php?message=' + encodeURIComponent(message), function(response) {
        fake_callback('send_message_async', [response]);
    });
};

You could save that as my_gm_lib.js and then add it into game maker. I'm sure there is details on how to do that somewhere else on this board.
Anyway, then create a message daemon object. Remember, it's a DSL (domain specific language). message_daemon is what we are going to call this object in game maker. It doesn't need a sprite. Give it a step function. Inside your step function put the following code:
// Did any callbacks get called since the last step?
id = fake_callback_query();
if(id == -1) {
    exit; // I guess nothing has been called.
}
if(fake_callback_get_name(id) == 'get_messages_async') {
    // we just got messages
    show_message("Messages: " + fake_callback_get_param(id, 0);
}
else if(fake_callback_get_name(id) == 'send_message_async') {
   // we just sent a message
   show_message("Message sent, response was: " + fake_callback_get_param(id, 0);
}

Now, inside game maker you just call getMessage and sendMessage at any time. When it is done executing, your message daemon will handle the response.

Edited by aiacono, 24 October 2011 - 01:56 AM.

  • 0