Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Event suggestion. Just an opinion. — Gideros Forum

Event suggestion. Just an opinion.

plamenplamen Member
edited January 2013 in Suggestions & requests
Using the framework i found real need of custom user broadcast event like the enterFrame or mouseDown , etc. It would be really great to have such event to connect to all registered objects. Even further if we have user defined global timer with event broadcasted to registered objects. Example of use: graphics update goes in enterFrame event, complex logic could be driven with less frame rate like half or third of the graphics and we will need only one timer to run it instead of timer in every object or with LUA iterator outside the objects. Or am i miss something?
Tagged:

Comments

  • moopfmoopf Guru
    Accepted Answer
    I've got my own broadcast event system. It's simple but it works.

    I use the following in my main.lua:
    <pre>
    allEventDispatchers = setmetatable({}, {__mode = "v"})
     
    function broadcastEvent(event)
    	for k in pairs(allEventDispatchers) do
    		k:dispatchEvent(event)
    	end
    end
    Basically, any object that you want to register with it you do this in the init:
    <pre>
    allEventDispatchers[self] = true
    self:addEventListener("game_paused", pauseGame) 
    self:addEventListener("game_resumed", resumeGame)
    Then you can raise events like this:
    <pre>
    local myevent = Event.new("game_paused")
    broadcastEvent(myevent)
    The only thing I've found is that when you remove an object, you need to create a function on the object to cleanup the reference, e.g.
    <pre>
    function myclass:Cleanup()
        allEventDispatchers[self] = true
    end
    Otherwise it stops the object from being collected as garbage which is strange, as it should be a weak table reference (I haven't quite worked out why it's happening at the moment).

    The original post for this was by @atilim in October here: http://www.giderosmobile.com/forum/discussion/comment/13991#Comment_13991
  • @moopf: this is one think that i wanted to get rid of. Things like iterating stuff in LUA, like in my case when objects are like 200-300 hooked to an event, this -
    function broadcastEvent(event)
    for k in pairs(allEventDispatchers) do
    k:dispatchEvent(event)
    end
    end - needs to be done native. /btw pairs iterator is almost 8 times slower than i=1,#allEventDispatchers./
    I am trying to stick to the original API and stay as much inside of "C" compiled code as possible. I know lot of people extend the API by writing many functions in LUA but some things just need speed. For example i really don't like particle system running on interpreter, main reason to remove the missile smoke tail in my project.

    Likes: moopf

    +1 -1 (+1 / -0 )Share on Facebook
  • moopfmoopf Guru
    Accepted Answer
    @plamen - yes, I completely understand. I'm only actually using this broadcast method for pausing an resuming so it's not actually having an impact on performance for me as it's not used during the game to send events. I do completely agree that there needs to be a way to do this natively in Gideros.
  • I found with weak value tables, I had to run collectgarbage a time or two after removing things for it to get rid of them in the tables.

    You could define something an EventDispatcher class and store it on the global level or pass it into your main game class etc and just add event listeners to that object. I saw someone do that in another thread here awhile ago.

Sign In or Register to comment.