Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
button optional data - Gideros Forum

button optional data

MoKaLuxMoKaLux Member
edited August 31 in General questions
how are you doing?
A question please. I am using the button class in a Sprite class. Can I pass extra data to my button?
btn01:addEventListener("click", self.changeScene, data)
btn01:addEventListener("click", self.changeScene, self, data)
https://wiki.giderosmobile.com/index.php/EventDispatcher:addEventListener

Basically I want to avoid doing this for all my buttons:
btn02:addEventListener("click", function()
	playSfx(true)
	sceneManager:changeScene("levels", 1, transitions[13], easings[7])
end)
Thank you :)
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • hgy29hgy29 Maintainer
    Accepted Answer
    You can assign your custom data to the button itself, and fetch the clicked button from the event.
    btn01.data={ ... }
    function Class:changeScene(evt)
     local data=evt:getTarget().data
     ...
    end
  • Thank you hgy29, as we say in french "oui mais non" :)
    This is a way of doing it but it adds another variable to the code.

    I was hoping to get something like:
    EventDispatcher:addEventListener(type,listener,data)

    An example in the wiki could be useful.

    I'll see if I can figure out something.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • keszeghkeszegh Member
    Accepted Answer
    yes, i also had cases when i wanted to send multiple data for an event and maybe even we had discussions on the forum about it but as far as i can remember there was no perfect solution.

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • hgy29hgy29 Maintainer
    Yes, I know, the best way to do it I can think of would be to change Gideros code so that
    addEventListsner(event,handler,data)
    would become
    addEventListerner(event,handler,...)
    and add the necessary
    table.unpack()
    before calling the handler

    Likes: keszegh, MoKaLux

    +1 -1 (+2 / -0 ) Share on Facebook
  • MoKaLuxMoKaLux Member
    edited August 31
    one last thing please :)
    do you have a very basic example for data?

    EventDispatcher:addEventListener(type,listener,data)
    for the wiki: https://wiki.giderosmobile.com/index.php/EventDispatcher:addEventListener
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.