ID:2645523
 
(See the best response by Kaiochao.)
Code:
            var/list/denyvar = list("locs","type","parent_type","verbs")
for(var/obj/custom_card/cust in usr.custom_cards)
if(cust.template_card)
var/obj/cards/CC = new cust.template_card.type()
for(var/i in cust.template_card.vars)
if(i in denyvar) continue
CC.vars[i] = cust.template_card.vars[i]
CC.CrdLoc = 0
usr.Set += CC


Problem description:
runtime error: Cannot write to datum.vars.
proc name: View Set (/mob/verb/View_Set)
usr: (src)
src: IceFire2050 (/mob)
src.loc: (65,125,1) (/turf/FLOOR/CENTER/SMOL)
call stack:
IceFire2050 (/mob): View Set("cust")


So I am trying to add a system to my card game that allows for some flexible use of custom created cards.

I've been slowly working through the system and this is the last piece that seems to be giving me issue.

The usr mob has a list var usr.custom_cards which stores all of their created cards. It's a list of the obj/custom_card.

The obj/custom_card has a var which is template_card which stores a copy of a created card with all of its 3 dozen-ish variables already defined.

My goal with the above code is to make a copy of the template_card. Give the copy all the same variables as the template_card, and then add it to the usr.Set list, which will be used in a farther section of this code.

But when I try to run this, I get the error above.

Anyone know if I'm overlooking something?
Found my answer. I did not realize there's a vars var inside the vars var.
Best response
I think the simplest way to copy objects is to use savefiles:
custom_card
proc/Copy()
var/savefile/copier = new // temporary savefile
copier << src // save src
var/datum/copy
copier >> copy // load src as new copy object
return copy

Another alternative is to use a separate datum to contain data that is shared among all copies, by simply having all copies refer to the same datum:
custom_card_data
// all the shared custom card vars go here instead
// e.g.
var/name

custom_card
var/custom_card_data/data

proc/Copy()
var/custom_card/copy = new
copy.data = data // the data object is shared
return copy

// e.g.
proc/Name()
return data.name

Sharing the data object means changing it will change all users of that object, though. Copying the data object to make changes could be done with the above savefile-based copier.