Improve use of composer for different game modes

Hello everyone!

In my game I have different game modes.

Every game mode is a different lua file with composer scena.

The files have the following structure:

--modules that I use for all modes local allModule\_1 = require("allModule\_1") local allModule\_2 = require("allModule\_2") local allModule\_3 = require("allModule\_3") --modules that I use only for this mode local thisModModule\_1 = require("thisModModule\_1") local thisModModule\_2 = require("thisModModule\_2") local thisModModule\_3 = require("thisModModule\_3") --variables that I use for all modes local allVar\_1 local allVar\_2 local allVar\_3 --variables that I use only for this mode local thisModVar\_1 local thisModVar\_2 local thisModVar\_3 --functions with the same code for all the mods that use the same variables (es. thisModVar\_1) for all the mods. --However, these variables (es. thisModVar\_1) can be modified by personal methones of a mod (es. "enterFrame") local function gameOver() end local function pause() end local function resume() endimprove use of composer for different game modes --Different functions that change according to the modality and modify both variables (es. allVar\_1 and thisModVar\_1) local function enterFrame(event) end local function other1( group ) end local function other2() end --Naturalmete created different things according to the modality function scene:create( event ) end function scene:show( event ) end function scene:hide( event ) end function scene:destroy( event ) end

Therefore in essence for each mode I copy some identical functions and some equal variables. The problem of course and that if I have to make a change I have to do it all.

Is there a way to optimize this?

Instead of having level1.lua , level2.lua try and change it to only have level.lua and then have your level data stored in JSON files called level1.json , level2.json.

Then for each level you simply load the relevant JSON file to configure your level.

Thanks two help in one day!

However what you said I do already. I’ll give you an example.

Mode 1 make goals (I’ll load the levels apart  level1.lua  , level2.lua…)

Mode 2 make basket(I’ll load the levels apart  level1.lua  , level2.lua…)

Mode 3 …

The fact is that for example the gameOver function in all the scenes collects the score and then shows it on video.

The same code for all modes…

I explained myself better?

Still not entirely sure what you mean…  But if you are having a gameOver() function in each module then I would put all those in a common module and then require that in your other modules.

Then when the player is dead (or whatever) simply call common.gameOver().  This function should create a group to display your game over UI.  Because this function is not tied to a single lua file it will display on top of whatever scene it is called from.

I apologize if I do not explain how I should. Probably because I’m not a native speaker…

However, it seems to me that we are almost there. Your solution is fine and that’s what I do in other parts of the game.

But in this case to gameOver I should pass the variables that I have defined (“variables that I use for all modes”).

So I wondered if there was a way to make a composer scene almost like a class.

So I can have some variable and functions that are the same for all mode and then, according to the mode, I can change/add only what I need.

Yeah I would pass what you need to the function, so do something like common.gameOver(score).

If you mean can you have a base scene and then inherit from that scene and add extra functionality then that is not possible (AFAIK).

Well then I’ll do it, I hoped there are other ways.

Thanks again for your time!!

Instead of having level1.lua , level2.lua try and change it to only have level.lua and then have your level data stored in JSON files called level1.json , level2.json.

Then for each level you simply load the relevant JSON file to configure your level.

Thanks two help in one day!

However what you said I do already. I’ll give you an example.

Mode 1 make goals (I’ll load the levels apart  level1.lua  , level2.lua…)

Mode 2 make basket(I’ll load the levels apart  level1.lua  , level2.lua…)

Mode 3 …

The fact is that for example the gameOver function in all the scenes collects the score and then shows it on video.

The same code for all modes…

I explained myself better?

Still not entirely sure what you mean…  But if you are having a gameOver() function in each module then I would put all those in a common module and then require that in your other modules.

Then when the player is dead (or whatever) simply call common.gameOver().  This function should create a group to display your game over UI.  Because this function is not tied to a single lua file it will display on top of whatever scene it is called from.

I apologize if I do not explain how I should. Probably because I’m not a native speaker…

However, it seems to me that we are almost there. Your solution is fine and that’s what I do in other parts of the game.

But in this case to gameOver I should pass the variables that I have defined (“variables that I use for all modes”).

So I wondered if there was a way to make a composer scene almost like a class.

So I can have some variable and functions that are the same for all mode and then, according to the mode, I can change/add only what I need.

Yeah I would pass what you need to the function, so do something like common.gameOver(score).

If you mean can you have a base scene and then inherit from that scene and add extra functionality then that is not possible (AFAIK).

Well then I’ll do it, I hoped there are other ways.

Thanks again for your time!!