Deleting Moai Props / Ease Drivers

New to Moai? Get started here with our best tips and tutorials.

Deleting Moai Props / Ease Drivers

Postby Phillipk » Sun Mar 18, 2012 9:11 am

Heyho!
Its me again :D

I build a few elements for a planned game, like a Gui (buttons, onclick functions, and so on). At the moment i try to debug some leaks in my programm.
Heres some codesnippet, that dont work really nice:
Code: Select all
  1. function TGuiButton:makeHover(gui, lastPicked)

  2.         function deleteHover(hover)

  3.         --      print(hover)

  4.                 hover.gui.layer:removeProp(hover.prop)

  5.                 hover.prop = nil

  6.                 hover = nil

  7.         end

  8.        

  9.         for i,_frm in pairs(lastPicked.frame) do

  10.                 local prop = _frm.prop

  11.                 local x,y = prop:getLoc()

  12.                 local sx,sy = prop:getScl()

  13.                 local index = prop:getIndex()

  14.                 local frm2 = gui:addFrame(prop.deck,x,y,index)

  15.                 frm2.prop:setScl(sx,sy)

  16.                 local prio = prop:getPriority()

  17.                 frm2.prop:setPriority(prio+0.1)

  18.                 frm2.prop:setBlendMode(MOAIProp2D.BLEND_ADD)

  19.                 --self.lastPicked.hover = frm2

  20.                

  21.                 frm2.prop:setColor(1,1,1,0.5)

  22.                 local action = frm2.prop:seekColor(1,1,1,0,1)

  23.                 action.gui = gui

  24.                 action.prop = frm2.prop

  25.  

  26.                 action:setListener(MOAIAction.EVENT_STOP, deleteHover)

  27.         end

  28. end



lastPicket is a TGuiButton object, thats under the mouse.
When a click is performed, this code is launched. It makes a copy of the Props that the Button contains (in the most times, its just one frame / prop).
The blend is a simple addition, to make a lighten-effect.
After the fadeout (seekColor to an alpha value of 0) it should be deletet.

Now, i got some buttons, that print several values.
One uses MOAISim.reportHistogram.

Here i can see, that each time i click the button, the MOAIEasedriver-count and MOAIProp count gets plus one. So, i guess this hovering-prop never gets deletet.
When i use the MOAISim.forceGarbageCollection(), the game laggs like hell.

Is there a good way, to delete Props and Easedrivers when i dont need them anymore? Because i use Easedrivers very often :(

I tryed to give the program some time, so i started it and walk in the garden for ~ 1hour - after that period of time, there was a huge ammount of unused ease drivers (somthing arround 20k+) - thats probably a huge performance loss :-/

(ps: Sorry again for my bad english :( I really try to write it understandable :cough:)
Phillipk
 
Posts: 10
Joined: Sun Mar 04, 2012 5:31 am

Re: Deleting Moai Props / Ease Drivers

Postby pip010 » Mon Mar 19, 2012 10:44 am

this seems really complicated to me.
I rather not bother dynamically adding-removing the extra props. always keep your button composed of two props in two different layers/partitions and simply make the hoer prop visible on mouse hover, otherwise go hidden!
you need two layers cause I found out that in MOAI making a prop invisible will stillcontinue to return your prop when you query the prop under the mouse coord (e.g. local pick = partLayerUnits:propForPoint ( mouseX, mouseY ) )
pip010
 
Posts: 18
Joined: Wed Feb 29, 2012 4:35 am

Re: Deleting Moai Props / Ease Drivers

Postby Phillipk » Mon Mar 19, 2012 11:20 am

Okay, thats a solution for the prop problem. Thank you =)

So, ill try to use the lua-own GarbageCollection, it tooks a parameter that looks like a number, how much objects will be searched. I dont know, if it will work, and i dont know, if it work on other devices. If it wont work, ill use a second layer for the prop problem :)
But: WHY does ease drivers doesnt get collected? The moving/seeking functions are one of the best parts of moai - i guess, its most used :(
Did i miss any options? Is there a way to manually run the garbage collector until it collected ~ 10 objects? (somthing that i can run every 1-2 seconds without any lagg, even on smart phones with lower cpu / ram)
The build in GC-force laggs for 2-3seconds, that suxx.
Phillipk
 
Posts: 10
Joined: Sun Mar 04, 2012 5:31 am

Re: Deleting Moai Props / Ease Drivers

Postby dana » Mon Mar 19, 2012 12:26 pm

Pip, that issue with the partitions sounds like a bug that we're currently working on. I'm not going to make any promises since we are pretty busy preparing for the 1.0 launch, but hopefully that fix will be made for 1.0. We have some progress on it but it isn't complete yet. It sounds like our changes should fix the issue with the prop still being picked when it's invisible, but I will be sure to verify your example when our changes are complete.

Phillip, I'm not sure why it doesn't seem to do garbage collecting. I will ask Patrick about that when he's available and I'll get back to you.
User avatar
dana
Site Admin
 
Posts: 200
Joined: Fri Mar 02, 2012 6:10 pm
Location: Zipline Games

Re: Deleting Moai Props / Ease Drivers

Postby pip010 » Mon Mar 19, 2012 1:28 pm

@dana well, you might keep with an optional param. it might be that some people will expect such behavior. after all its not only for GUI the layers migth be used but alot of hackery too ;)

@Phillipk the reason must be because of your persistent reference passing between your functions. also use local! use local ese = MOAIEaseDriver:new etc. . do generic routines.
Code: Select all
  1. function animDamage(unitAttacking,unitDefending)

  2.         function animEnd()

  3.                 propDamage:setScl(1,1)--if removed will have strange random effect

  4.                 propDamage:setVisible(false)

  5.         end

  6.        

  7.         assert(unitAttacking)

  8.         assert(unitDefending)

  9.         local d_x, d_y = unitDefending:getLoc()

  10.         local a_x, a_y = unitAttacking:getLoc()

  11.         --print( "anim fight", a_x , a_y, d_x, d_y )

  12.        

  13.         --propDamage:setScl(-1,-1)

  14.         propDamage:setLoc( a_x, a_y )

  15.         propDamage:setVisible(true)

  16.        

  17.  

  18.        

  19.         local ease = MOAIEaseDriver.new ()

  20.         ease:reserveLinks ( 4 )

  21.         ease:setLink ( 1, propDamage, MOAIProp2D.ATTR_X_LOC, d_x-a_x )

  22.         ease:setLink ( 2, propDamage, MOAIProp2D.ATTR_Y_LOC, d_y-a_y )

  23.         ease:setLink ( 3, propDamage, MOAIProp2D.ATTR_X_SCL, 2 )

  24.         ease:setLink ( 4, propDamage, MOAIProp2D.ATTR_Y_SCL, 2 )

  25.         ease:setLength ( 2.5 )

  26.         ease:setListener ( MOAIAction.EVENT_STOP, animEnd )

  27.         ease:start ()

  28.        

  29.         MOAIThread.blockOnAction ( ease )

  30. end

  31.  


this is my anim i play every time a unit attacks another. i keep it generic as to all i need to know is prop of the one and the prop of the other. (in fact I might need only locations) Also the prop of the anim I only hide not remove and GC, since my game is turned based and Im blocking! so only one anim will be played at a time.
pip010
 
Posts: 18
Joined: Wed Feb 29, 2012 4:35 am

Re: Deleting Moai Props / Ease Drivers

Postby dana » Mon Mar 19, 2012 5:32 pm

Pip, there was a bug where if you set up a partition and then put props on a layer, then move the layer, the buttons would move but their locations weren't updating when using propForPoint on the partition, ie. you could call propForPoint on their starting location and it would still return the button. It sounds kind of like this issue is having similar results as what you described when setting the button to invisible, which is why I think maybe it will be fixed when this fix is made. I'm not sure, but in any case I'm going to make a sample where I do what you describe and I'll verify that it works as you would expect!

Phillip, I asked about your garbage collection issue and was told that in Wolf Toss we run the garbage collector incrementally in small pieces so it doesn't lag the game. You're also able to re-use EaseDrivers, you can build a cache of them and keep using them, which would increase your performance.

If you need help doing this let me know and I'd be happy to support you!
User avatar
dana
Site Admin
 
Posts: 200
Joined: Fri Mar 02, 2012 6:10 pm
Location: Zipline Games

Re: Deleting Moai Props / Ease Drivers

Postby Phillipk » Tue Mar 20, 2012 2:38 am

Well, i got the reuse-idea after reading pip's code.
This is a great idea that i dont got before :>

Ill update my scripts, so that every button and object (in my case, a "house" - windmill with spinning wheel) use another prop and some easedrivers for moving / coloring.
I dunno, if i can reuse ease drivers for seeking the color, but ill look up the docs :>

Heres another code snippet, that produces this 'error', it makes a huge ammount of ease drivers in little time:

(windmill-init, a little piece of the whole code, that effects the ease driver):
Code: Select all
  1.                 h.animProp = MOAIProp2D.new() -- the animation-prop

  2.                 h.animProp:setLoc(x+7,y+20)

  3.                 h.animProp:setDeck(self.deck)

  4.                 h.animProp:setIndex(1)

  5.                

  6.                 local windSpeed = math.random(2,5) -- some debug value, later it'll be global

  7.                

  8.                 local action = h.animProp:seekRot(-360, 3 / windSpeed, MOAIEaseType.LINEAR) -- the animation: a 360° turn.

  9.                 action.house = h

  10.                 action.windSpeed= windSpeed

  11.                 action:setListener(MOAIAction.EVENT_STOP, repeatRotation) -- the listener, follows in the next codeblock



Code: Select all
  1. function repeatRotation(old)

  2.         -- as i get so far, 'old' is the old action given to the listener function.

  3.         old.house.animProp:setRot(0) -- reset the rotation to 0 instead of -360, avoiding some integer overflow that may come in thousends of hours playing the game :D

  4.         local action = old.house.animProp:seekRot(-360, 3 / old.windSpeed, MOAIEaseType.LINEAR) -- the new ease driver

  5.         action.house = old.house -- give the 'parameter' to the new action

  6.         action.windSpeed = old.windSpeed

  7.         action:setListener(MOAIAction.EVENT_STOP, repeatRotation) -- the new listener.

  8.         old:clear()

  9.         old.house = nil

  10.         old.windSpeed = nil

  11. end



as i can see, theres no other reference to the old ease driver. So, thats why i couldnt understand, why it wont get collected.

In the windmill code i can super reuse the ease driver - no problem, i guess. Well, maybe i should use the same method like pip, means that i fill an ease driver 'manually':
Code: Select all
  1.         local ease = MOAIEaseDriver.new ()

  2.  

  3.         ease:reserveLinks ( 4 )

  4.  

  5.         ease:setLink ( 1, propDamage, MOAIProp2D.ATTR_X_LOC, d_x-a_x )

  6.  

  7.         ease:setLink ( 2, propDamage, MOAIProp2D.ATTR_Y_LOC, d_y-a_y )

  8.  

  9.         ease:setLink ( 3, propDamage, MOAIProp2D.ATTR_X_SCL, 2 )

  10.  

  11.         ease:setLink ( 4, propDamage, MOAIProp2D.ATTR_Y_SCL, 2 )

  12.  

  13.         ease:setLength ( 2.5 )

  14.  

  15.         ease:setListener ( MOAIAction.EVENT_STOP, animEnd )

  16.  

  17.         ease:start ()



Good, now i got some other trys todo :)
But i got another question:
Phillip, I asked about your garbage collection issue and was told that in Wolf Toss we run the garbage collector incrementally in small pieces so it doesn't lag the game.

-> How can i do that? That was another idea, that i dont tryed yet.
The only GC function that i could found was the MOAISim.force thingy, but - said by the doc's - it doesnt took any parameters! :(

EDIT:

Okay, iam unable to reuse the ease drivers :( I tryed to reset all attributes, like setLink, reserveLink, start, listener etc.
In some variations, like all together, just start() an so on - but the listener event keeps getting performed after the first delay - the time doesnt seems to get reset.
Can anyone explain me, how i can do this?
At the moment, work on a reuse system for moai props. - Every gui element gets a set of props, that gets updatet (index, position, scale..) instead of recreate them. After that, ill try to find some GC options / functions from lua + moai, to avoid unused ease drivers :)
Phillipk
 
Posts: 10
Joined: Sun Mar 04, 2012 5:31 am

Re: Deleting Moai Props / Ease Drivers

Postby dana » Tue Mar 20, 2012 12:15 pm

Would you be able to post your code that's recycling the EastDrivers, and I can look into the issue. If there's a bug and I can get a reproduction case for it then we'll get it fixed.

Thanks!
User avatar
dana
Site Admin
 
Posts: 200
Joined: Fri Mar 02, 2012 6:10 pm
Location: Zipline Games

Re: Deleting Moai Props / Ease Drivers

Postby Phillipk » Wed Mar 21, 2012 4:03 am

Okay thank you :)
Well, i better post the whole code, that "sets" my windmill on the map.
There several things, that may take influence, but i dont know:
1) The code is a seperate file, launched with loadfile(), out of "THouse.lua"
2) THouse.lua holds some empty functions for house-objects, to avoid a nil-call on functions (events, like build and onClick)
3) The house-files overload theses empty dummys with own functions, they may include other scrips (here: the windmill-file loads another lua, called anim.lua, where the 'repeateRotation' functions is set

Heres the build-code of the house:
Code: Select all
  1. house.build = function(self, x,y)

  2.                 local h = self:copy()

  3.                 THouse.exists[table.getn(THouse.exists)+1] =h

  4.                

  5.                 h.prop = MOAIProp2D.new()

  6.                

  7.                 h.prop:setLoc(x,y)

  8.                 h.prop:setDeck(self.deck)

  9.                 h.prop:setIndex(2)

  10.                

  11.                 layer:insertProp(h.prop)

  12.                

  13.                 h.animProp = MOAIProp2D.new()

  14.                 h.animProp:setLoc(x+7,y+20)

  15.                 h.animProp:setDeck(self.deck)

  16.                 h.animProp:setIndex(1)

  17.                

  18.                 local windSpeed = math.random(2,5)

  19.        

  20.      -------------- >> MOAIEaseDriver initialisation   

  21.         local ease = MOAIEaseDriver.new ()

  22.         ease:reserveLinks ( 1 )

  23.         ease:setLink ( 1, h.animProp, MOAIProp2D.ATTR_Z_ROT, -360,MOAIEaseType.LINEAR)

  24.         ease:setLength ( 3 / windSpeed )

  25.         ease.house = h

  26.         ease.windSpeed = windSpeed

  27.         ease:setListener ( MOAIAction.EVENT_STOP, repeatRotation )

  28.         ease:start ()

  29.                

  30.     ------------------->> Old version of the animation

  31.         --      local action = h.animProp:seekRot(-360, 3 / windSpeed, MOAIEaseType.LINEAR)

  32.         --      action.house = h

  33.         --      action.windSpeed= windSpeed

  34.         --      action:setListener(MOAIAction.EVENT_STOP, repeatRotation)

  35.    ----------------------------        

  36.  

  37.  

  38.                 layer:insertProp(h.animProp)

  39.                

  40.                 return h

  41.         end



Heres the "repeatRotation" function, that wont work correctly:
Code: Select all
  1. --[[

  2.     I tryed several version:

  3.                 1) The version showed below

  4.                 2) without clear, stop, reserveLinks

  5.                 3) without 2) and setLink

  6.                 4) without 3) and setListener

  7.                 5) just start() again

  8.        

  9.         but i dont get it to work - repeatRotation works the first time, but after the first listener call, i cant reset the delay.

  10.         Tested it with a print - just a spam :(

  11. ]]--

  12. function repeatRotation(old)

  13.         -- my attempt to re-use 'old' - its a MOAIEaseDriver, thats call this function.

  14.         old.house.animProp:setRot(0) -- reset the rotation of the prop to 0 (currently: -360°)

  15.         old:clear() -- just a test, tryed everything to reset the driver :D

  16.         old:stop() -- just a test, too

  17.         old:reserveLinks(1)

  18.         old:setLink( 1, old.house.animProp, MOAIProp2D.ATTR_Z_ROT, -360, MOAIEasType.LINEAR)

  19.         old:setLength(3/old.windSpeed)

  20.         old:setListener( MOAIAction.EVENT_STOP, repeatRotation)

  21.         old:start()

  22. end

Phillipk
 
Posts: 10
Joined: Sun Mar 04, 2012 5:31 am

Re: Deleting Moai Props / Ease Drivers

Postby dana » Wed Mar 21, 2012 1:31 pm

Hi Phillip,

It looks like there might be some issues in .95 that appear to be resolved in 1.0. I just wrote a sample to demo reusing ease drivers, which I'll paste in below, and it worked great in the 1.0 build but when I tried it in .95 it failed to repeat at all. 1.0 is planned to launch on Friday, so I'd wait until that happens and then see how your ease drivers work.

The full code that I used is:

Code: Select all
  1. MOAISim.openWindow ( "test", 512, 512 )

  2.  

  3. viewport = MOAIViewport.new ()

  4. viewport:setSize ( 512, 512 )

  5. viewport:setScale ( 512, 512 )

  6.  

  7. layer = MOAILayer2D.new ()

  8. layer:setViewport ( viewport )

  9. MOAISim.pushRenderPass ( layer )

  10.  

  11. gfxQuad = MOAIGfxQuad2D.new ()

  12. gfxQuad:setTexture ( "cathead.png" )

  13.  

  14. prop = MOAIProp2D.new ()

  15. prop:setDeck ( gfxQuad )

  16. prop:setFrame ( -64, -64, 64, 64 )

  17. layer:insertProp ( prop )

  18.  

  19. local ease = MOAIEaseDriver.new ()

  20.  

  21. function initDriver ()

  22.         ease:reserveLinks ( 1 )

  23.         ease:setLink ( 1, prop, MOAIProp2D.ATTR_Z_ROT, -360, MOAIEaseType.LINEAR )

  24.         ease:setSpan ( 3 )

  25.         ease:setListener ( MOAIAction.EVENT_STOP, repeatRotation )

  26.         ease:start ()

  27. end

  28.  

  29. function repeatRotation ( )

  30.         ease:setSpan ( 1 )

  31.         ease:start ()

  32. end

  33.  

  34. thread = MOAIThread.new ()

  35. thread:run ( initDriver )



One thing you may notice is that in 1.0 you need to use the setSpan function instead of setLength.

Keep me updated on your progress!
User avatar
dana
Site Admin
 
Posts: 200
Joined: Fri Mar 02, 2012 6:10 pm
Location: Zipline Games


Return to New Users

Who is online

Users browsing this forum: No registered users and 0 guests

x