Be a Supporter!

Global Variables

  • 307 Views
  • 13 Replies
New Topic Respond to this Topic
Noamyoungerm
Noamyoungerm
  • Member since: Dec. 6, 2009
  • Offline.
Forum Stats
Member
Level 11
Programmer
Global Variables 2010-12-22 20:37:10 Reply

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
zrb
zrb
  • Member since: Aug. 8, 2006
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Global Variables 2010-12-22 20:42:21 Reply

Just make a class containing any global variables you want to use in your game as static variables, it'd be simple to use, there's nothing wrong with that is there ?

package
{
public class Globals
{
public static var score:Number = 100;
public static var otherGlobalVariable;
}
}


School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature
Noamyoungerm
Noamyoungerm
  • Member since: Dec. 6, 2009
  • Offline.
Forum Stats
Member
Level 11
Programmer
Response to Global Variables 2010-12-22 20:47:10 Reply

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


BBS Signature
zrb
zrb
  • Member since: Aug. 8, 2006
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Global Variables 2010-12-22 20:52:29 Reply

At 12/22/10 08:47 PM, Noamyoungerm wrote: Thanks. I just don't want to immerse myself with something that's over my head.

By the way that was in AS3, I'm not sure if it would be exactly the same in AS2.


School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature
Noamyoungerm
Noamyoungerm
  • Member since: Dec. 6, 2009
  • Offline.
Forum Stats
Member
Level 11
Programmer
Response to Global Variables 2010-12-22 21:01:36 Reply

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
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Global Variables 2010-12-22 21:10:13 Reply

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
GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Global Variables 2010-12-22 21:32:57 Reply

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
zrb
zrb
  • Member since: Aug. 8, 2006
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Global Variables 2010-12-22 21:48:32 Reply

At 12/22/10 09:32 PM, GodlyKira wrote: Couldnt you set globals as your document class and still have it work?

Your document class has to have as base class Sprite or MovieClip, that wouldn't work with the Globals class.

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

Not really, that just depends on how you want to work.


School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Global Variables 2010-12-23 05:11:32 Reply

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!

GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Global Variables 2010-12-23 12:08:24 Reply

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
23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to Global Variables 2010-12-23 12:14:03 Reply

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
Noamyoungerm
Noamyoungerm
  • Member since: Dec. 6, 2009
  • Offline.
Forum Stats
Member
Level 11
Programmer
Response to Global Variables 2010-12-23 15:17:12 Reply

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
Noamyoungerm
Noamyoungerm
  • Member since: Dec. 6, 2009
  • Offline.
Forum Stats
Member
Level 11
Programmer
Response to Global Variables 2010-12-23 15:18:58 Reply

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
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Global Variables 2010-12-23 17:59:41 Reply

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