Hey,
I have one problem. I realized that lua passing argument function by value not by reference ( what’s gives me opportunity to change original object ).
For example:
local mySimpleFunction = function( obj) print("Inside function: " .. obj) obj = obj + 1 print("Inside function: " .. obj) end local someObject = 1 print( "Before = " .. someObject) -- gives me 1 mySimpleFunction( someObject) -- 1 and 2 print( "After = " .. someObject) -- again gives me 1
But what if I have some kind of object which repeats a lot in my game e.g. monsters
local monster1 = display.newImage( "monster.jpg", 100, 400 ) local monster2 = display.newImage( "monster.jpg", 200, 400 ) local monster3 = display.newImage( "monster.jpg", 300, 400 ) etc...... physics.addBody( monster1, "dyamic", { density=10.0, friction=1, bounce = 0} ) physics.addBody( monster2, "dyamic", { density=10.0, friction=1, bounce = 0} ) physics.addBody( monster3, "dyamic", { density=10.0, friction=1, bounce = 0} ) etc.....
And I want to make them to start moving. Some monster will be walking to the left, other to the right direction. With different velocity etc.
I don’t want to make new function for that to each monster (DRY - don’t repeat yourself). I want to write some general function
local moveMonster = function( monster, direction, velocity) ...... end
and use it like this
local timerMonster1 = timer.performWithDelay( 500, function() moveMonster( monster1, left, 60); end, 0) local timerMonster2 = timer.performWithDelay( 250, function() moveMonster( monster2, right, 10); end, 0) local timerMonster3 = timer.performWithDelay( 1000, function() moveMonster( monster3, right, 30); end, 0)
Other general function can be ‘killMonster’ where I’ll use ‘removeSelf()’ for this monster.
I need to do all this stuff to orginal object not a copy.
Do you have any solution to solve this?