Displaying score on other screen

Hello,

I have a game in which you score points, i am using director class. The score will later then appear on another screen (main menu). But i have noticed that adding the score to another director screen is not as easy as i thought. I was wondering how i could do this. Basically scoring points on another screen then displaying the score on different screen. Does anyone have any ideas on how this can be done? [import]uid: 66985 topic_id: 18275 reply_id: 318275[/import]

Moved this to the appropriate forum. (Features and Roadmap is not for questions like this.)

I actually answered a question about this same thing earlier today, see this thread; http://developer.anscamobile.com/forum/2011/11/26/score-showing-gameover-screen#comment-69979

Any questions, let me know.

Peach :slight_smile: [import]uid: 52491 topic_id: 18275 reply_id: 70006[/import]

Sorry, I forgot to change the forum.

I am still confused with global variables. Can you give me a quick preview on what exactly they are?
And the difference between local and Global. Thanks a lot. [import]uid: 66985 topic_id: 18275 reply_id: 70048[/import]

Global - more of a memory hog, should be avoided where possible, can be accessed from any other “scene” in your app.

Local - much better choice but can only be accessed from the “scene” you’ve created it in.

Now although globals are “bad” there are times you’ll find you can’t really get around using them. You just don’t want to make a habit of it.

In your case, you start your app using Director and you go to your actual game play scene.

In this scene right now you have something like;
[lua]local score = 0[/lua]

Then you add to it doing something like this;
[lua]score = score + 10[/lua]

Correct?

Well, in your game over scene if you said;
[lua]print (score)[/lua]

It would return nil.

SO - you could do this in your game file;
[lua]_G.score = 0
_G.score = _G.score + 10[/lua]

Then on gameover do;
[lua]print (_G.score)[/lua]

And the correct value from the game will be returned.

Does this clear things up? If not I can try to get more in depth.

Peach :slight_smile:

PS - I must stress, again, that you should avoid globals where possible - don’t make a habit of it. [import]uid: 52491 topic_id: 18275 reply_id: 70100[/import]

Yes that clears a lot up! Thanks so much. Just to be clear, in my level1.lua i have something just like that.

local score = 0
score = score + 10.

So to display the same score on another screen I would replace the code on level1.lua by adding this:

_G.score = 0
_G.score = _G.score + 10

Now this would be able to record the games score on different screens, right?

Thanks so much Peach!
[import]uid: 66985 topic_id: 18275 reply_id: 70124[/import]

You’re correct - and I’m happy to have been able to help.

Any issues let me know, I’ve done this a million times with Director so I’m pretty used to it by now.

Peach :slight_smile: [import]uid: 52491 topic_id: 18275 reply_id: 70126[/import]

Hey I have one question. I might be using global variables a lot during my game, approximately 70 times for different scores. I was wondering if their was any other way to get around this and is that a lot? Also, what does memory leaks exactly mean? The cause of the global variables?

Thanks. [import]uid: 66985 topic_id: 18275 reply_id: 71803[/import]

Memory leaks are when you are… well, I’ll give you an example.

You load your menu screen and memory usage is 0.5MB, texture memory usage is 0.5MB (random numbers) - you go to your options screen and usage goes to 0.4, 0.6 - then you go back to menu and it’s at 0.6, 0.6. There is something not getting cleaned up there as it should be 0.5, 0.5.

Here is a function you can use to check for memory usage (and texture memory usage) and see if you have a problem with a memory leak(s);
[lua]local function monitorMem(event)
collectgarbage(“collect”)

print( “\nMemUsage: " … (collectgarbage(“count”)/1000) … " MB”)
print("Texture Usage " … system.getInfo( “textureMemoryUsed” ) / 1000000)

return true
end

Runtime:addEventListener(“enterFrame”, monitorMem)[/lua]

70 globals is a lot and not a great idea.

Here’s something worth reading, skip to the section on globals and memory management; http://developer.anscamobile.com/content/app-programming-guide

You could also consider using Ice to save and load as needed rather than having 70 all the time; http://developer.anscamobile.com/code/ice

Peach :slight_smile: [import]uid: 52491 topic_id: 18275 reply_id: 71816[/import]