Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

New here with some questions on game loops and listeners
Started by tom03 Feb 14 2019 01:37 AM

6 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

tom03

[GLOBAL: userInfoPane.html]
tom03
  • Observer

  • 7 posts
  • Corona SDK

Hi all,

 

This is my first post, Im new here. 

 

I've been making games for iOS with the html5 game engine Impact JS and deploying to the app store with Ejecta JS.  This has been fine up to a point but have decided it's time to move on so have committed to transitioning over to Corona. 

 

I dabbled in Love 2d  for a bit so am semi comfortable with Lua but am a little confused on how Corona handles its update loop? In Impact JS every entity has an update and draw function that can be overridden, how do I handle objects/entities that need to be updated per frame? Is that with event listeners? and where is the best place for these in my code - anywhere? all in my game.lua ?

 

 

I'm sure I'll have a tonne of questions over the next few weeks as I get my head around this new engine.

 

Many thanks,

tom



[TOPIC: post.html]
#2

Alan PlantPot

[GLOBAL: userInfoPane.html]
Alan PlantPot
  • Contributor

  • 907 posts
  • Corona SDK

As the saying goes "There are many ways to skin a cat". One way to answer your question would be to use a single Runtime event listener and handle all objects in there:

local function update()
    for i  = 1, #yourObjects do
        yourObjects[i].x = yourObjects[i].x - 1
    end
end

Runtime:addEventListener("enterFrame", update)

Obviously which objects this function knows about will depend on scope. 

 

 

 

where is the best place for these in my code - anywhere? all in my game.lua ?

 

That's a harder question to answer, because it will depend entirely on how your project is set up. If you have one game lua file and one menu file, then you wouldn't want to update the items created in menu.lua from within game.lua. 

 

As a general rule, I try to only update objects in the file that created them (e.g. all objects created in game.lua are updated there too).



[TOPIC: post.html]
#3

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 950 posts
  • Corona SDK

Good question!

 

Corona starts without any game loop at all. You add your own gameLoops by adding functions/methods that are called every frame. In theory that means that these functions are called 30 or 60 times per second, depending on your settings. In practice it could be that your game code is too heavy for the processor or graphics card, and that will make the app run slower than the proposed 30 or 60 frames per second, but let's forget about that for now.

 

So, you start without any gameLoop at all, and can add your own as follows, from anywhere in your code:

 

Runtime:addEventListener("enterFrame", theFunctionIwantToCall)

 

What I try to do is structure my code logically, and have each object run its own gameLoop containing code that makes sense for this object. Bullets will have their own code that tells the bullet to move a bit each frame, a timer bar will have its own code that subtracts a certain amount of time each frame etc...

 

I would advise against a single event listener for all objects, as this will become unmanageable at a certain point, unless your games are really really simple.

 

p.s. If you are starting out: it's not the easiest topic to grasp, but true power in Corona comes when you learn to structure your code well in different modules.



[TOPIC: post.html]
#4

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 950 posts
  • Corona SDK

For what it's worth, regarding best place in code, all my objects are structured as follows:

 

1 - creation of all object variables

2 - creation of display groups for the object

3 - creation of display objects for the, uhm, objects

4 - insertion of display objects in the right display groups and order

5 - creation of the different functions to be used by the object that are not event listeners

6 - creation of the functions that are event listeners or added to timers

7 - calling of specific functions

8 - adding the different event Listeners

9 - cleanup code, for removal of all the above.



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,544 posts
  • Corona SDK

@Tom,

 

Corona does not have a game loop in the way Love 2D does.  Game actions are all event driven.

 

While you can (and will) set up timers and enterFrame listeners for looping purposes, you don't need to have a actual game loop that redraws the scene over and over like in Love 2D.

 

PS - I'm not saying there isn't a game loop, but rather that Corona handles it for you and all you need to do is set up event listeners for interactions, and various bits of control code for game logic.



[TOPIC: post.html]
#6

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 950 posts
  • Corona SDK

Roaminggamer is right. I've done games like Candy Crush where I didn't have a single gameLoop running. Everything just "waited" until a touch event happened, and then some transitions, animation and code ran. And then the game waited for new touch input.

 

That being said, I now mainly make platform/action games, and for those I have gameLoop all over the place!



[TOPIC: post.html]
#7

tom03

[GLOBAL: userInfoPane.html]
tom03
  • Observer

  • 7 posts
  • Corona SDK

Thanks  for the answers folks.

 

I've been digging into the engine now for almost a week and have to say really enjoying it so far. 

 

@roaminggamer the event driven side of things, whilst it stumped me a little at first, it does seem a lot more of an efficient way of doing things and your explanation made a lot of sense to me, cheers.




[topic_controls]
[/topic_controls]