GameMonkey Script

GameMonkey Script Forums
It is currently Tue Jun 18, 2019 3:14 pm

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: gmMulticastFunctionLib
PostPosted: Thu Apr 02, 2009 8:57 pm 

Joined: Thu Jan 01, 2004 4:31 pm
Posts: 307
Attached to this post is version 1.0.0 of the gmMulticastFunction library binding for GameMonkey Script 1.25j (may work with older versions).

This binding provides a multicast function type and supporting functions to the GameMonkey Script environment. Multicast functions are similar in purpose to multicast delegates in .NET and allow functions to be chained. This is useful for event handlers and other such routines that require multiple functions to be called with the same set of parameters.

Due to the way GM works, a multicast function cannot be used as a direct replacement for a function, so you must call multicastInvoke() or multicastSchedule() to execute the functions. The multicastInvoke() function calls each function in the multicast immediately, whereas the schedule function will create threads and leave them pending until the next execute cycle

Usage is simple:

// Create some test functions
a = function(x) { print("test func 1", x); };
b = function(x) { print("test func 2", x); };
c = function(x) { print("test func 3", x); };

// create a multicast of them
m = multicastfunction(a, b, c);
// Invoke passing a parameters

I should add that passing 'this' is fully supported by the multicastInvoke()/multicastSchedule() methods, allowing you to pass an object as 'this' to every function in the chain.

a = function() { print("hello from",; };
b = function() { print("goodbye from",; };
m = multicastfunction(a, b);
t = table( name = "peter" );

You should ensure that all function signatures are the same (eg: accept the same params) if you want to get the best out of them. This version doesn't let you add other multicasts to a multicast, but if people want I can add it easily (and may do for completion).

You can use the multicastAddFunction(), multicastInsertFunction() and multicastRemoveFunction() functions to add, insert and remove functions to the multicast. You can also call multicastCountFunctions() to return the number of items in the multicast. Note that it's possible to add the same function multiple times to the multicast, allowing you to call the same function several times in a chain if you need to. Removing this function will remove all instances of it.

Additionally, the GetInd/SetInd operators are implemented, allowing you to swap out or nullify functions at specific places in the multicast.

This library can only be bound to one machine at a time and currently only supports the incremental garbage collector. Memory management is done internally using the gmMemFixed routines from the GM library, so no other dependencies are needed.

To install the library, add the gmMulticastFunctionLib.cpp file to your project, and include the gmMulticastFunction.h header. In your code, be sure to bind the library using:

gmMachine gm;

Any problems, please let me know. If you require any other features, again please let me know.

I hope this is useful!

File comment: v1.0.0 [3.87 KiB]
Downloaded 286 times
Reply with quote  
PostPosted: Thu Apr 02, 2009 9:27 pm 

Joined: Fri Jan 14, 2005 2:28 am
Posts: 439
neato. I did something similar a while back with a simple "FunctionChain" object, but I used +=, -= and I believe a .call and .queue function on the object itself. I ended up going another direction and don't really use the object anymore but it's a pretty neat tool.

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group