RPG Class Selection Menu
So I i'm making an RPG game and I have 3 classes; warrior, mage, assasin made by me following HardlyBrief Programming's tutorial. He shows how to do it using the GUI system, but I wanna use canvas. Can someone tell me the script for example: if mage class is select, player's class is mage? I know the logic but I don't know how to translate it into C# code. Pls help.
Answer by Mmmpies · Jun 04, 2016 at 06:48 PM
Create a canvas, add a panel as the back ground. Add another panel and scale it so it's big enough to show 1 class. Then add 6 Text items to the panel (that's the name, description and four attributes that Dan showed.
Drag this script onto the panel
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class DisplayClass : MonoBehaviour {
public enum CharType {aMage, aWarrior}; // just a list of names that make sense
public CharType myType; // an enum of CharType = this will show in the inspector
private BaseCharacterClass myClass; // a Base Class to be set based on myType
public Text ClassName; // Just a bunch of public text boxes to drag into the inspector
public Text ClassDescription;
public Text ClassStrength;
public Text ClassEndurance;
public Text ClassStamina;
public Text ClassIntelligent;
void Start()
{
if (myType == CharType.aMage)
myClass = new BaseMageClass ();
else if (myType == CharType.aWarrior)
myClass = new BaseWarriorClass ();
else
Debug.LogError("I have no class at all!");
ClassName.text = myClass.CharacterClassName;
ClassDescription.text = myClass.CharacterClassDescription;
ClassStrength.text = myClass.Strength.ToString ();
ClassEndurance.text = myClass.Endurance.ToString ();
ClassStamina.text = myClass.Stamina.ToString ();
ClassIntelligent.text = myClass.Intelligence.ToString ();
}
}
(the panel with all the texts on it) then select that panel and look in the inspector. You should see a load of empty slots in the script attached to the panel so go ahead and drag each text UI item from the hierarchy onto the empty slots in turn.
Select which class type you want in the inspector. Click play and it should have filled in that class.
Now the good bit :D
Highlight that panel and duplicate it, drag it off to a spare bit of the canvas and from the enum in the inspector select the other class.
I only put 2 in and if you had loads you might want a case statement rather than as if else but with 3 I wouldn't worry about it.
But I need even something for making a class be selected by clicking on the character type image. So for example: There is an image of the warrior character and when it is selected the warrior class will be true.
Easy enough to do. Have your 3 images as the Image element of a button in the UI.
Change that script so ins$$anonymous$$d of Start() you have
public void SetClass(int myTypeNo)
{
if (myTypeNo == 1)
myClass = new Base$$anonymous$$ageClass ();
else if (myTypeNo == 2)
myClass = new BaseWarriorClass ();
else
Debug.LogError("I have no class at all!");
You can delete the enum stuff as we can just do this with an int (you only have 3 classes after all).
On each button click + in the onClick area (again a slot will appear) drag the panel with that script attached to it onto the slot.
From the drop down select DisplayClass -> SetClass and a little Int box appears. In my script I set $$anonymous$$age with 1 and Warrior with 2. So on the $$anonymous$$age button change that int to 1 and on the Warrior to 2.
That's all there is to it.
If in the first script you made I add 3 functions: SetWarriorClass(); Set$$anonymous$$ageClass(); SetAssassinClass();
For each function I set the private bool isWarriorClass, is$$anonymous$$ageClass, isAssassinClass wich I created before ins$$anonymous$$d of the enum, true. And I add the button component to the images and OnClick the function will be called?
For example if I click the Warrior icon I call the SeWarriorClass() function wich says the player's class is a new WarriorClass will it work?
You need to get your head round the event system.
$$anonymous$$ake your functions public then you click + in the OnClick of each button to make the slot appear. Drag the panel with your script attached to it onto that slot and from the DropDown select
YourScriptName -> YourFunctionName
and it'll call that function for that button. So yes it'll work IF you make that function PUBLIC (very important, hence the caps) and you setup the OnClick Event for each button.
Answer by masteproofgamel · Jun 06, 2016 at 09:08 AM
Sry but now I have another problem: When I click the image the class changes but the stats no. So if I click the warrior image the private bool isWarriorClass that I made public changes but the stats don't.
Here's the code: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class UI_Creazione_P : MonoBehaviour {
private BasePlayer newPlayer;
public bool isWarriorClass;
public bool isMageClass;
public bool isAssassinClass;
public Image Guerriero;
public Image Mago;
public Image Assassino;
public Text Nome_Classe;
public Text Descrizione_Classe;
public Text Forza;
public Text Intelligenza;
public Text Destrezza;
void Start()
{
newPlayer = new BasePlayer();
isWarriorClass = false;
isMageClass = false;
isAssassinClass = false;
if (isWarriorClass == true)
{
newPlayer.ClasseGiocatore = new WarriorClass(); // ClasseGiocatore = CharacterClass
}
if (isMageClass == true)
{
newPlayer.ClasseGiocatore = new MageClass();
}
if (isAssassinClass == true)
{
newPlayer.ClasseGiocatore = new AssassinClass();
}
else
{
Debug.Log("NO CLASS");
}
PlayerStats();
}
public void SetWarriorClassTrue()
{
isMageClass = false;
isAssassinClass = false;
isWarriorClass = true;
}
public void SetMageClassTrue()
{
isMageClass = true;
isWarriorClass = false;
isAssassinClass = false;
}
public void SetAssassinClassTrue()
{
isAssassinClass = true;
isMageClass = false;
isWarriorClass = false;
}
void PlayerStats()
{
// Classe giocatore
// Vita giocatore
newPlayer.VitaAttuale = newPlayer.ClasseGiocatore.VitaAttuale;
newPlayer.VitaMassima = newPlayer.ClasseGiocatore.VitaMassima;
newPlayer.VitaMinima = newPlayer.ClasseGiocatore.VitaMinima;
// Energia giocatore
newPlayer.EnergiaAttuale = newPlayer.ClasseGiocatore.EnergiaAttuale;
newPlayer.EnergiaMassima = newPlayer.ClasseGiocatore.EnergiaMassima;
newPlayer.EnergiaMinima = newPlayer.ClasseGiocatore.EnergiaMinima;
// Forza
newPlayer.Forza = newPlayer.ClasseGiocatore.Forza;
// Intelligenza
newPlayer.Intelligenza = newPlayer.ClasseGiocatore.Intelligenza;
// Destrezza
newPlayer.Destrezza = newPlayer.ClasseGiocatore.Destrezza;
Nome_Classe.text = newPlayer.ClasseGiocatore.Nome;
Descrizione_Classe.text = newPlayer.ClasseGiocatore.Descrizione;
Forza.text = newPlayer.Forza.ToString();
Intelligenza.text = newPlayer.Intelligenza.ToString();
Destrezza.text = newPlayer.Destrezza.ToString();
}
}
// forza = strength, // intelligenza = intellect, // destrezza = agility, nome_classe = class_name, descrizione_classe = class_description. ClasseGiocatore = CharacterClass
I wrote it a bit in italian and english sorry.
Well you're setting the bool to true but you're not actually setting the class are you. Think about this.
Start()
runs once when the game starts, it doesn't get called (and it shouldn't get called - even by you) again.
If you want to stick to your way of calling the separate functions then add the set line to each function. Or, as it looks like that script is getting more structure to it (maybe in that tutorial it extends the features), you need to just show the stats and call that newPlayer base class when you click on an Accept button to set your player.
Got it work but I have still a stupid problem: When I click the warrior and assassin image the class name doesn't appear ins$$anonymous$$d, for the mage it works.
Well you'd be better off creating a separate function and calling that. No real need to call it every single frame (which Update does) it only needs to call when a button is clicked.
How you show your game is up to you but if you want to show the base class then you don't need to set the player up till you click a Create button.
If you just want them to click the one you choose and it just creates the the player then it's O$$anonymous$$ but I think you'll want to show the class information and then have a separate function to create.
So when your function gets called set the bools then call a ShowClass() function to grab that class's base information.
When you click the Create button actually set the player to be the currently selected base type in another function.
Hope this makes sense.