Search code examples
coronasdkcorona-storyboard

Corona SDK - Managing Game State/Objects/Inventory System/Sprite Animation


I am attempting to determine the best way and most efficient way to handle the following tasks in a game written with the Corona SDK. It seems like there are so many ways to do things that it becomes quite confusing, so I am hoping someone here can help!

I am creating an adventure type game that will have an inventory system/puzzles etc. The thought process I've developed so far involves using separate "classes" to handle each specific aspect of the game. Such as InventoryManagement.lua, ObjectManagement.lua, PuzzleManagement.lua etc.

Just a side note - this game really doesn't involve Physics, but I would like to have static images with animations that occurs (Think opening a door or picking up an object):

Here is an example of what I am trying to accomplish:

  • Say you start a new game and it loads the first scene. I need to setup the player's inventory, the objects in the room, their state, images for these things etc. which I assume could be defaulted on first game load, and loaded subsequently ...
  • Then the player clicks on a key to pick it up - at this point the key needs to appear in their inventory so now it will be removed from the scene, added to their inventory (Via InventoryManagement?), and the scene will be updated (Via SceneManagement?)...
  • From now on the key should no longer show in the scene.
  • Now say they click the key and use it on a door, the door should animate open and remain open from now on.
  • If the player leaves the room and comes back, the key should not appear.

Now to me it makes sense to load/unload the scene every time you enter/leave the scene, but ... won't this become memory intensive etc. if you do it this way? ... Is there a better way to handle a scene if it has say, 30 objects on screen?

Hopefully that is clear - It is hard to find specific information related to each one of these elements. Everything seems to be related to physics games and I can't seem to find something on how to "Add a key to the scene if, but not if, and if it's been used then animate that door" :(

Thanks!


Solution

  • You need to make some stuff in Global space (using _G) to use them between scenes.

    To change between scenes, "Storyboard" is very nice.

    But mind you, you will need to use some manual memory hacking if you make things too much memory intensive, mainly, you might need to before loading a scene, do several calls of "collectgarbage" after cleaning the last scene, otherwise you will hit a curious problem:

    You loaded scene "A".

    You switch to scene "B".

    The game 'unloads' "A", but the garbage collector don't, then loads "B", the end result is memory use of "A" + "B".

    On this adventure game I was making in Corona, it resulted in several crazy crashed until I figured the "collectgarbage" trick.