- Home /
Is it possible to link character skill lists to a GUI, and if so, how?
In our game, characters are able to level up, and when they do they are presented with options of some skills to choose from, but not everyone will end up with the same skills(as the option of which skills you want to learn is different for each character/player). My question is, how could I display a list of the skills they have unlocked with their character on a GUI, so that they are able to see the available skills in combat.
Currently my GUIs are all built through the OnGUI function.
I'm not an experienced programmer, so if you could, please explain simply, or at least in a way that I can further research
Answer by enmeii · Mar 06, 2013 at 06:21 AM
I suggest you buy a plugin which named "NGUI". There are some awesome examples which is like your idea. Talent tree?
Looks like a great plugin, and I thank you for the recommendation but I'd prefer to learn how to do it myself. I'm rather new to program$$anonymous$$g, and I need to learn. I've been trying to work out the solution to this for a few days now on my own but have been having trouble, so here I am.
Again, thank you for the recommendation.
Answer by Chronos-L · Mar 06, 2013 at 05:59 AM
// I assumed that you have a interface/abstract class called Skill
// Because you mentioned that not every character has the same skill,
// so I made this assumption that you are taking this approach.
public class Skill {
public string name;
...
...
}
public class Example : MonoBehaviour {
//It doesn't matter as long as it is a collection of skills
public Skill [] skill;
//You can either leave it as a function or put it in your OnGUI
private string[] GetSkillName() {
string [] str = new string[skill.Length];
for( int i = 0; i < skill.Length; i++ ) {
str[i] = skill[i].name;
}
}
void OnGUI() {
string [] skillName = GetSkillName();
//Display the skills
}
}
This is a very basic solution, much more work are needed if you need more than displaying a list of skill.
Yes, the skill list is basically being listed as an "available actions" type of menu where you choose your skill.
After reading your first comments, I was curious to why you'd assume I'd be using a interface for this so I did a long reading session as I have never used an interface inside any of my other projects(though up until now my projects have all been simple projects).
As a "newbie" to Unity, and program$$anonymous$$g in general there are a lot of things I simply don't know about, I guess you could say.
As you said, your solution is a very basic solution, being that it would only show the skill names, and no functionality to it other than that. I'm not familiar with C#, but from what I'm getting in your OnGUI, it is returning a string of each skill name the skill[] iterates through? How is this being displayed onto the screen though the GUI, just as text?
To display the skills as text, you can use GUILayout, GUILayout is automatically aligned & positioned while GUI is manual.
void OnGUI() {
string [] skillName = GetSkillName();
//$$anonymous$$anually adjust the position/size of the area
GUILayout.BeginArea(new Rect(10, 10, 100, 100));
foreach( string s in skillName ) {
GUILayout.Label( s );
}
GUILayout.EndArea();
}
$$anonymous$$ore on GUILayout
I am talking about interface because you are doing Skill. Usually skill for different character has different effect/codes, so the norm is to use a interface/abstract class to generalize your skill (name, $$anonymous$$P cost, description etc).
It is even possible to not using a interface/abstract class, then you are looking at delegates, which is quite useful when you need variations in code between objects of the same class.
Answer by gfvfubb · Mar 06, 2013 at 06:13 AM
So how is it you are storing your skills? In a Dictioary? An Array? What? When the user is presented with the option to level up a skill, what is he doing?
The simplest way to do this is to have a generic skill dictionary or hashtable look up where you save indexes of which skills the player has and then print those indexes out later in the GUI label showing active skills. Or you could just push the skill names to an array if you are really lazy and use .ToString() on the Array.
If you have unique skill objects you can also use dictionary index lookups for them as well instead of having to assign them and call a GetName function as in the other answer.
I haven't yet decided which solution would be the best way to store and display my skills. I see you suggest using a dictionary or hashtable.
This might be a bit much to ask, but could you explain the use of a dictionary to store all my skills to me like I'm a child?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Items with Statistics(such as attack damage) that actually effect the character? 2 Answers
Sprint Error script? 2 Answers
Need help with my script 0 Answers