Forum Topic: AS2 Class - non-static object vars

(109 views • 5 replies)

This topic is 1 page long.

<< < > >>
None

ShirkDeio

Reply To Post Reply & Quote

Posted at: 9/23/09 11:29 PM

ShirkDeio LIGHT LEVEL 16

Sign-Up: 03/29/07

Posts: 989

I'm working on a class in AS2 and I'm having a problem with variables that are objects behaving as static variables, but I don't want them to do that.

Here's a quick example. I want to use an object to hold several stats for my class (more complicated in the real code -- objects are desirable over several number variables). I have included a number variable to demonstrate what I want the object to do.

// Class code
class MyClass {
  var stats = {health:100,magic:100,stamina:100};
  var health = 100;
  
  public function MyClass() {};

  public function changeHealth(amount:Number):Void {
    stats.health += amount;
	health += amount;
  };

  public function getStatsHealth():Number {
    return stats.health;
  };
  
  public function getHealth():Number {
    return health;
  };
}

// Timeline code
var m1 = new MyClass();
var m2 = new MyClass();
m1.changeHealth(10);
m2.changeHealth(-5);

trace(m1.getStatsHealth());  // Returns 105 instead of expected 110
trace(m1.getHealth());  // Returns expected 110

Interestingly, this works:

class MyClass {
  var stats;
  var health = 100;

  public function MyClass(p_stats:Object) {
    stats = p_stats;
  }
  [..]
}

// Timeline
var m1 = new MyClass({health:100});
var m2 = new MyClass({health:100});
m1.changeHealth(10);
m2.changeHealth(-5);

trace(m1.getStatsHealth());  // Returns expected 110
trace(m1.getHealth());  // Returns expected 110

I may have to construct things by passing objects to the constructor, but it seems like an unnecessary hassle to me. Anyways, I hope I explained my problem well enough. I want to use object variables in my class, but it's treating all my objects like static variables that are the same across all instances, which I do not want. Is there any way around this problem, aside from passing the objects directly to the constructor (basically eliminating any possibility for a default)?

I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)


None

ShirkDeio

Reply To Post Reply & Quote

Posted at: 9/23/09 11:37 PM

ShirkDeio LIGHT LEVEL 16

Sign-Up: 03/29/07

Posts: 989

Wow. ActionScript is weird. I found a fix. Strange to me, but by declaring the object inside the constructor rather than outside, a new object is created rather than a reference to a "static-like" object. Works well enough for me.

Any thoughts?

class MyClass {
	static var default_stats:Object = {health:100};
	var stats:Object;
	
	public function MyClass() {
		stats = {};
		for(var i in default_stats) {
			stats[i] = default_stats[i];
		};
	};
	
	public function changeHealth(amount:Number):Void {
		stats.health += amount;
	};
	
	public function getHealth():Number {
		return stats.health;
	};
}

And in the timeline:

var m1 = new MyClass();
var m2 = new MyClass();
m1.changeHealth(10);
m2.changeHealth(-5);

trace(m1.getHealth());  // Returns expected 110
trace(m2.getHealth());  // Returns expected 95

I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)


None

milchreis

Reply To Post Reply & Quote

Posted at: 9/24/09 04:29 AM

milchreis DARK LEVEL 16

Sign-Up: 01/11/08

Posts: 430

why do you use an object as status? why not a status class?
why don't you give some love to your vars and type them?
why do you use a fake getter function, even though your variable is public?

"Even if it's just cynicism that remained to push us ahead, we'll carry on!"


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 9/24/09 04:44 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,548

If you want to know the reason for this:

EVERYTHING in flash, apart from the base types, acts as a pointers, whenever you call 'new' (of which declaring an object using {} and arrays using [] implicitly does) it allocates space for that object, whatever type it may be, and gives you a pointer to it.

It's for this reason that the following occurs:

function move(m:MovieClip):Void
{
    m._x += 150;
}

because when you pass the movieclip as the argument to the function, you're passing a pointer to the original movieclip, not a copy of it, this is the same for everyobject, whether it be a movieclip, an array, a basic Object instance, whatever.

When flash compiles a class, anything that is simply inside of the declaration, is defined in the static class constructor, as opposed to the constructor called whenever an instance is created of the class through 'new'

Since objects are pointers, not the actual data itself, if you define an object,array, or anything else that is not a base type within the declaration, and not in the constructor for the class, each class will be set up to have the same value at creation, which in this case, means each class will have an instance of that object, pointing to the same object in memory.

It is not strictly static, because you could do the following:

class Test
{
   public var o:Object = {x:10};
}
var a:Test = new Test();
var b:Test = new Test();
a.o.x += 10; 
trace(b.o.x); //traces 20, the pointer 'o' within a,b are still both pointing to the same object, so it appears to be acting as a static member referenced through the object of the class

a.o = {x:0};
trace(a.o.x); //traces 0
trace(b.o.x); //still traces 20, by using the assignment operator, the value of the pointer has been chaged to point to a new Object.

My social worker says im special!

BBS Signature

Beaten

milchreis

Reply To Post Reply & Quote

Posted at: 9/24/09 04:53 AM

milchreis DARK LEVEL 16

Sign-Up: 01/11/08

Posts: 430

At 9/24/09 04:44 AM, dELtaluca wrote: If you want to know the reason for this:

EVERYTHING in flash, apart from the base types, acts as a pointers,

just want to add here that arrays do not count as base types, I once thought so, which messed up everything.

"Even if it's just cynicism that remained to push us ahead, we'll carry on!"


None

ShirkDeio

Reply To Post Reply & Quote

Posted at: 10/6/09 12:22 PM

ShirkDeio LIGHT LEVEL 16

Sign-Up: 03/29/07

Posts: 989

why don't you give some love to your vars and type them?

I do type them, just not in a quick, slapped together example.

If you want to know the reason for this:
EVERYTHING in flash, apart from the base types, acts as a pointers, whenever you call 'new' (of which declaring an object using {} and arrays using [] implicitly does) it allocates space for that object, whatever type it may be, and gives you a pointer to it.

Ahh, thank you so much. That makes sense. So what are base types? Numbers and strings only?

I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)


All times are Eastern Standard Time (GMT -5) | Current Time: 03:40 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!