Building Scripts on each other
So, I'm making an RPG and I'm wondering, should I have lot of separate scripts and reference them as I need it or is there a better way? So like my Level up script:
private int experienceToNextLevel = 20;
public int currentExperience = 0;
public int attackPower;
public int defencePower;
public int HP;
public int Level;
// Use this for initialization
// Update is called once per frame
void Update () {
if (currentExperience == experienceToNextLevel) {
Level++;
attackPower *= 2;
defencePower *= 2;
HP *= 2;
experienceToNextLevel *= 3;
Debug.Log (experienceToNextLevel);
}
}
And then I'm wondering how to implement my Attacking script. Should I reference this script every time the player attacks to see how much damage the player gives based on his current level or should I work the attacking logic here so I don't have to keep grabing these varibles into another script.
Sorry if that sounds confusing and thanks in advance =)
Answer by Chubzdoomer · Jan 29, 2017 at 03:19 AM
I think you're definitely headed in the right direction, and this is how most people seem to do it. And although you're referring to this script as a Level Up script, it actually looks more like a Player Stats script since it also stores and manages the player's health, attack, and defense. That's a great starting point for an RPG.
What you said about your Attack script should work fine. You can get references to scripts really easily to communicate amongst them. I think another way is to use an event/messaging system, but references are probably the quickest and easiest way if you're a beginner.
One great thing about breaking your scripts up into multiple smaller ones is that it's far easier to maintain them. If something goes wrong, for example, you can just debug one script that handles a very specific task versus scrolling endlessly through a massive script that tries to do a hundred things at once.
Alrighty, thanks ^_^ This cleared up a few things for me, just one more question, won't grabbing another script's values all the time slow down the performance?
It shouldn't, especially if all you're doing is getting a reference in the Start/Awake function (or via the Inspector) and dealing with simple data types like ints, floats, strings, etc.
Alright ^^ Thank you so much for your input!