Game Logic File

Hi everyone,

I’ve heard when creating each level inside a different file it is more memory-smart to put everything that stays the same into one logic file and load it inside each level file. What type of things should be inside this file, functions, sprites etc?

Also, how do I load this in the different files if all my variables are local?
I have tried require “logic” - my file name containing all my functions, however I’m not sure how I get the functions written inside to ‘activate’ because currently they don’t.

If you need me to post any code I’m happy to do so.
Thanks for reading! [import]uid: 90223 topic_id: 16906 reply_id: 316906[/import]

Just as an update, I’ve decided to put all my functions and sprites in tables.

However I’m not sure how I can reference this table in my other files.

This is what I have tried:

-- Logic File  
local functionTable = {}  
  
functionTable[1] = onTouch, -- Function Names  
functionTable[2] = physicsStart,  
functionTable[3] = onComplete,  
functionTable[4] = winCondition  
  
-- Level File  
local logic = require "logic"  
  
logic.functionTable[1]  
logic.functionTable[2]  
logic.functionTable[3]  
logic.functionTable[4]  

Unfortunately this causes errors and my objects don’t display.
If you know of anyway to reference these functions please let me know! [import]uid: 90223 topic_id: 16906 reply_id: 63541[/import]

Try :

[code]
– Logic File

functionTable = {} --Dont make it local

functionTable[1] = onTouch, – Function Names
functionTable[2] = physicsStart,
functionTable[3] = onComplete,
functionTable[4] = winCondition

– Level File
local logic = require “logic”

logic.functionTable[1]
logic.functionTable[2]
logic.functionTable[3]
logic.functionTable[4] [import]uid: 84637 topic_id: 16906 reply_id: 63598[/import]

Unfortunately I get the same error, here’s how it looks in the terminal:

Runtime error error loading module 'level02' from file 'C:\Users\Samuel\Desktop\Transp arent\level02.lua': C:\Users\Samuel\Desktop\Transparent\level02.lua:80: '=' expected near 'l ogic' stack traceback: [C]: ? [C]: ? [C]: in function 'require' C:\User

logic.functionTable[1] logic.functionTable[2] -- This is line 80 logic.functionTable[3] logic.functionTable[4] [import]uid: 90223 topic_id: 16906 reply_id: 63611[/import]

Hey Samuel.

Could you post up some code from the logic file so i can see more what your trying to do ?

Thanks [import]uid: 84637 topic_id: 16906 reply_id: 63628[/import]

Sure, here you go :slight_smile:

[code]module(…, package.seeall)

function new()
local localGroup = display.newGroup()


– Function –

local function onTouch( event )
local t = event.target

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end

local function physicsStart (event)
if “began” == event.phase then
physics.start()
transparentball:removeEventListener( “touch”, onTouch )
end
end

– Handler that gets notified when the alert closes
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Go to next level
director:changeScene(“level02”)
elseif 2 == i then
– Quit level if “Quit” (the 2nd button) was clicked
director:changeScene(“menu”)
end
end
end

local function winCondition(event)
– if transparentball.currentFrame == 3 then
if event.other.myName == “portal” then
physics.pause()
– Show alert with two buttons
local alert = native.showAlert( “Well Done!”, “Continue to the next level!”, { “Next Level”, “Quit” }, onComplete )
end
– end
end

transparentball:addEventListener(“collision”, winCondition)
startbutton:addEventListener(“touch”, physicsStart)

transparentball.myName = “transparentball”
yellow.myName = “yellow”
portal.myName = “portal”

localGroup:insert(background1)
localGroup:insert(instruction)
localGroup:insert(ground)
localGroup:insert(transparentball)
localGroup:insert(portal)
localGroup:insert(orange)
localGroup:insert(yellow)
localGroup:insert(btn1)
localGroup:insert(startbutton)

myNameTable {}

myNameTable[1] = transparentball.myName = “transparentball”
myNameTable[2] = yellow.myName = “yellow”
myNameTable[3] = portal.myName = “portal”

functionTable = {}

functionTable[1] = onTouch,
functionTable[2] = physicsStart,
functionTable[3] = onComplete,
functionTable[4] = winCondition

localGroupTable = {}

localGroupTable[1] = localGroup:insert(background1)
localGroupTable[2] = localGroup:insert(instruction)
localGroupTable[3] = localGroup:insert(ground)
localGroupTable[4] = localGroup:insert(transparentball)
localGroupTable[5] = localGroup:insert(portal)
localGroupTable[6] = localGroup:insert(orange)
localGroupTable[7] = localGroup:insert(yellow)
localGroupTable[8] = localGroup:insert(btn1)
localGroupTable[9] = localGroup:insert(startbutton)[/code]

I know I need to create my functions before I include them in the tables however is the same true for my localGroupTable and myNameTable? [import]uid: 90223 topic_id: 16906 reply_id: 63758[/import]