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)
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:
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.
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.
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’.
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.
@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.
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:
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.
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.