- Home /
How to connect multiple stats scripts for the actual character stats?
Necessary information: I am making this RPG in unity using C# scripts. This RPG includes character stats, based on spendable stat points gained each level, but these character stats are also influenced by armour, character level, etc. So, my idea was to make different scripts which will get the stats for the different aspects, for example:
A script which will "get" the stats of the armour the player currently wears
a script which will "get" the stats of the weapon the player currently wears
a script which will "get" the stats when the player gets a buff
a script which will "get" all of the spend points in a stat
and so on.
Eventually these scripts all influence the actual stats of the player (lets say a hotkey "C" is pressed to lookup the character stats), so they must be connected together. Now my question is, how will I actually connect all those scripts in 1 script named: "calculate stats", so I can inherit from it using a script called: "actual stats". Will I have to use interfaces to inherit from them all? I hope my question is clear and somebody can give me a proper answer...
Answer by getyour411 · Aug 12, 2015 at 11:12 PM
Not an argument against "Inheritance" but there's nothing that dicates its a must here. The Armor class can call a method in the CalcStats class whenver armor is equipped, repaired, enchanted, removed,etc as can all the other external classes.
Armor Class
private CalcStat calcStat;
Start()
calcStat = GameObject.FindObjByTag("Player").GetComponent<CalcStat>();
(methods in Armor can call CalcStat)
calcStat.BuffStrMod(10);
calcStat.BuffDexMod(10);
calcStat.weight = weight + item.weight;
etc.
Thank you so much for your reply! It didnt come to my $$anonymous$$d to tackle the problem this way, and I will definitely try this out :').
One little question though, do you have an example when I should use inheritance over the way u just described here? And if it does not bother u, could you give an example of the same problem solved with inheritance? I guess there are multiple ways to do it, but I often find it hard to make the right choice at the right moment and I'm curious what it will look like when inheritance is actually used...
Your question implies you aren't familiar with inheritance, that's why I suggested an alternate route which might make your code maintenance easier until you have picked that up. There are some good examples of potential Inheritance candidates in your post though. There's a good tut from Unity on the subject here
https://unity3d.com/learn/tutorials/topics/scripting
Intermediate / Inheritance (and Interfaces).
To go from Coder 101 to Coder 201, I highly recommend spending time with those vids.
Your answer
Follow this Question
Related Questions
Unity networking create & join with custom character?, 0 Answers
Rigidbody player keeps moving after AddForce has stopped? 3 Answers
Character selection 1 Answer
Cloth Component in Unity 5 1 Answer