Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
calling a function on a parent from a table — Gideros Forum

calling a function on a parent from a table

MoKaLuxMoKaLux Member
edited May 2021 in Game & application design
hope you are all doing well :)
I have this issue and I don't know if what I am trying to do is possible in lua.

I have a table which has a function in it. Now I am passing this table from one class to another.
Is it possible to execute the function on the parent?

Some code for you to see:

Menu.lua:
	...
	local mybtn = ButtonTP9UDDT.new({
		scalexup=1, scalexdown=1.2,
		audio=self:selectionSfxM(), -- this is the function
	}, 1)
	...
ButtonTP9UDDT.lua:
	...
--		if self.params.audio then pcall(self.params.audio) end
--		if self.params.audio then self:getParent():getfenv(self.params.audio) end
		if self.params.audio then
			local fun = self.params.audio
			self:getParent():fun()
--			self.params.audio()
		end
	...
self.params.audio holds the function from the table (checked with type(self.params.audio)).

As you can see, I have tried a couple of things to no avail :*

Is it possible to call a function in a table on the parent?

Thank you for your time ;)

PS: I can do
if self.params.audio then self:getParent():selectionSfxM() end
but I would like to pass the name of the function in a table, that would be more convenient.
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories

Comments

  • rrraptorrrraptor Member
    edited May 2021 Accepted Answer
    Here:
    audio=self:selectionSfxM()
    you call a function

    to pass function as argument:
    audio=self.selectionSfxM
    and simply call like that:
    self.audio(self, other, arguments, here)
    or
    self:audio(other, arguments, here)

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited May 2021
    that worked, I thank you a bunch captain rrraptor. Thank you also for the explanations.

    what is now working code:
    class1:
    	local mybtn03 = ButtonTP9UDDT.new({
    		scalexup=1, scalexdown=1.2,
    		fun=self.myfunction,
    	}, 3)
    class2:
    function ButtonTP9UDDT:init(xparams, xselector)
    	self.params = xparams or {}
    	self.selector = xselector
     
    	self.params.scalexup = xparams.scalexup or nil -- number
    	self.params.scaleyup = xparams.scaleyup or self.params.scalexup -- number
     
    	self.params.fun = xparams.fun or nil -- function
    	if self.params.fun ~= nil and type(self.params.fun) ~= "function" then
    		print("*** ERROR ***", "YOU ARE NOT PASSING A FUNCTION", self.selector)
    		return
    	end
    	...
    end
     
    function ButtonTP9UDDT:onMouseDown(e)
    	if self:hitTestPoint(e.x, e.y) and self:getParent():isVisible() then
    		self.isfocused = true
    		self.isclicked = true
    		self:updateVisualState()
    		if self.params.fun then self.params.fun(self:getParent()) end
    		e:stopPropagation()
    	end
    end
    PS: another alternative would be to add the wanted function directly in the second class, sometimes that is even better this way.

    Viva Gideros! <3
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    edited May 2021
    Note that you can also pass the function name as a string instead of the function itself, if you see fit. Then you can call it
     object[functionName](object,...)
  • MoKaLuxMoKaLux Member
    edited May 2021
    I don't understand the code hgy29 :p :o :)

    how does it fit in this? self.params.fun(self:getParent())

    EDIT: now with the addition below from hgy29 I think we have all available possibilities o:)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    edited May 2021 Accepted Answer
    (self:getParent())[self.params.fun](self.getParent()]

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • rrraptorrrraptor Member
    edited June 2021
    You can remove if statement here:
    if self.params.fun then self.params.fun(self:getParent()) end
    local function dummy() 
    end
     
    function ButtonTP9UDDT:init(xparams, xselector)
    	...
    	self.params.fun = xparams.fun or dummy
    	...
    end
    so you always have a function to call
    self.params.fun(self:getParent()) -- no if

    Likes: MoKaLux

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