working on rpg but ran into an issue while designing the character classes ex(warrior, thief,priest,wizard)
so the issue I have is priest uses wisdom for his mana and damage and the wizard will use intelligence.
my mana script works with intelligence I could simply add wisdom, however, I only want wisdom to go up if hes a priest, and only intelligence if hes a wizard.
First, i’m not sure what that code’s supposed to do, but you’re adding to current, then changing max and setting current to max. The adding-to-current part therefore does nothing.
You want class inheritance:
class Character {
float wis;
float int;
float maxMana;
void AdjustCurrentMana( int adjMana ) {
}
}
class Wizard : Character {
void AdjustCurrentMana( int adjMana ) {
maxMana = int * 5;
}
}
class Priest : Character {
void AdjustCurrentMana( int adjMana ) {
maxMana = wis * 5;
}
}
...
Character c;
...
c.AdjustCurrentMana( amt );
Since I have a level system, everytime the player levels, he will gain max health, different script, but he would also gain mana, so if it was 50 say it goes to 100 being max, well if he has used 50 mana and leaves him with 50 mana, if he levels up at that moment, the mana would increase to it’s max mana and then if his current mana is not at the maximum, make it maximum. So upon level up, he gains full health and mana.
for the whole messy code:
using UnityEngine;
using System.Collections;
public class PlayerMana : MonoBehaviour {
public int curMana;
public GameObject player;
public int maxMana;
public int wisdom = 10;
public int intelligence = 10;
// Use this for initialization
public void Start () {
curMana = 50;
}
// Update is called once per frame
void Update () {
AdjustCurrentMana(0);
if(curMana < 1)
{
curMana = 0;
}
if(curMana > maxMana)
{
curMana = maxMana;
}
}
void AdjustCurrentMana(int adjMana)
{
curMana += adjMana;
maxMana = intelligence * 5;
curMana = maxMana;
}
void OnGUI(){
GUI.Box (new Rect(20,35,200,20), "MP:" + curMana + "/" + (float)maxMana);
}
}
I’m sure my code is sloppy and stuff doesn’t really belong, but I have not programmed in many years, so trying to get back into it.
basically I have a static health bar defined as 200 length. the code is to increase the maximum value of mana, so say level 1 he has 50 mana, he obtains level 2 now has has a mana pool of 100, the bar does not extend however the bar would change from 50/50 to 100/100. when he levels up if it’s 75/100 when he obtains level 3 it would be 150/150 rather then 75/150.
I pasted the whole code so if there is a more efficient way of doing it, I would be happy to hear about it.
No, you call it on a Character - not from Character.cs. A Character knows whether it’s a Wizard or Priest, but in the Character class file itself there’s no way to tell.
You need to actually use class inheritance. Priest can’t extend MonoBehaviour, it has to extend Character to get Character’s functionality. I gave a code snippet; you’re having issues with it because you’re not using any of it.
If you have my Character.cs, and my Priest.cs, and my Wizard.cs, without any changes at all, then you can do this:
Character player;
function Start() {
if ( playerChoseWizard ) player = playerObject.AddComponent( Wizard );
// and so on
}
function Update() {
player.AdjustCurrentMana(0);
// and so on
}
Because player IS A Character, it has all the functions of a Character. But because it IS A Wizard, and Wizard is a child class of Character, when you call player.AdjustCurrentMana, it will use Wizard’s version of the function instead of Character’s version.