Game Development Best Practices?

Is there a best practices guide for Game Development with Corona?

I have found a best practice with how to set up the project and assets, and lots of how to do this effect tutorials but nothing really explaining how to set up the code in your project.

For example, I’m setting up a game with multiple scenes and multiple character that are shared in the scenes. Should I put the character code in the main.lua or each in their own file?

I’m also having problems with scenes, that I can go through them once and everything works, but when I go through them a 2nd time, variables are being lost, or the wrong character shows up. I’m sure this is all to do with issues with local vs global vs _G.variables.

I’m just looking for somewhere to reference for games that are more complicated then the simple tutorial game example and not sure where best to look.

Thanks, [import]uid: 226755 topic_id: 36961 reply_id: 336961[/import]

I’m not sure about any guides to best practices. I’ll let the community chime in on that one. For your specific example, I can tell you that having all your code in main.lua for a complex game is a very bad idea. It will become unsupportable and hard to work with. I made that mistake with my first game and I dread touching that code these days. But it takes time to learn how to use modules to break your code apart, but you really should do it. Now if you’re doing something rather simple, then sure, using main.lua for everything can be fine.

We have quite a few tutorials here: http://www.coronalabs.com/resources/tutorials/
Also check out TandG Apps. They have quite a bit too: http://www.tandgapps.co.uk/resources/coronalabs-resources/

Now to your storyboard problem. Bare with me, this may get tedious, but it was an AhHa moment for me because it wasn’t something I thought about when building storyboard apps but now is very forefront in my mind.

Storyboard scenes are Lua modules just like any other Lua module you might use. Usually for modules you do a:

local mymodule = require("mymodule")  

Storyboard does that for you as part of storyboard.gotoScene(). When Lua loads modules, it puts them into a table and if it’s not there, it executes the code in its main chunk once and only once. If you load that module again, that code is not re-executed. This is why you can load an object file, like your character.lua file or inventory.lua and have it work in multiple files.

In your storyboard scene if you initialize anything outside of the storyboard event functions that will happen only once ever unless you go through steps to completely remove and “unrequire” the scene (which I think storyboard.removeScene() is supposed to do for you). The storybaord.purgeScene() and storyboard.purgeAll() don’t do an “unrequire” and that module is still in memory and code outside of any called function will retain it’s last values.

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local myVar = 10  
  
for i = 1, 10 do  
 myVar = myVar \* i  
end  
  
function scene:createScene(event)  
 -- do stuff  
end  
...  

In this sample the local myVar = 10 and that for loop will only run the very first time that scene is visited. After that, the only way nuke the scene completely.

See this post on un-requiriing. I’ll query around if storyboard.removeScene() does this or not. [import]uid: 199310 topic_id: 36961 reply_id: 145234[/import]

I’m not sure about any guides to best practices. I’ll let the community chime in on that one. For your specific example, I can tell you that having all your code in main.lua for a complex game is a very bad idea. It will become unsupportable and hard to work with. I made that mistake with my first game and I dread touching that code these days. But it takes time to learn how to use modules to break your code apart, but you really should do it. Now if you’re doing something rather simple, then sure, using main.lua for everything can be fine.

We have quite a few tutorials here: http://www.coronalabs.com/resources/tutorials/
Also check out TandG Apps. They have quite a bit too: http://www.tandgapps.co.uk/resources/coronalabs-resources/

Now to your storyboard problem. Bare with me, this may get tedious, but it was an AhHa moment for me because it wasn’t something I thought about when building storyboard apps but now is very forefront in my mind.

Storyboard scenes are Lua modules just like any other Lua module you might use. Usually for modules you do a:

local mymodule = require("mymodule")  

Storyboard does that for you as part of storyboard.gotoScene(). When Lua loads modules, it puts them into a table and if it’s not there, it executes the code in its main chunk once and only once. If you load that module again, that code is not re-executed. This is why you can load an object file, like your character.lua file or inventory.lua and have it work in multiple files.

In your storyboard scene if you initialize anything outside of the storyboard event functions that will happen only once ever unless you go through steps to completely remove and “unrequire” the scene (which I think storyboard.removeScene() is supposed to do for you). The storybaord.purgeScene() and storyboard.purgeAll() don’t do an “unrequire” and that module is still in memory and code outside of any called function will retain it’s last values.

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local myVar = 10  
  
for i = 1, 10 do  
 myVar = myVar \* i  
end  
  
function scene:createScene(event)  
 -- do stuff  
end  
...  

In this sample the local myVar = 10 and that for loop will only run the very first time that scene is visited. After that, the only way nuke the scene completely.

See this post on un-requiriing. I’ll query around if storyboard.removeScene() does this or not. [import]uid: 199310 topic_id: 36961 reply_id: 145234[/import]

I’m not sure about any guides to best practices. I’ll let the community chime in on that one. For your specific example, I can tell you that having all your code in main.lua for a complex game is a very bad idea. It will become unsupportable and hard to work with. I made that mistake with my first game and I dread touching that code these days. But it takes time to learn how to use modules to break your code apart, but you really should do it. Now if you’re doing something rather simple, then sure, using main.lua for everything can be fine.

We have quite a few tutorials here: http://www.coronalabs.com/resources/tutorials/
Also check out TandG Apps. They have quite a bit too: http://www.tandgapps.co.uk/resources/coronalabs-resources/

Now to your storyboard problem. Bare with me, this may get tedious, but it was an AhHa moment for me because it wasn’t something I thought about when building storyboard apps but now is very forefront in my mind.

Storyboard scenes are Lua modules just like any other Lua module you might use. Usually for modules you do a:

local mymodule = require("mymodule")  

Storyboard does that for you as part of storyboard.gotoScene(). When Lua loads modules, it puts them into a table and if it’s not there, it executes the code in its main chunk once and only once. If you load that module again, that code is not re-executed. This is why you can load an object file, like your character.lua file or inventory.lua and have it work in multiple files.

In your storyboard scene if you initialize anything outside of the storyboard event functions that will happen only once ever unless you go through steps to completely remove and “unrequire” the scene (which I think storyboard.removeScene() is supposed to do for you). The storybaord.purgeScene() and storyboard.purgeAll() don’t do an “unrequire” and that module is still in memory and code outside of any called function will retain it’s last values.

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local myVar = 10  
  
for i = 1, 10 do  
 myVar = myVar \* i  
end  
  
function scene:createScene(event)  
 -- do stuff  
end  
...  

In this sample the local myVar = 10 and that for loop will only run the very first time that scene is visited. After that, the only way nuke the scene completely.

See this post on un-requiriing. I’ll query around if storyboard.removeScene() does this or not. [import]uid: 199310 topic_id: 36961 reply_id: 145234[/import]

I’m not sure about any guides to best practices. I’ll let the community chime in on that one. For your specific example, I can tell you that having all your code in main.lua for a complex game is a very bad idea. It will become unsupportable and hard to work with. I made that mistake with my first game and I dread touching that code these days. But it takes time to learn how to use modules to break your code apart, but you really should do it. Now if you’re doing something rather simple, then sure, using main.lua for everything can be fine.

We have quite a few tutorials here: http://www.coronalabs.com/resources/tutorials/
Also check out TandG Apps. They have quite a bit too: http://www.tandgapps.co.uk/resources/coronalabs-resources/

Now to your storyboard problem. Bare with me, this may get tedious, but it was an AhHa moment for me because it wasn’t something I thought about when building storyboard apps but now is very forefront in my mind.

Storyboard scenes are Lua modules just like any other Lua module you might use. Usually for modules you do a:

local mymodule = require("mymodule")  

Storyboard does that for you as part of storyboard.gotoScene(). When Lua loads modules, it puts them into a table and if it’s not there, it executes the code in its main chunk once and only once. If you load that module again, that code is not re-executed. This is why you can load an object file, like your character.lua file or inventory.lua and have it work in multiple files.

In your storyboard scene if you initialize anything outside of the storyboard event functions that will happen only once ever unless you go through steps to completely remove and “unrequire” the scene (which I think storyboard.removeScene() is supposed to do for you). The storybaord.purgeScene() and storyboard.purgeAll() don’t do an “unrequire” and that module is still in memory and code outside of any called function will retain it’s last values.

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
local myVar = 10  
  
for i = 1, 10 do  
 myVar = myVar \* i  
end  
  
function scene:createScene(event)  
 -- do stuff  
end  
...  

In this sample the local myVar = 10 and that for loop will only run the very first time that scene is visited. After that, the only way nuke the scene completely.

See this post on un-requiriing. I’ll query around if storyboard.removeScene() does this or not. [import]uid: 199310 topic_id: 36961 reply_id: 145234[/import]