Score showing on gameover screen?

Hey,

So I’m developing a game using Corona SDK and the Director Class and I was wondering if it is possible to have the score display on the game over screen?

Inside the playGroup (group in game.lua file), I have a variable called score which updates over a period of time, displayed using text that calls the variable.

I was wondering if it is possible to show this score on the gameover.lua page, with a group called ‘endGroup’.

I am unsure how to achieve this, so any help would be appreciated.

Score code:

local score = 0 local txtScore = display.newText( " " .. score, 0, 0, native.systemFontBold, 16) txtScore.x = 10 txtScore.text = score

Thanks. [import]uid: 88269 topic_id: 18249 reply_id: 318249[/import]

Because it’s local the score isn’t going to get carried over if you are using director. Because you’re a test user you can’t yet use the new storyboard, so your only real option is to make the score global.

If you make the score global rather than local it will then be accessible from your gameover screen and you can show it there too. (As it is currently local it will be nil.)

Make sense?

Peach :slight_smile: [import]uid: 52491 topic_id: 18249 reply_id: 69851[/import]

Hey, thanks for replying.

I tried to make it global and call it on the gameover screen, however the gameover screen just prints “0”.

How could I fix this? [import]uid: 88269 topic_id: 18249 reply_id: 69863[/import]

I thought using global variables led to memory leaks? No?? If not, that would be great for my dev…

Thanks! [import]uid: 20690 topic_id: 18249 reply_id: 69883[/import]

Hey there,

Is it saying 0 because you’ve stated “local score = 0” prior to printing the value? (If you set it as something else in the game it should hold that value on the gameover screen provided you don’t reset it to 0.)

@tcm692 - Globals aren’t good and you shouldn’t use them, however there WILL be times that you need them. Avoid them at all costs but when there’s no way around it, don’t be afraid to use one for something like a score.

A carefully selected global wont cause major memory leaks - using globals carelessly and not trying to avoid them will lead to leaks but anyone coding like that is going to have leaks anyway :wink:

Peach :slight_smile: [import]uid: 52491 topic_id: 18249 reply_id: 69979[/import]

soo…is there a different/better/alternative way to show score(s) on a different page? Or…(gasp!)…not use director?! [import]uid: 20690 topic_id: 18249 reply_id: 70225[/import]

peach actually has submitted some code for implementing a POINT/SCORING mechanism.

I had a simple POINT mechanism in under 5 minutes !!!

Check it out under the publically submitted code SECTION.

thanks again peach !!!
[import]uid: 11094 topic_id: 18249 reply_id: 70226[/import]

My Ice library might be able to help - http://developer.anscamobile.com/code/ice [import]uid: 5833 topic_id: 18249 reply_id: 70228[/import]

Globals are costly in performance because it takes longer to look them up. The can cause problems when you use the same variable for different things while not realizing it. One of those issues can be a memory leak if you allocate something like a display object, then overwrite that variable with something else.

Global variables have use and used judicially are okay. One of the reasons we have the _G table is that is specifically tells you the variable is global in scope to help you realize what you’re doing.

But it is super simple to make a module for this:

-- highscore.lua  
module(..., package.seeall)  
  
local score  
  
function set(value)  
 score = value  
end  
  
function get()  
 return score  
end  

then later

local highscore = require("highscore")  
  
local score = score + amount  
highscore.set(score)  
  

then later in another module:

local highscore = require("highscore")  
  
local score = highscore.get()  
  

All of that is of course untested… but in theory should work. [import]uid: 19626 topic_id: 18249 reply_id: 70260[/import]