Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
LUA question: may I call methods from a table list? - Gideros Forum

LUA question: may I call methods from a table list?

piepie Member
edited March 2015 in General questions
I am using TNT Particle Engine by @GregBUG, and I was wondering how I could "automate" the process of creating some particles.

I can save an array of "properties" with their respective values - like
myParticle = {
[setColor] = {20,20,255},
[setSpeed] = {10,50}
}
 
--and then call them out like this.
local particle = CParticles.new(particleGFX, 0,0,0,"add")
particle:setSpeed(unpack(myParticle.setSpeed))	
particle:setColor(unpack(myParticle.setColor))
And this works.


May I "read" myParticle values in pairs and call the respective particle/CParticles method?

I made some tests, but the nearest I went was:
 
myParticle = {
[setColor] = {20,20,255},
[setSpeed] = {10,50}
}
 
local particle = CParticles.new(particleGFX, 0,0,0,"add")
 
local metat = getmetatable(particle)	
			for v, func in pairs(metat) do
				print( v, func) 
 
--[[ this print outputs "almost" what I think I need. I just get some methods out of all the available particle methods
 
setEnableAlphaMorphIn	function: 0619ECCC
getAlphaMorphOutEnabled	function: 0619EEAC
getSizeMorphOutEnabled	function: 0619EE94
getColorMorphOutEnabled	function: 0619EE7C
setEnableColorMorph	function: 0619EC24
getMaxDelay	function: 0619E9B4
setMaxLife	function: 0619E96C
getAlphaMorphInEnabled	function: 0619EE34
setSizeMorphOut	function: 0619EB4C
getDirectionMorphOutEnabled	function: 0619EE4C
setSize	function: 0619EA5C
__new	function: 061947C4
setEnableSpeedMorph	function: 0619EC0C
getSizeMorphInEnabled	function: 0619EE1C
setLoopMode	function: 0619EA44
setGravity	function: 0619EA74
getSpeedMorphInEnabled	function: 0619EDEC
init	function: 0619E920
getSizeMorphEnabled	function: 0619EDA4
setSpeed	function: 0619E9E4
]]
 
				if  myParticle[v] then
					print_r(myParticle[v]) --this outputs exactly what I meant. 
					particle[v](unpack(myParticle[v])) --I tought this should work
				end
			end
Here I get an error from TNT Particle Engine:
classes/tntparticlesengine.lua:1109: attempt to index local 'self' (a number value)
stack traceback:
classes/tntparticlesengine.lua:1109: in function '?'

while it was executing setSpeed (so it was "right") but line 1109 is on the wrong side of the if statement (since I provided MaxSpeed) - but This doesn't count, because I am not parsing the real "method table"/external API (how do i call it?).

I suppose I need some lua magic / guru support here: How do I get the "methods" table of a class instance?

I could follow the "bad" way of "if statements chain", but I can't believe that I can't do this with a for loop and all the wonders provided by lua :)

Thank you

Comments

  • Hello pie.
    Here it is:
    particle.spec = particle["spec"]
    particle:spec() = particle.spec(self)

  • piepie Member
    edited March 2015
    Thanks, but I think it's not exactly what I need to make this work - or I misunderstood your answer :)
    I still can't get the "reference" to the methos of particle - and "how" do I call them?
  • Hi again
    The first parameter in the function particle:something() is self.
    Change this line:
    particle[v](unpack(myParticle[v])) --I tought this should work
    To:
    particle[v](self,unpack(myParticle[v])) --I tought this should work

    Look at this attachment
    lua
    lua
    main.lua
    203B
  • piepie Member
    Thank you @amin13a
    At least I learned something new but i still get an error :)

    --this is the output print_r of myParticle[v]
    [1] => 50
    [2] => 250
    --then giderosplayer FC and gives the error:
    classes/tntparticlesengine.lua:1105: attempt to index local 'self' (a nil value)
    stack traceback:
    classes/tntparticlesengine.lua:1105: in function '?'

    Which is different from previous post, this time it happens in the right side of the if statement (in setSpeed() method, so I would think that your information was right). :)
    However it seems that this is not exactly the same thing as calling it from

    particle:setSpeed(unpack(myParticle[v]))

    which works seamlessly
    :-?

    I think I would go for the "if chain" in my project, but this info bothers me from a scientific point of view :D
  • hgy29hgy29 Maintainer
    Hi @pie, try to use particle[v] instead of self as the first argument to your call. This should work as you expect.
  • piepie Member
    @hgy29

    thank you for trying, but nope :D

    classes/tntparticlesengine.lua:1105: attempt to index local 'self' (a function value)
    stack traceback:
    classes/tntparticlesengine.lua:1105: in function '?'

    same error as before.

    However, I should use a better "starting point" than particle metatable: it seems best to use my array.

    let's recap, project attached if someone has a minute to share and try it out :D
     
    --pairs block
     
    myParticle = {
    [setColor] = {20,20,255},
    [setSpeed] = {10,50}
    }
     
    local particle = CParticles.new(particleGFX, 0,0,0,"add")
     
    			for property, values in pairs(myParticle) do				
    					particle[property](particle[property], unpack(values))
    			end
    Is not the same of:
    --standard block
     
    myParticle = {
    [setColor] = {20,20,255},
    [setSpeed] = {10,50}
    }
     
    local particle = CParticles.new(particleGFX, 0,0,0,"add")
     
    particle:setSpeed(10,50)
    particle:setColor(20,20,255)
    How do I write the first block ("pairs block") so that it gives the same results of the second block ("standard block")?
  • hgy29hgy29 Maintainer
    Accepted Answer
    Sorry @pie, I meant:
    particle[property](particle, unpack(values))
    As @anim13a said, the first parameter to a method call is the object called itself.
    particle:setSpeed(10,50) is the same as particle.setSpeed(particle,10,50).

    Likes: pie

    +1 -1 (+1 / -0 ) Share on Facebook
  • piepie Member
    @hgy29 Thank you, it's working now.
    I was sure I tried it already, but maybe I did something wrong last time.. :)

    TNT Particle editor on its way.. :D
  • amin13aamin13a Member
    Accepted Answer
    Hi again.
    Just copy it:
    it works in my attachment.

    myParticle = {
    ["setColor"] = {20,20,255},
    ["setSpeed"] = {10,50}
    }

    local particle = CParticles.new(particleGFX, 0,0,0,"add")

    for property, values in pairs(myParticle) do
    particle[property](self, unpack(values))
    end
    lua
    lua
    main.lua
    487B
  • hgy29hgy29 Maintainer
    @anim13a, beware: in your test project it works only because you don't use the self variable in your something1 or something2 functions. This is really particle that should be passed and not self, because self is not defined (i.e. is nil) at top level.
  • mmm YES.
    thank you hgy29.
Sign In or Register to comment.