00:00
00:00
Newgrounds Background Image Theme

xo1029392 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Global Variables

519 Views | 10 Replies
New Topic Respond to this Topic

Global Variables 2010-12-22 20:37:10


I need to make a global variable "score" for a game I'm making. however. I hope to do it outside a class. is it possible to declare a global variable outside a class. Just NO CRAP about bad practice. Thanks


BBS Signature

Response to Global Variables 2010-12-22 20:47:10


Thanks. I just don't want to immerse myself with something that's over my head.


BBS Signature

Response to Global Variables 2010-12-22 21:01:36


I'm trying your method. but I don't seem able to connect the files. I've tryed

import Globals

but that didn't work.


BBS Signature

Response to Global Variables 2010-12-22 21:10:13


At 12/22/10 09:01 PM, Noamyoungerm wrote: I'm trying your method. but I don't seem able to connect the files. I've tryed

import Globals

but that didn't work.

Should be working. How are you calling the variables? Should be:

Globals.score+=20;

BBS Signature

Response to Global Variables 2010-12-22 21:32:57


Couldnt you set globals as your document class and still have it work?

also public variables really should be private and use get set methods to change values, but thats a little more in depth


BBS Signature

Response to Global Variables 2010-12-23 05:11:32


At 12/22/10 09:48 PM, zrb wrote: Not really, that just depends on how you want to work.

No it's not.
Abusing static variables like that leads to horrible code.
You will have so many and tight dependencies that using any class will be useless effort.

You only get a benefit of classes if you apply the ideas of oop. If you don't, you're just dumping code into several files. You will soon realise that this takes away most of the reusability/modularity classes can grant you.

@OP:
Your main class can have a private variable "score", only this class should change the score.
You most likely want to have your main class listen for Events of the game elements and react accordingly. (for example increase the score)

If each and everyone can change each and everything, you are screwed!

Response to Global Variables 2010-12-23 12:08:24


using static variables can weave a huge net of dependencies before you even notice those dependencies are there, then you compile your game and you have the smallest bug, youll have to go through all that shit over again and go through all your code to find the bug, while getts/setters keep evrything organized, and ofcourse you need a constructor class, I meant make variables in document class then in constructor of document class, set the variables... you dont have to declare the value for when you assign it


BBS Signature

Response to Global Variables 2010-12-23 12:14:03


One day the Flash IDE might include an automated getter and setter functionality like so many other standard IDE's. But not today!


BBS Signature

Response to Global Variables 2010-12-23 15:17:12


WOW. I wasn't expecting so many replies. or so much CRAP. anyways, I'm using Globals because My game is relatively simple. and I am having trouble moving variables from one frame to another and so on. since my game also has only one class (Globals) I really can't see myself getting tangled in dependencies or any of that crap. and it makes running functions easier.

Also
NO CRAP ABOUT BAD PRACTICE. I'm fully aware that I will go to hell if I use global variables. but i don't F***ing care


BBS Signature

Response to Global Variables 2010-12-23 15:18:58


At 12/22/10 09:10 PM, 23450 wrote:
Should be working. How are you calling the variables? Should be:

Globals.score+=20;

Thanks. That worked


BBS Signature

Response to Global Variables 2010-12-23 17:59:41


At 12/23/10 03:17 PM, Noamyoungerm wrote: Also
NO CRAP ABOUT BAD PRACTICE. I'm fully aware that I will go to hell if I use global variables. but i don't F***ing care

That pretty much speaks for itself.
makes me a little sad