ID:1657039
 
(See the best response by Kaiochao.)
Code:
GettingPassives(var/Passivetree/P)
var/Passivetree
paspoints
Unlocked_Passives
if( (P.name in src.PassiveTreeList) )
if(P.JutsuMaxLevel==P.JutsuLevel)
src << "You have already mastered this!"
if(P.JutsuMaxLevel > P.JutsuLevel)
if(src.Skill_Point < paspoints)
src << "You don't have the required skill points for this!"
else if( alert("Would you like to level up [P.name] for [paspoints] skill points?","Current Level: [P.JutsuLevel]/[P.JutsuMaxLevel]", "Yes","No") == "Yes")
P.JutsuLevel+=1
src <<"You have leveled up [P.name]!"
src.Skill_Point = max(0, src.Skill_Point - paspoints)
SavePassives(P)
else if( !(P.name in src.Activated_Passives) )
src <<"This Passive is not available to you at this time."

else if(src.Skill_Point < paspoints)
src << "You don't have the required skill points for this!"

else if( alert("Obtain [P.name] for [paspoints] skill points?","Skill Obtain","Yes","No") == "Yes")
src << "You have obtained [P.name]!"
src.PassiveTreeList += P.name
for(var/A in Unlocked_Passives)
if( !(A in src.Activated_Passives) )
src.Activated_Passives += A
src.Skill_Point = max(0, src.Skill_Point - paspoints)
src.contents += new P.pass_obtained
src.SkillBoosts()
src.Refresh_Skilltree()


Problem description: Well what I got here is basically a skill tree system that when someone clicks an item it allows the user to gain a passive. That all works but the problem starts at the beginning when I tell the proc to level up the passive, which it does successfully. My only problem is that the variable that shows the level of the Passive is under the Passive itself, and I don't know a way to save it to the mob. Whenever the mob logs out it forgets that the passive had ever leveled.

So my question is how would I save a variable that is located in a passive to a mob's save system?


Best response
The "Passive" object is automatically saved if it's stored in a variable of the mob, such as a direct reference or a list. For example, if you put P in PassiveTreeList instead of P.name, then PassiveTreeList (if it's not a tmp variable) would keep all the /Passivetree objects that it contains when the mob is saved normally.

This is how items in a player's contents are saved and loaded automatically.
Something went wrong, I changed

 if( (P.name in src.PassiveTreeList) )

to
 if( (P in src.PassiveTreeList) )


and

  src.PassiveTreeList += P.name


to

 src.PassiveTreeList += P


And now the Passive doesn't have an effect at all. I don't understand how such a simple change would do that.
In response to The Infinite Flame
Are there other places where you expect the name of a passive in PassiveTreeList?
I fixed the first problem and yeah there was.

But now for some reason when I relog it's like P disappears from the PassiveTreeList and as a result it won't allow me to level up a Passive after relogging. So let's say if I relogged with the passive at level 3/5 when I relogged I'd still have the stats from 3/5 but when I click the Passive and try to increase it to 4/5 it just skips the first if statement as if P is no longer in PassiveTreeList and instead goes to the next statement.