[Suggestion] Custom events to "self"

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

Post Reply
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

[Suggestion] Custom events to "self"

Post by gouxim »

Very howdie,

Idea
There's already an Event system in ManiaScript.
There's already the notion of Custom Events to communicate between CUiLayer / Main script.

How cool would it be to allow a Manialink page (CMlScript context) or a ManiaApp to dispatch a custom event to itself, retrievable in PendingEvents. It would allow a very simple event-based flow for developing maniascript applications.

We are already using a ManiaScript implementation of this pattern. While it works okay and provides a nice proof of concept for the pattern itelf, there are some drawbacks:
* Dependency requirement
* Code requirement to init the lib, run the loop, etc.
* Weird code because of Event representation as an array

Poor man's implementation
Here is our current implementation:

Code: Select all

declare Text[][][] G_Event_PendingEvents;
declare Text[][][] G_Event_UpcomingEvents;

Void Event_Init() {

}

Void Event_DispatchCustomEvent(Text _Type, Text[] _Data) {
    if(_Data.count > 0)
    {
        Debug_Log("[Event] "^_Type^" "^_Data[0]);
    }
    else
    {
        Debug_Log("[Event] "^_Type);
    }
	G_Event_UpcomingEvents.add([[_Type], _Data]);
}

Void Event_DispatchCustomEvent(Text _Type) {
	Event_DispatchCustomEvent(_Type, Text[]);
}

Text[][][] Event_PendingEvents() {
	return G_Event_PendingEvents;
}

Void Event_Yield() {
    G_Event_PendingEvents = G_Event_UpcomingEvents;
	if(G_Event_UpcomingEvents.count > 0) {
		G_Event_UpcomingEvents = Text[][][];
	}
}
Example usage

And here's an example usage with our HTTP library. It dispatches events (HTTP.Success or HTTP.Error) whenever a request finishes.

Somewhere you fire an async HTTP request:

Code: Select all

declare Ident G_HomeRequestId;
G_HomeRequestId =  HTTP_GetAsync("http://example.com/some_xml_webservice");
And in your main loop you listen to the HTTP events for that request Id:

Code: Select all

foreach(Event in Event_PendingEvents())
{
	switch(Event[0][0]) {
        case "HTTP.Success":  {
            if(Event[1][0] == ""^G_HomeRequestId) {
				declare CXmlDocument Xml <=> Xml.Create(Event[1][1]);
				// So something with the response...
			}
		}
		case "HTTP.Error":  {
            if(Event[1][0] == ""^G_HomeRequestId) {
				// So something in case of error...
			}
		}
}



:pop:
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

Re: [Suggestion] Custom events in CMlScript context

Post by gouxim »

Up :yes:
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
reaby
Posts: 956
Joined: 29 Dec 2010, 23:26
Location: Eastern Finland
Contact:

Re: [Suggestion] Custom events in CMlScript context

Post by reaby »

Such Wow :thumbsup:,

personally I have totally missed the posting until now.

...And, it would be very cool 8-)
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

Re: [Suggestion] Custom events in CMlScript context

Post by gouxim »

Also i said CmlScript context, but i really meant "some" context (CManiaApp :yes: )
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
Post Reply

Return to “ManiaScript”

Who is online

Users browsing this forum: No registered users and 1 guest