Re: Dynamic class change?
Re: Dynamic class change?
- Subject: Re: Dynamic class change?
- From: Graham J Lee <email@hidden>
- Date: Wed, 1 Jun 2005 17:40:05 +0100
On 1 Jun 2005, at 17:28, Justin Bakse wrote:
I am looking for general thoughts on how to make an object change its
class during runtime. Or at least make it appear to. The objects would
be chaning between related classes, probably all subclasses of the
same parent. To make it clearer what I'm trying to do, and why, here
is a little context. These objects will represent enemies in a game
level editor. The editor is built in the full running engine of the
game, with time stopped. When you place the a new enemy on the level,
an object of some enemy subclass is generated, and it draws itself,
and otherwise behaves just as it would during game play (except
paused). Later, you might wish to change the class of the enemy. That
is when I wish have the object change class.
My thinking right now is that the object (or some where else in code)
would have to create a new object, copy over all the member data that
it could, and then delete the original. One possible problem of that
is that any other objects that reference the changing object would
have to be updated to reference the new object.
Anyone have any ideas for a better way to get this effect?
Change the 'isa' ivar. The Object root class lets you cleanly do this
with -transmuteClassTo: but I don't think NSObject has an equivalent
method. It's not guaranteed to do anything sensible anyway; your two
classes must be the same size and the original class must descend from
the destination class. I'd go with your original plan to copy the
ivars across to a new object and return that, unless the above
conditions are met.
--
Graham Lee GPG Key ID: 01D5B9D8
UNIX Systems Manager,
Oxford Physics Practical Course
http://nextstep.sdf-eu.org 01865 273450
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden