ID:180895
 
I'm looking for an easy way to duplicate an obj and not sure if I'm missing something that could really help.

I basically just need a new obj type of the same type as the original plus all of the variables the same.

Any ideas?

- -
On 11/6/00 6:17 pm Gabriel wrote:
I'm looking for an easy way to duplicate an obj and not sure if I'm missing something that could really help.

I basically just need a new obj type of the same type as the original plus all of the variables the same.

Any ideas?

- -


There's no automated support for it, because DM would have a hard time knowing what the proper behavior would be. The challenge in any OO system is to define what copy means in different contexts.

There is no such thing as copying a single object. Because that object may contain or refer to other objects, a decision has to be made about how "shallow" or "deep" the copy is, that is when you stop duplicating everything you find.

So you pretty much have to decide that for yourself.

You might consider doing a Copy() proc in your objects that behaves similar to the Write() proc does for savefiles -- create a new object, fill it appropriately, and return it, like so:

obj/proc/Copy()
var/new_me = new type()
new_me.my_first_var = my_first_var
.
.
return new_me

etc.

Some clever stuff could be done using the same techniques that BYOND uses for the automated Write() functionality (and maybe Dantom might think about putting a future Copy() on the list), but you'll probably find it simplest to just do this.

In response to Deadron (#1)
Deadron's methods a little safer, but if you have a ton of vars and don't feel like typing them all out, you can do it with some clever use of the vars list, like so:

obj/proc/Copy()
var/obj/new_obj = new type()
var/list/dontcopy = list("verbs","vars","type")//Read-only
var/V
for(V in vars)
if(!dontcopy.Find("[V]"))
new_obj.vars["[V]"] = vars["[V]"]
return new_obj

You'll have to change the location of the new object, since it would be copied to the location of the old one, but thats why I returned the new_obj. You can force it not to copy some variables by adding their names to the dontcopy list.

-AbyssDragon
In response to AbyssDragon (#2)
On 11/6/00 7:14 pm AbyssDragon wrote:
Deadron's methods a little safer, but if you have a ton of vars and don't feel like typing them all out, you can do it with some clever use of the vars list, like so:

obj/proc/Copy()
var/obj/new_obj = new type()
var/list/dontcopy = list("verbs","vars","type")//Read-only
var/V
for(V in vars)
if(!dontcopy.Find("[V]"))
new_obj.vars["[V]"] = vars["[V]"]
return new_obj

You'll have to change the location of the new object, since it would be copied to the location of the old one, but thats why I returned the new_obj. You can force it not to copy some variables by adding their names to the dontcopy list.

You could also add a check in between -if(!dontcopy.Find("[V]"))- and -new_obj.vars["[V]"] = vars["[V]"]-:

if(issaved(V))

In some cases that might not be desired if you want an exact duplicate, but if you want, for example, to make the process more speedy, and/or you have temporary variables that shouldn't be copied, then this is the way to go.

I might add that Deadron did mention the way of using the vars list as included in The Dream Maker book, where Dan explains the soft-coded version of Write(). But if you don't have the book, it doesn't help much to hear mention of it... so whoever hasn't, go buy it already! ;-)