logic error for timers

For some reason the timer (Destroyer_Update) is not executing more that once. Am I declaring it wrong? How should I be declaring it?

[lua]


– Destroyer.lua


------------------------------------------------------------------file inclusion 

local Destroyer = require( “Destroyer” )

------------------------------------------------------------------file inclusion

------------------------------------------------------------------forward declerations

local Create_Destroyer

------------------------------------------------------------------forward declerations

------------------------------------------------------------------VARIABLES

local in_game = display.newGroup()

------------------------------------------------------------------VARIABLES

------------------------------------------------------------------START FUNCITON

local function Start()

    print(“The Game Has Started”)

    Create_Destroyer(0,0)

    in_game.x=display.contentWidth/2

    in_game.y=display.contentHeight/2

end

------------------------------------------------------------------START FUNCTION

------------------------------------------------------------------RUNTIME FUNCTIONS

local function Destroyer_Update(Destroyer)

    Destroyer:make_money(5)

    print(1)

end

------------------------------------------------------------------RUNTIME FUNCTIONS

------------------------------------------------------------------SINGLE FUNCTIONS

function Create_Destroyer(x,y)

    local Destroyer1 = Destroyer.new(x,y) – creates the destroyer class object

    timer.performWithDelay( 1, Destroyer_Update(Destroyer1),10) – gives it a runtime funciton

    in_game:insert( Destroyer1.object )

end

------------------------------------------------------------------SINGLE FUNCTIONS

------------------------------------------------------------------FUNCTION CALLS AND DECLERATIONS

Start() --Calls the initial “Start” function that initiates the program

------------------------------------------------------------------FUNCTION CALLS AND DECLERATIONS
[/lua]

You should change your line “timer.performWithDelay( 1, Destroyer_Update(Destroyer1),10)” to “timer.performWithDelay( 1, function() Destroyer_Update(Destroyer1) end,10)”.

This is a common issue for new developers in Lua.  timer.performWithDelay expects to receive a reference to a function.  However, Destroyer_Update(Destroyer1) is not a reference, it’s an actual call to a function.  A reference would be Destroyer_Update, by itself, with no parentheses.  However, that won’t work if you want to pass arguments to it.  The solution is to use what’s called a “closure”, which is to wrap your call within a function definition.

If you wanted to separate it onto two lines to more clearly see what’s going on, you could do it like this:

[lua]

local myClosure = function() Destroyer_Update(Destroyer1) end

timer.performWithDelay( 1, myClosure,10)

[/lua]

  • Andrew

Thanks that works but I have another question.

How would I implement a touch function that has a “Destroyer1” argument passed in? would I have to pass in the event argument first? What is the event argument doing exactly?

[lua]
------------------------------------------------------------------RUNTIME/TIMER FUNCTIONS

local function Destroyer_Update(Destroyer)

    Destroyer:make_money(Destroyer.income_rate)

end

local function Destroyer_Touch(event)

    if event.phase == “began” then

        print(“ouch”)

    end

end

------------------------------------------------------------------RUNTIME/TIMER FUNCTIONS

------------------------------------------------------------------SINGLE FUNCTIONS

function Create_Destroyer(x,y)

    local Destroyer1 = Destroyer.new(x,y) – creates the destroyer class object

    local temp_money_timer = function() Destroyer_Update(Destroyer1) end

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

    local temp_touch_function = function() Destroyer_Touch(Destroyer1) end

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

    in_game:insert( Destroyer1.object ) – adds the destroyed to the in_game group

end

------------------------------------------------------------------SINGLE FUNCTIONS
[/lua]

Hi there,

Generally what you would do is assign a custom property to your object, such as “id” or “name”, which you’ll be able to access and act accordingly in your touch listener.

In your touch listener, the event table is quite important – it’s what gives you information about the touch, like what phase it’s in.  Importantly, event.target points to the object that was touched, and if you’ve assigned a custom property to it called id, then event.target.id would provide you that id and let you act based on it.

  • Andrew

You should change your line “timer.performWithDelay( 1, Destroyer_Update(Destroyer1),10)” to “timer.performWithDelay( 1, function() Destroyer_Update(Destroyer1) end,10)”.

This is a common issue for new developers in Lua.  timer.performWithDelay expects to receive a reference to a function.  However, Destroyer_Update(Destroyer1) is not a reference, it’s an actual call to a function.  A reference would be Destroyer_Update, by itself, with no parentheses.  However, that won’t work if you want to pass arguments to it.  The solution is to use what’s called a “closure”, which is to wrap your call within a function definition.

If you wanted to separate it onto two lines to more clearly see what’s going on, you could do it like this:

[lua]

local myClosure = function() Destroyer_Update(Destroyer1) end

timer.performWithDelay( 1, myClosure,10)

[/lua]

  • Andrew

Thanks that works but I have another question.

How would I implement a touch function that has a “Destroyer1” argument passed in? would I have to pass in the event argument first? What is the event argument doing exactly?

[lua]
------------------------------------------------------------------RUNTIME/TIMER FUNCTIONS

local function Destroyer_Update(Destroyer)

    Destroyer:make_money(Destroyer.income_rate)

end

local function Destroyer_Touch(event)

    if event.phase == “began” then

        print(“ouch”)

    end

end

------------------------------------------------------------------RUNTIME/TIMER FUNCTIONS

------------------------------------------------------------------SINGLE FUNCTIONS

function Create_Destroyer(x,y)

    local Destroyer1 = Destroyer.new(x,y) – creates the destroyer class object

    local temp_money_timer = function() Destroyer_Update(Destroyer1) end

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

    local temp_touch_function = function() Destroyer_Touch(Destroyer1) end

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

    in_game:insert( Destroyer1.object ) – adds the destroyed to the in_game group

end

------------------------------------------------------------------SINGLE FUNCTIONS
[/lua]

Hi there,

Generally what you would do is assign a custom property to your object, such as “id” or “name”, which you’ll be able to access and act accordingly in your touch listener.

In your touch listener, the event table is quite important – it’s what gives you information about the touch, like what phase it’s in.  Importantly, event.target points to the object that was touched, and if you’ve assigned a custom property to it called id, then event.target.id would provide you that id and let you act based on it.

  • Andrew