4 global variables with director class - memory

I’ve got 4 global variables in my game. 2 for x/y force of acceleration, 1 for sound and 1 for score. Does this drain my memory? Because there is no other way to do what I’m trying to do without using these global variables… And this morning the game was nice, but after I added the forth global variable, the score one, it feels like it’s lagging way more, almost unplayable at times… Is this normal and is really only 4 global variables supposed to cause this much damage to memory?

thanks in advance, [import]uid: 30185 topic_id: 6236 reply_id: 306236[/import]

Please take a look at this topic:

http://developer.anscamobile.com/forum/2011/02/02/how-can-i-easily-implement-save-and-load-system-0 [import]uid: 8556 topic_id: 6236 reply_id: 21428[/import]

do you mean global as in using [lua]_G[“score”][/lua]?

or do you mean

[lua]score=0

local function addPoints(n)

score=score+n

end[/lua]

in the last case you could make the initial definition local as well, (depending on what file it is defined in and what other files are accessing it though)

[lua]local score=0

local function addPoints(n)

score=score+n

end[/lua] [import]uid: 6645 topic_id: 6236 reply_id: 21459[/import]

Thanks for the replies. No I’m talking about _G.score

i’ll be checking that link out Ricardo! thanks [import]uid: 30185 topic_id: 6236 reply_id: 21463[/import]

hmm if 4 _G variables is really what’s slowing it down . try a table with the vars in it

eg

[lua]_G[“vars”]={}
_G[“vars”].position=10
_G[“vars”].score = 100[/lua]

might make a difference. dont know

also, though… dont access all your variables from _G with _G all the time

probably bad
[lua]if(_G[“score”] > 100) then
_G[“score”]=_G[“score”]+10
else
_G[“score”]=_G[“score”]+5
end[/lua]
probably good
[lua]-- get & cache the score locally
local score = _G[“score”]

if(score > 100) then
score=score+10
else
score=score+5
end

– save the amended score back to the global
_G[“score”]=score[/lua]

[import]uid: 6645 topic_id: 6236 reply_id: 21464[/import]

Thanks jmp! I’ll try that out as soon as I get home! [import]uid: 30185 topic_id: 6236 reply_id: 21466[/import]

actually you can see the speed difference there

go here http://www.lua.org/cgi-bin/demo and try these 2 programs

Test #1
[lua]_G[“score”]=5

local startTime = os.clock()

for i=1, 3500000, 1 do
if(_G[“score”] > 1000) then
_G[“score”]=_G[“score”]+10
else
_G[“score”]=_G[“score”]+5
end
end

print(_G[“score”])

print(os.clock()-startTime)[/lua]
Test #2
[lua]_G[“score”]=5

local startTime = os.clock()

local score = _G[“score”]
for i=1, 3500000, 1 do
if(score > 1000) then
score=score+10
else
score=score+5
end
end

print(score)
_G[“score”]=score

print(os.clock()-startTime)[/lua]

i get 0.77 for the first and 0.14 for the second

and remember that caching is important for functions too

[lua]local startTime = os.clock()
for i=1, 2000000, 1 do
local s=os.clock()
end
print(os.clock()-startTime) – 0.44s

– cache os.clock

local clock = os.clock
local startTime2 = clock()
for i=1, 2000000, 1 do
local s=clock() – calling the function is faster now
end
print(clock()-startTime2) – 0.37s[/lua] [import]uid: 6645 topic_id: 6236 reply_id: 21467[/import]

Wow yeah, changing that made some improvements! I was using a global variable inside an enterFrame and I assume that was draining a lot! [import]uid: 30185 topic_id: 6236 reply_id: 21499[/import]

http://developer.anscamobile.com/content/performance-and-optimization [import]uid: 6645 topic_id: 6236 reply_id: 21521[/import]

Yeah I read that one, been optimizing my code according to it for 2 days now… Also though, that thing you said about the spawning. Is there any tricks to ‘optimize’ that too for better performance? Code:

local rnd = math.random  
local function choose1(event)  
  
 local choose = rnd(1, 2)  
 -- If satsen  
 if(choose == 1) then  
 enemyNumber = rnd(1, 7)  
  
 enemy = display.newImage(""..enemyNumber..".png", rnd(0, 480), -140)  
 localGroup:insert(enemy)  
 worldGroup:insert(enemy)  
  
 -- Power på enemy  
 if(enemyNumber == 1) then  
 enemyPower = 4  
 enemy.class = "1"  
 elseif(enemyNumber == 2) then  
 enemyPower = 5  
 enemy.class = "2"  
 elseif(enemyNumber == 3) then  
 enemyPower = 6  
 enemy.class = "3"  
 elseif(enemyNumber == 4) then  
 enemyPower = 7  
 enemy.class = "4"  
 elseif(enemyNumber == 5) then  
 enemyPower = 8  
 enemy.class = "5"  
 elseif(enemyNumber == 6) then  
 enemyPower = 12  
 enemy.class = "6"  
 elseif(enemyNumber == 7) then  
 enemyPower = 14  
 enemy.class = "7"  
  
  
  
 -- Fysik  
 physics.addBody(enemy, { density=1.0, friction=0.3, bounce=0, isSensor=true })  
  
 enemy:applyForce(0, rnd(10, 30), 0, 0)  
  
 -- Lägg in i grupp  
 enemyGroup:insert(enemy)  
  
 -- Ingen rotation  
 enemy.isFixedRotation = true  
  
 local function enemyCollision(self, event)  
  
 if(event.phase == "began" and event.other.class == "enemyCleaner") then  
  
 if(enemy) then  
 self:removeSelf()  
 self = nil  
 end  
  
 elseif(event.phase == "began" and event.other.class == "eastWall") then  
  
 if(enemy) then  
 self:removeSelf()  
 self = nil  
 end  
  
 end  
  
  
 end  
 enemy.collision = enemyCollision  
 enemy:addEventListener("collision", enemy)   
  
  
  
 -- Slut på If satsen  
 end  
 return true  
  
end  
local enemyTimer = performAfterDelay(0.2, choose1, 0, true)  

I shortened the code a bit, in the real one I’ve got approx 40 different if statements checking if the enemy spawned is a cretain number then adding attributes to it. Is this code in maximum optimization…? [import]uid: 30185 topic_id: 6236 reply_id: 21564[/import]