help with timer.performWithDelay

Hi, everybody

Hope I’m not being too annoying…
As I said in my previous topic I am a beginner trying to make a virtual pet-like app.
I have a function for everything that happens in-game
function getHungry() is supposed to make pet hungrier, something like this:
function inGame()
(…)
local function getHungry()
hunger = hunger + 1
end
timer.performWithDelay( 1000, getHungry, 0 )

getHungry()
GOTHungry()

end

later I have a function to show the changes of the pet if it gets hungry(for now it turns black):

function GOTHungry()
if hunger>10 then
pet:setFillColor(0,0,0,255)
end
end

I just can’t get the timer to work… What am I doing wrong?

Thanks!
[import]uid: 161390 topic_id: 29097 reply_id: 329097[/import]

Is the GOTHingry ever called somewhere in your code? [import]uid: 160496 topic_id: 29097 reply_id: 117044[/import]

timer.performWithDelay( 1000, getHungry, 0 )

You called the function ‘getHungry’ to run 1 time per second ‘zero times’.

try this:

timer.performWithDelay( 1000, getHungry, -1 )
-will run it infinitely or
timer.performWithDelay( 1000, getHungry, 3 )
-will run it every second 3x.

[import]uid: 75779 topic_id: 29097 reply_id: 117047[/import]

its called from inside the inGame function and it works like it should, I guess…
For example, if I change the getHungry() function to hunger = hunger + 11, the GOTHungry function works properly and instantly - the pet turns black, I just can’t make it work with the delay and looped. [import]uid: 161390 topic_id: 29097 reply_id: 117049[/import]

Lets say that perhaps its counting but GOTHungry is never called after the counting reaches 10, like I think mike is inferring. Why not check it’s hunger level in the getHungry function itself? Something like this:

local function getHungry() hunger = hunger + 1 if hunger \> 10 then pet:setFillColor(0,0,0,255) end end timer.performWithDelay( 1000, getHungry, 0 ) [import]uid: 56820 topic_id: 29097 reply_id: 117050[/import]

Have you tried putting a print statement in your getHungry() function to see how often it’s being called? [import]uid: 56820 topic_id: 29097 reply_id: 117051[/import]

rmckee282002 I’ve tried this before - and tried it now, unfortunately it didn’t help :frowning:
Well at least now I have it set to “-1” :slight_smile: thanks [import]uid: 161390 topic_id: 29097 reply_id: 117053[/import]

/deleted/ [import]uid: 161390 topic_id: 29097 reply_id: 117052[/import]

anderoth

local function getHungry()
hunger = hunger + 1
if hunger > 10 then
pet:setFillColor(0,0,0,255)
end
end
timer.performWithDelay( 1000, getHungry, 0 )

It worked!!! Thank you and everybody else very much! (Mike - I didn’t get the question at first I guess :)) I really don’t understand why I needed both of the functions. Don’t understand why it didn’t work anyway, but now it doesn’t matter :slight_smile:

Thanks again! [import]uid: 161390 topic_id: 29097 reply_id: 117054[/import]

It probably didn’t work because you called GOTHungry once. You have to keep calling it all the time in order for it to “catch” the hunger>10 condition. [import]uid: 160496 topic_id: 29097 reply_id: 117057[/import]

Something like:

while hunger>10 do
GOTHungry()
end

? [import]uid: 161390 topic_id: 29097 reply_id: 117060[/import]

No :slight_smile: You have to understand how Corona SDK works. You can’t have loops like that, because they will stop everything that is happening in your app. The code that you have executes (and has to return) in between screen frame updates that happen, hopefully, every 1/30th or 1/60th of a second. If you have a loop like that, it will never return, the screen will never update, and the whole app will come to a screeching halt. [import]uid: 160496 topic_id: 29097 reply_id: 117062[/import]

thanks! so much to learn! :slight_smile: back to the tutorials I go :slight_smile: [import]uid: 161390 topic_id: 29097 reply_id: 117064[/import]

Mike: By the way, any idea on where to read something about this? :slight_smile: [import]uid: 161390 topic_id: 29097 reply_id: 117065[/import]

grigoriy: try http://www.learningcorona.com/ - that’s a lot of material, start from basics.

One thing to understand right away though - Corona coding is not “linear”. The program doesn’t start at the top, then executes to the bottom and stops. You set things up the right way, then your various functions wait to be notified by various methods (timer, event listeners, transition onCompletes etc) that something happened, execute some code in response to the notification, then return and the wait continues. A lot of things (sprites animating, things moving during transitions, physics actions) happen by themselves during those “wait” times, without your code having to perform those things. Sometimes you have to do special animations in the space inbetween the video “frames” (that happen 30 times a second) - look at the Runtime “enterFrame” event listener documentation.

The rest - look at the various tutorials, look at sample code and learn :slight_smile: It is a great SDK and all kinds of things that are non-obvious can be done with it. [import]uid: 160496 topic_id: 29097 reply_id: 117072[/import]

thanks once again! sounds pretty easy and comfortable, but still a lot to learn and understand :wink: [import]uid: 161390 topic_id: 29097 reply_id: 117075[/import]