ID:266688
 
This should be so simple, but it befuddles me.

In OO-speak, I'd like to cast a parent object to a type that inherits from it. The basic idea of my code is as follows...

obj/host_settings
//further class definition goes here
obj/host_settings/game_obj
//further class definition goes here
proc/extraMethodNotFoundInHostSettings()
//define method here
.
.
//At some point within my code...

var/obj/host_settings/foo
var/obj/host_settings/game_obj/bar

bar = foo
bar.extraMethodNotFoundInHostSettings()


Now the problem with this code, is that the DM compiler tells me there's no such method associated with bar. It thinks that bar is of the 'obj/host_settings' class. So how do I cast the foo object to 'obj/host_settings/game_object'? Is it even possible? Or am I going to have to write some code which copies variables between the objects?

Regards,
Corporate Dog
Now the problem with this code, is that the DM compiler tells me there's no such method associated with bar. It thinks that bar is of the 'obj/host_settings' class. So how do I cast the foo object to 'obj/host_settings/game_object'? Is it even possible? Or am I going to have to write some code which copies variables between the objects?

Strange... what you are doing should compile fine, but it would cause a run-time error. Are you sure the compiler is giving the error?

In any case, I don't actually think there is a way to dynamically change an object's type. If the 'parent_type' var isn't compile-time only, maybe you could change that to /obj/host_settings/game_obj at run-time, which would make the foo object inherit those procs automatically.

However, I doubt that would actually work.


Also note that trying to stick a foo into the bar variable is simply putting a /obj/host_settings into a /obj/host_settings/game_obj var. Variables are not designed to change the data provided to them.

Heck,

var/list/L = usr

will work (or at least I think it would).


Fortunately, the proc for copying an object is really quite simple:

obj/proc/duplicate(obj/O)
//Makes src become a near-perfect copy of O
//Variables marked 'const', 'tmp', 'global', or 'static' will not be copied!

for(var/V in O.vars) if(!issaved(V)) if(src.vars[V] != O.vars[V]) src.vars[V] = O.vars[V]
In response to Spuzzum
Spuzzum wrote:

Strange... what you are doing should compile fine, but it
would cause a run-time error.

Yep. My bad. That's exactly what it's doing.

Fortunately, the proc for copying an object is really
quite simple:

Thanks, Spuzzum! Guess I'm too used to the niceties of Java. :)

Regards,
Corporate Dog