Author Topic: Const objects?!  (Read 1383 times)

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Const objects?!
« on: Sun, Aug 12, 2012 »
Hi,

I would like to store different speed variables for my objects like main player into constant objects but problem is when I define a const object, I can change it's components, for example if you:

var const RunSpeed:FlxPoint = new FlxPoint (100, 200);

then you can do this later on:

RunSpeed.x = 101; // this compiles OK, wtf!

So any way to do this?
blog, twitter, Check out my award winning game, Rot Gut:

Jeff

  • Active Member
  • ***
  • Posts: 127
  • Karma: +0/-0
    • View Profile
    • Entertainment Evolution
Re: Const objects?!
« Reply #1 on: Mon, Aug 13, 2012 »
Hi,

I would like to store different speed variables for my objects like main player into constant objects but problem is when I define a const object, I can change it's components, for example if you:

var const RunSpeed:FlxPoint = new FlxPoint (100, 200);

then you can do this later on:

RunSpeed.x = 101; // this compiles OK, wtf!

So any way to do this?

I don't find the "RunSpeed.x = 101" a wtf issue, really. Even though the object is constant, it doesn't mean that it's contents need to be constant. Take yourself as an example. You can't be swapped for Barack Obama, but you can change your clothing.

Why is it important that you couldn't change the runspeed from within the code? If it's important for some reason then you could make an extension of the FlxPoint that doesn't allow changing those variables.
Please give me feedback for my latest game in progress:
Blog: Entertainment Evolution

puggsoy

  • Not done quite yet
  • Member
  • **
  • Posts: 33
  • Karma: +0/-0
    • View Profile
Re: Const objects?!
« Reply #2 on: Mon, Aug 13, 2012 »
Looks like you're defining the constant wrong. You shouldn't write

Code: [Select]
var const RunSpeed:FlxPoint = new FlxPoint (100, 200);
but rather just

Code: [Select]
const RunSpeed:FlxPoint = new FlxPoint (100, 200);
You can either write "var" or "const", but not "var const". A variable and constant are different things: a variable varies, and a constant stays, well, constant. So trying to make a variable a constant is like trying to make an apple a pear: you can't, it'll just be an apple. Same applies here: you can't make a variable a constant, it'll just be a variable, hence why you can change the value.

What I find weird is that it compiled. The first line, that is. I thought AS3 would be smart enough to not allow you to try and use var and const at the same time since, as far as I can see, there's no point.
« Last Edit: Mon, Aug 13, 2012 by puggsoy »

JohnColburn

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Const objects?!
« Reply #3 on: Mon, Aug 13, 2012 »
As pointed out by Jeff, a const is actually just a constant reference. For immutable objects like Numbers or Strings, this makes the data itself effectively constant. For mutable data types (including Object and all of its subclasses IE basically every class) the const just guarantees that the reference will always point to the same object, whose properties may change at any time.

I'll also echo Jeff's "why do you need to guarantee that this cannot change?". Sounds like a pretty serious "just don't do that" situation to me. If you don't want run speed to change then don't change it. If you need the compiler to stop you from changing your physics constants you may need to restructure something.

That said, I took like two minutes to write up point class that will throw a compiler error if you ever try to change its values:

Code: [Select]
public class ConstPoint
{
protected var _x:Number;
protected var _y:Number;

public function ConstPoint(x:Number=0, y:Number=0){
_x = x;
_y = y;
}

public function get x():Number{ return _x; }
public function get y():Number{ return _y; }
}

I tried extending FlxPoint, but it uses public vars for x and y, so it's not easy to just override with accessors.

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Const objects?!
« Reply #4 on: Mon, Aug 13, 2012 »
Hi,

I would like to store different speed variables for my objects like main player into constant objects but problem is when I define a const object, I can change it's components, for example if you:

var const RunSpeed:FlxPoint = new FlxPoint (100, 200);

then you can do this later on:

RunSpeed.x = 101; // this compiles OK, wtf!

So any way to do this?

Note that even if you correctly use the const keyword here, you will still be able to change the x and y values. When you say const, you are saying the "pointer" to the object is constant, not that everything within the object is constant. For example:

const p:FlxPoint = new FlxPoint(10, 20); // make a constant point
trace(p.x, p.y); // prints 10, 20
p.x = 5; // this will compile and work
p.y = 6; // and this
trace(p.x, p.y); // prints 5, 6
p = new FlxPoint(500, 500); // this will NOT work because p is const

So while you cannot change what p points to (using =), if it points to a non-primative, you can alter the object.

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Const objects?!
« Reply #5 on: Mon, Aug 13, 2012 »
Arkeus did a great job explaining why. To solve your issue, I think the easiest way would just be to create 2 primitives, eg

const runSpeedX:Number = 100;
const runSpeedY:Number = 200;

Then runSpeedX = 101 won't work.

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Const objects?!
« Reply #6 on: Wed, Aug 15, 2012 »
Thanks I get it guys.
blog, twitter, Check out my award winning game, Rot Gut: