ID:152358
 
When writting an equipping methods that uses an associative list to keep track of what is equipped, is it better to:

Reference the list every time a value of a variable is needed from the item.

or

Copy the values of the variables to variables owned by the mob when the item is equipped. Then reference the mobs variables.

I have done it both ways, I think the first way would be more accurate and safer, but the second way would be faster getting the values of the variables. Any opinions?
The first is a better method. I don't really understand the second- the mob (I assume you mean mob. You spelt it 'mod' but I think that was just a typo) shouldn't need extra variables for equipping things if there's already an associative list.

I don't know the all the details but I would've assumed an associative list in DM is simply an object that can have variables added to it at runtime so there's no real speed difference between accessing a variable in an associative list and a normal variable.


Also, let's face it, we're talking about DM here. Even if there were differences in speeds of two different methods the more accurate and safer option would be the best.
If you do it correctly it won't matter either way.
"Safe" beats "needlessly optimised" any day.
In response to Crispy
Crispy wrote:
"Safe" beats "needlessly optimised" any day.

Unless it's this day, where Top Gear converted a 3-wheeler Robin Reliant into a space shuttle, strapped it to a rocket and sent it into space.


If that's not the essence of "needlessly optimised" (and also the antithesis of safe) then I don't know what is.
for the second method, it depends on what values you may have to copy over. several different items might increase the same stat, in which case it would be advantageous to copy them all over to the mob's variable. for example, if several pieces of equipment give you armor, to find your total armor value it would be easier to keep track of the mob's total armor as equipment changes.

the second method is only unsafe if you implement it poorly. but, if you're going to implement things poorly then the first method could be unsafe too.
In response to OneFishDown
I usually do,

mob
var/list/equipment[]
var/item/sword
var/item/shield
Stat()
if(statpanel("Equipment"))
stat("Total Equipment",src.equipment.len)
stat(src.equipment)
item
parent_type = /obj
sword
Click()
var/mob/M = usr
if(src in M.equipment)
if(M.sword == src)
M.sword = null
src.suffix = null
else
M.sword = src
src.suffix = "Equipped"

I store all equipment in a seperate list from the contents, I am not sure if thats the best idea, but it works fine for me.
In response to Elation
Oh yeah. Shame about the big hole it made when it hit the ground.
My method with regards to equipment is similar to Dark Wizard's, with an associative array and a variable or two for short access to certain objects (mainly the player's weapon).