Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Creating and dispatching Event more efficiently - Page 2 — Gideros Forum

Creating and dispatching Event more efficiently

2»

Comments

  • totebototebo Member
    edited June 2016
    Resurrection!

    I've started making a DragSelect class (similar to AceSlide but more minimalist). I'm going to use my new, and favourite, CallbackBroadcaster class to trigger callbacks.

    To keep things neat DragSelect inherits from Sprite. A class can't inherit from two classes (I think), so what is the best way to have DragSelect use CallbackBroadcaster? I can think of three options:

    1. Create an instance of CallbackBroadcaster and use that.
    2. Inject methods into Sprite to send the events, as if Sprite inherited from CallbackBroadcaster.
    3. Inherit from CallbackBroadcaster instead of Sprite and manually add the required Sprite functions in DragSelect.

    Is there a fourth option? Not keen on the ones above.
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    4. Have CallbackBroadcaster inherits from Sprite and DragSelect inherit from CallbackBroadcaster

    But obivously this would waste resources...

    Likes: totebo

    +1 -1 (+1 / -0 )Share on Facebook
  • antixantix Member
    edited June 2016
    5. When you create CallbackBroadcaster, also create a global var called CALLBACKBROADCASTER. When you init DragSelect just cache that with self.CallbackBroadcaster = CALLBACKBROADCASTER.

    I assign global vars to pretty much all of my classes. It's so easy to use them in other classes if you stick to a naming system. This is my goto init.lua file..
    ATLAS = RenderTarget.new(2048, 2048)
    FONT = Font.new("fonts/Londrina70.fnt", "fonts/Londrina70.png")
     
    AGUI = require("modules/agui")
    AGUI.init()
     
    FILE = File.new()
    ACRYPT = SimpleCrypt.new()
     
    PREFS = Prefs.new()
    PREFS:loadPrefs()
     
    SFX = Sfx.new()
    SFX:enable(PREFS.soundEnabled)
    SFX:setMasterVolume((1 / 100) * PREFS.soundVolume)
     
    MUSIC = Music.new()
    MUSIC:enable(PREFS.musicEnabled)
    MUSIC:setVolume((1 / 100) * PREFS.musicVolume)
     
    PLAYER = Player.new()
    ENEMYPOOL = EnemyPool.new()
     
    ANIMATOR = Animator.new()
    TIMELINE = TimeLine.new()
     
    BUMP = require("modules/bump").newWorld(128)
    COLLISIONMAKER = CollisionMaker.new()
     
    PARALLAX = Parallax.new()
    LEVELMANAGER = LevelManager.new()
    Damn your tidy code! :D

    Likes: totebo

    +1 -1 (+1 / -0 )Share on Facebook
  • totebototebo Member
    Thanks guys, great to have more options. One day I may learn to love the Pandora's Box that is global vars. :)

    Likes: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Paulo777Paulo777 Member
    edited February 2018
    Hello!
    Is there a way to call
    self.value += 1
    ONCE inside an ENTER_FRAME event?

    This way, when called, it increments forever... I'm wondering if there is a way to do this... 'Cause I've tried everything and I am convinced that it is not possible and That I'll have to find another way out

  • wow this old topic is extremely interesting, thx @Paulo777 :)

    Guys, can somebody please share simple example of code with&without callbacks?
    I just don't understand - what the "callback" actually is? Thanks a lot, guys!

    Likes: Paulo777

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+1 / -0 )Share on Facebook
  • An event is a created object that is passed via the EventDispatcher.

    A callback is a pointer to a function. Calling a function directly, rather than doing it via EventDispatcher, is much faster.

    In my experience you should only use EventDispatcher if you're not calling it very often. If you need to create an EventDispatcher every frame, for instance, it will slow your game down.

    Likes: Apollo14

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    edited February 2018
    I did something like this for Arthur:
    EVENTMAP={}
    local function dispatchEvent(e)
    	e.processed=false
    	for k,f in pairs(EVENTMAP) do
    		if e.processed then return end
    		f(k,e)
    	end
    end
     
    stage:addEventListener(Event.MOUSE_DOWN,dispatchEvent)
    local ok,controller=pcall(function() return require("controller") end)
    if ok then 
    	controller:addEventListener(Event.KEY_DOWN,dispatchEvent)
    	controller:addEventListener(Event.KEY_UP,dispatchEvent)
    	controller:addEventListener(Event.DISCONNECTED,dispatchEvent)
    end
    Then whenever I need to register an event:
    EVENTMAP.Context1=function (ctx,e)
    	local et=e:getType()
    	if et==Event.MOUSE_DOWN then
                -- Do something
    	end
    	if et==Event.KEY_DOWN then
                -- Do something
    	end
    end
    And to unregsiter:
    EVENTMAP.Context1=nil

    Likes: totebo, Apollo14, antix

    +1 -1 (+3 / -0 )Share on Facebook
  • Paulo777Paulo777 Member
    edited February 2018
    @Apollo14 you're welcome!

    What I need is to call a function only once, but I have to call it inside a function that works only in an ENTER_FRAME event

    What I need is simple, just increment a value ONCE every time a collision happens

    What actually goes on is that every collision happens, it starts to increment forever... I just want to STOP IT and increment only once.

    Can anybody give some light over it?
  • Paulo777Paulo777 Member
    edited February 2018
    Hello everybody! Doing more tests, thinking hard putting my neurons to work I got to a solution:

    Initially my collision condition was inside a eventListener explicit
    self.addEventLinstener(Event.ENTER_FRAME, function (self,
     e)
     
    -- code here
     
    end, self)
    but then, I created a separate function and called inside a function that had that explicit code before:
    self:onDetectCollision()
    in the onDetectCollision function I have
    onDetectCollision(event)
     
    if shot:collidesWith(self.ship) then
        --code
     
        self.collided = true
    end
     
    	self:addEventListener(Event.ENTER_FRAME, self.onDetectCollision, self)
     
    	if self.collided then
    		self.increment += 1
    		self:removeEventListener(Event.ENTER_FRAME, self.onDetectCollision, self)
    		self.collided = false
    	end
    end
    All I needed was to create a function that has the ENTER_FRAME event and call it in the trigger function.
    Inside the function with ENTER_FRAME event, set boolean to true if collided, down in the if condition that verifies if collided, increment, remove the event listener and set collided to false.

    Likes: antix, Apollo14

    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.