Jump to content
  • Advertisement
Sign in to follow this  
bepawuca

Pointer-like functionality in AS3?

This topic is 3263 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any way at all to store a C++-like pointer in AS3? What I need is some way to keep track of an object's state without regularly passing that object as a parameter. For example, let an enemy have a pointer to a player to attack without needing to regularaly tell the enemy where the player is, and without storing the player object itself as part of the enemy.

Share this post


Link to post
Share on other sites
Advertisement
Pass the player in to the enemy and save it in a member variable, perhaps in a set property, perhaps in a function called setTarget() - whichever floats your boat.

In AS3, as in most managed languages, what you're really passing around are references to objects - in other words, pointers by another name.

In pseudo-AS3:

class enemy
{
private var _target:Player;

function set target(player:Player):void {
_target = player;
}

function update():void {
if (_target != NULL)
attack(_target);
}
}

Share this post


Link to post
Share on other sites
sindisil - Thanks for pointing that out about the references in AS3.
My main problem here, though, isn't the memory issue - it's more that I would prefer not to need to keep passing the object to another every frame.

I guess I'd better just get used to AS3 and stop trying to use C++ everywhere.

Share this post


Link to post
Share on other sites
Quote:
Original post by bepawuca
sindisil - Thanks for pointing that out about the references in AS3.
My main problem here, though, isn't the memory issue - it's more that I would prefer not to need to keep passing the object to another every frame.


Sure.

So the mechanism I've outlined gets you exactly that.
Quote:


I guess I'd better just get used to AS3 and stop trying to use C++ everywhere.


Yes. It is virtually always better to use a language idiomatically.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!