Changing variables in loops

Hi,

I am currently making a game on corona using lua. I am trying to change a variable in an infinite loop. The problem that I have is that as it is in an infinite loop, the variable is being changed constantly instead of just once.

For example;

function startGameLoop()

if startGame == 0 then

coins = 0

end

if died == 1 then

coins = coins + score (I would like this to only change once when the died variable is changed to 1)

end

Thank you so much if anyone replies.

Have a great rest of your day!

Corona is not a game loop driven environment (like LOVE).  It is an event based environment.

Here is some code that uses a variety of events:

Get the project here: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/02/events.zip

local function touch( self, event ) if( event.phase == "ended") then Runtime:dispatchEvent( { name = "onScore", value = self.value } ) display.remove(self) end end for i = 1, 5 do local obj = display.newImageRect( "balloon" .. i .. ".png", 295/2, 482/2 ) obj.x = display.contentCenterX - display.actualContentWidth/2 + (i-1) \* 200 + 75 obj.y = display.contentCenterY obj.value = i \* 10 obj.touch = touch obj:addEventListener( "touch" ) end local scoreLabel = display.newText( 0, 100, 50 ) function scoreLabel.onScore( self , event ) self.text = tonumber(self.text) + event.value end Runtime:addEventListener("onScore",scoreLabel)

https://www.youtube.com/watch?v=NGGmvBew2AI&feature=youtu.be

FYI, be aware I have answered a ton of questions here and made many examples for other things (ex: now stopped Corona Geek hangouts, among others).  

You can find my source code for all of these things here:

PS - Follow my YouTube channel for regular videos associated with answers to questions here too: https://www.youtube.com/user/roaminggamer

Note: If you’re a die-hard gotta use game loops thinker (bad way to start, but no judgement)
 
You can do this:

local coins = 0 local score = 0 local startedGame = false local function gameLoop() if startedGame then coins = 0 end if died == 1 then coins = coins + score end end Runtime:addEventListener("enterFrame", gameLoop)

PS - Welcome to the community. Please format code posts in the future to help us help you:

formatyourcode.jpg
 

Sorry, but I have to disagree… everything in Corona from timers to transitions is game loop-based (or at the very least frame-based).

Most large games will have some concept of a game loop to control the many variables, screen updates, logic updates and state changes.

Saying that, I would say most games would benefit from a hybrid approach… being both event driven (user taps an object) to game loop based (based on users tap now adjust state on chained entities).

The trick is (and this only comes with experience) is to know what should be event driven v loop-based.

@SGS,
 
Surely you understand what I’m saying.
 
The typical game in Corona is not written like this:

local function myGameLoop() -- everything the game does is handled here end

Of course Corona has a ‘game loop’, but that is not what I’m saying.   Also, that game loop is mostly hidden from you and corona is primarily event driven.  i.e. Even enterFrame() the single way to access the game loop is and event.

This new user seems to be approaching game design in Corona as requiring an actual game loop function which is how you do it in LOVE.

I am trying to dissuade him from starting the Corona learning journey on the wrong premise.

For sure, just didn’t want others reading this post and thinking Corona has no “game loop”. 

For real simple games (mostly physics-based), most likely no game loops are required.

As you point out most other engines require a loop-based approach - particularly Unity.

Hey, I can completely understand what you mean but I still don’t understand how I can write this code? I basically want it so that when the player died, the score is added to the amount of coins they have, but the current problem is that when the died variable = 1, it is constantly adding the score to the coins variable. How else would I write this?

Thank you so much for your help!

If you are wondering why I am wanting to know how to do this it is because I am currently doing a lua assignment at college and need to make a game. I can email you my main.lua file if you wish. Thank you again!

Why don’t you just do this at the point died is set to 1?

If you must do it within your gameLoop, have another flag ‘handledDeath’ that you set after the first run through after died = 1, and check that both died = 1 and handledDeath = 0.

I would personally use true/false (Boolean) rather than 1/0, as this allows easier to read code such as ‘if died then’ or ‘if playerIsReady then’.

Helping people do their college work is getting dull real fast…

It does seem they teach people the code needed to do X in isolation, but not how to think like a programmer. Which is surely the biggest thing you can take away from a one-off development module, as the syntax is quickly going to be forgotten if not used regularly.

@decoder,

  1. Please do not hijack threads.

  2. See your other post for answers about scope and visibility as well as declaration order…

Ok bro…I didn’t know where to post…This won’t happen again…!

@decoder143, if you can’t find an appropriate forum post that answers your question, you should search for a forum post that is asking the same question if you need some additional clarification. If you can’t find an appropriate thread then start a new one!

When you post a different topic on an thread that isn’t what you’re discussing it does two things. First people see the headline/subject and its about topic X they won’t have a clue that topic Y is being discussed.  The second issues is people who come looking for Topic X to learn more about that problem find a thread talking about topic Y get frustrated because this isn’t the information isn’t what you’re looking for.

Rob

Corona is not a game loop driven environment (like LOVE).  It is an event based environment.

Here is some code that uses a variety of events:

Get the project here: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/02/events.zip

local function touch( self, event ) if( event.phase == "ended") then Runtime:dispatchEvent( { name = "onScore", value = self.value } ) display.remove(self) end end for i = 1, 5 do local obj = display.newImageRect( "balloon" .. i .. ".png", 295/2, 482/2 ) obj.x = display.contentCenterX - display.actualContentWidth/2 + (i-1) \* 200 + 75 obj.y = display.contentCenterY obj.value = i \* 10 obj.touch = touch obj:addEventListener( "touch" ) end local scoreLabel = display.newText( 0, 100, 50 ) function scoreLabel.onScore( self , event ) self.text = tonumber(self.text) + event.value end Runtime:addEventListener("onScore",scoreLabel)

https://www.youtube.com/watch?v=NGGmvBew2AI&feature=youtu.be

FYI, be aware I have answered a ton of questions here and made many examples for other things (ex: now stopped Corona Geek hangouts, among others).  

You can find my source code for all of these things here:

PS - Follow my YouTube channel for regular videos associated with answers to questions here too: https://www.youtube.com/user/roaminggamer

Note: If you’re a die-hard gotta use game loops thinker (bad way to start, but no judgement)
 
You can do this:

local coins = 0 local score = 0 local startedGame = false local function gameLoop() if startedGame then coins = 0 end if died == 1 then coins = coins + score end end Runtime:addEventListener("enterFrame", gameLoop)

PS - Welcome to the community. Please format code posts in the future to help us help you:

formatyourcode.jpg
 

Sorry, but I have to disagree… everything in Corona from timers to transitions is game loop-based (or at the very least frame-based).

Most large games will have some concept of a game loop to control the many variables, screen updates, logic updates and state changes.

Saying that, I would say most games would benefit from a hybrid approach… being both event driven (user taps an object) to game loop based (based on users tap now adjust state on chained entities).

The trick is (and this only comes with experience) is to know what should be event driven v loop-based.

@SGS,
 
Surely you understand what I’m saying.
 
The typical game in Corona is not written like this:

local function myGameLoop() -- everything the game does is handled here end

Of course Corona has a ‘game loop’, but that is not what I’m saying.   Also, that game loop is mostly hidden from you and corona is primarily event driven.  i.e. Even enterFrame() the single way to access the game loop is and event.

This new user seems to be approaching game design in Corona as requiring an actual game loop function which is how you do it in LOVE.

I am trying to dissuade him from starting the Corona learning journey on the wrong premise.

For sure, just didn’t want others reading this post and thinking Corona has no “game loop”. 

For real simple games (mostly physics-based), most likely no game loops are required.

As you point out most other engines require a loop-based approach - particularly Unity.