Hello there!
I wonder how objects can inherit/copy other objects keys/values.
I watched some videos and read through some explanation pages about prototypes, constructors and stuff, but found it a bit confusing.
Some of them say don't do that. Don't use this. Since ES6 this is obsolete. This is old. Even though you can still do this, you should do that instead etc. etc. .
Maybe someone can explain this to me how it can be done before I spend another 5 hours finding the up-to-datest or best way to inherit one objects properties into another ones.
What I'm trying to do is as follows: I want to have objects that are "templates" for other objects.
Let's have an example. An RPG.
There could be a template with the key weapons, one with the key armors, spells and one with the key items.
This are just fantasy values but the JSON could look like this:
{
"templates": {
"rarities": {
"colors": {
"common": "80,80,80",
"rare": "0,0,80",
"epic": "90,0,80"
}
},
"weapons": {
"color": "100,0,0",
"name": {
"x": 25,
"y": 5,
"font_size": 16,
"font_style": "bold"
},
"text": {
"x": 10,
"y": 50,
"font_size": 12,
"font_style": ""
},
"picture": {
"x": 0,
"y": 20,
"width": 50,
"height": 50
},
"attack": {
"x": 40,
"y": 5,
"font_size": 18,
"font_style": "bold",
"icon": "sword"
}
},
"spells": {
"color": "0,0,100",
"name": {
"x": 25,
"y": 5,
"font_size": 16,
"font_style": "bold"
},
"text": {
"x": 10,
"y": 120,
"font_size": 12,
"font_style": "italic"
},
"picture": {
"x": 0,
"y": 20,
"width": 50,
"height": 100
},
"cost": {
"x": 40,
"y": 5,
"font_size": 18,
"font_style": "bold",
"icon": "mana"
}
}
}
}
Now if I create new weapons in the JSON I do not want to always define where the x and y positions of the weapons name should be or where it's picture should be etc. .
{
"items": {
"weapons": {
"1": {
"name": "Axe",
"text": "Great for cutting trees.",
"attack": 5,
"rarity": "common"
},
"2": {
"name": "Rusty Sword",
"text": "Not a great sword at all.",
"attack": 7,
"rarity": "common"
},
"3": {
"name": "Rusty Greatsword",
"text": "A greatsword which is not a great sword either.",
"attack": 10,
"rarity": "common"
}
}
}
}
1) How can I inherit the templates properties? Is there a way how I can have all of the entries under the key "weapons" have the properties of the weapons template or do I have to "link" the weapon template to each of the weapons individually?
Oh and bonus question:
2) Should I instead of an ID as the key name use the weapons name as the key name? Or an array?
{
"weapons":{
"Axe": {
"text": "Great for cutting trees.",
"attack": 5,
"rarity": "common"
},
"Rusty Sword": {
"text": "Not a great sword at all.",
"attack": 7,
"rarity": "common"
},
"Rusty Greatsword": {
"text": "A greatsword which is not a great sword either.",
"attack": 10,
"rarity": "common"
}
}
}
Thanks for reading and thank you for explaining it to me. :D