pass by reference

So I have a class called destroyer.

I want  to increment the value of the destroyer.money by 5 every 500 frames, from inside the destroyer class script

to do this I need to pass in destroyer.money by reference otherwise i an just changing the value of a useless local variable.

QUESTION: how do you pass by reference in lua and corona sdk?

thanks in advance :wink:

my code:

[lua]

– Destroyer.lua


local Destroyer = {}

local Destroyer_mt = { __index = Destroyer }    – metatable


– PRIVATE FUNCTIONS


local function set_referance_point(object,x,y)

    object:setReferencePoint(display.CenterReferencePoint);

    object.x=x;

    object.y=y;

end


local function make_money(money, income_rate)

    money = money + income_rate

    print(money);

end


local function got_touched( event )

    print(“hi”)

    if(event.phase==“began”) then

    end

end


– PUBLIC FUNCTIONS


function Destroyer.new(x,y)    – constructor

        

    local newDestroyer = 

    {

        Health = 100,

        money = 1000,

        income_rate=5,

        object = display.newImage( “deathstar.png” )

    }

    set_referance_point(newDestroyer.object,x,y) – positions the Destroyer

    local temp_touch_function = function() got_touched() end

    newDestroyer.object:addEventListener( “touch”, temp_touch_function )

    local temp_money_timer = function() make_money(newDestroyer.money, newDestroyer.income_rate) end

    timer.performWithDelay( 500, temp_money_timer, -1) – gives Destroyer a runtime funciton

    return setmetatable( newDestroyer, Destroyer_mt )

end


function Destroyer:print_confirmation()

    print(  “Destroyer Has been created” )

end



return Destroyer
[/lua]

If you make make_money a method of the class, i.e:

Destroyer:make_money()

Then access member variables using the self property; i.e:

[lua]

Destroyer:make_money()

self.money = self.money + self.income_rate

print(self.money)

end

[/lua]

This code isn’t tested but the approach is one of encapsulation regarding OOP. I suggest looking up the topic on PiL; however this approach should point you in the right direction.

If you make make_money a method of the class, i.e:

Destroyer:make_money()

Then access member variables using the self property; i.e:

[lua]

Destroyer:make_money()

self.money = self.money + self.income_rate

print(self.money)

end

[/lua]

This code isn’t tested but the approach is one of encapsulation regarding OOP. I suggest looking up the topic on PiL; however this approach should point you in the right direction.