ID:263433
 
Code:
Duplicate_Item(O as obj in src)
var/obj/M = O
new M (src)


Problem description:
runtime error: Cannot create objects of type /obj/EconItems/Pick.
proc name: Duplicate Item (/mob/God/verb/Duplicate_Item)
source file: New GM.dm,39
usr: Knowbie (/mob)
src: Knowbie (/mob)
call stack:
Knowbie (/mob): Duplicate Item(Pick (/obj/EconItems/Pick))
M is an object, not a type path. Hence new() won't work the way you've got it.

Lummox JR
Duplicate_Item(obj/O as obj in src)
set category = "GoD"
var/path = O.type
var/obj/M = new path //Creates a new obj of type that was passed
M.loc = src //Sets the loc
for(var/p in M.vars) //Loops through all of the created obj's vars
if(p != "type" && p != "parent_type" && p != "verbs" && p != "vars") //Cancels if the var is one that can't be edited or has null value or would be stupid
M.vars[p] = O.vars[p] //Sets the new obj's var to the old one's.


Now, if I comment out the last 3 lines, it creates the object and gives it to me, but if the last 3 lines are active, the object isn't created. No runtime error happens.
In response to Top player
You better have a specialized proc for duplicating datums anyway. Try to use this:
datum/proc/Copy(datum/D)
if(!D) D = new src.type() //if no datum to copy the vars over to was specified, create a new one from the same type as 'src'
else if(!istype(D)) return 0 //if something was specified but it isn't a datum, return 0
var/list/skipped_vars = list("type","parent_type","vars",\
"verbs","tag","key","ckey","client","group") //there may be more vars needed here
for(var/V in src.vars) //loop threw ever var 'src' has
if(!(V in skipped_vars) && V in D.vars) //if the var isn't in skipped_vars, and D has the var too
D.vars[V] = src.vars[V] //copy the value over
return D //return the datum, if needed (typically if none was specified, so this proc created a new one)

mob/verb/Duplicate_Item(obj/O in view())
var/obj/new_item = O.Copy() //call the copy proc to copy from O, and store the created mob into 'new_item' var
//the new_item will have the same loc as O since it's copied, but now you can move the new_item anywhere you want.
new_item.loc = src //put it in the player's contents*


*I'd use a different method, like a 'set_loc' proc, but this is for demonstration only and stuff.

EDIT: You can make the Copy() a bit shorter and faster like this:
datum/proc/Copy(datum/D)
if(D && !istype(D)) //if an argument was specified, but it isn't a datum
return 0 //return false and stop the proc
if(!D) D = new src.type() //if no datum to copy the vars over to was specified, create a new one from the same type as 'src'
for(var/V in (src.vars-list("type","parent_type","vars",\
"verbs","tag","key","ckey","client","group"))) //there may be more vars needed here))
if(V in D.vars) D.vars[V] = src.vars[V] //if D has the var as well...
return D //return the datum, since it may be needed (typically if none was specified, so this proc created a new one)