- Home /
How to make classes?
Hi i was wondering how you could make classes for your character or just have it so that at the char creation screen you can choose a preset thing of stats thank you!!!!
Are classes here program$$anonymous$$g classes, or are they classes in an RPG style game?
Please ask a specific question. This is way too general to be answered here, except by saying "You make classes by program$$anonymous$$g them, and it depends on the structure of your game", which probably doesn't help much.
Answer by Yoerick · Jul 19, 2011 at 07:21 AM
If you want to start using classes, the best approach would be C# coding. In that case this is how you write a class:
public class Character
{
//some example variables
private float strength;
private int charID;
private float health
//example constructor which sets the default health
public Character(float hp)
{
health = hp;
}
//some example functions
public void someFunction()
{
//do stuff
}
public int getCharID()
{
return charID;
}
}
since you talked about a character and stats I wrote this example with related variables you might be able to use. If you want to make a character you just use Character char = new Character(100); This makes a new character with 100 health for example
Note: if you want to add the script to an object in the scene, the class should derive from MonoBehaviour like so:
public class Character:MonoBehaviour
{
...
}
in this case you shouldn't use "Character char = new character(100)" (for example) but just drag the script onto the object.
if you want to make an interface to set the stats of the character you should use the OnGUI function (preferably in another class since this is a seperate functionality, for example a class Interface):
public class Interface
{
void OnGUI()
{
// some GUI elements
}
}
for GUI scripting I refer to this link: http://unity3d.com/support/documentation/Components/gui-Basics.html
Hope this helps ;)
I applaud your effort, but I get the sinking feeling that this is an uphill battle :)
Aww. Don't lose hope! :-D I've +1'ed both this answer and its comment, because I, too, applaud the effort, and the comment made me smile. :)
Well, I'm guessing he's quite new to program$$anonymous$$g so I just wanted to give him a little support on the program$$anonymous$$g basics and how they work in Unity. Though I think he's got a long way to go if he doesn't know how to program classes.. But if he is deter$$anonymous$$ed to learn, he'll get there :)
I would upvote it as well, but you should fix the errors :D
the
class
keyword is lower-casea class don't have brackets behind the class-name
thanks for pointing that out ;) I didn't write the code in a text editor but in the answer itself and I probably went over it a little too quickly, those were stupid mistakes :p
Answer by CHPedersen · Jul 19, 2011 at 10:50 AM
There is no global definition of a "Class" that all RPGs use to store their presets. It could be anything you define yourself. What is an RPG "Class" in your game? And by asking, I don't want you to answer me, I want you to ask yourself. Is it the traditional set of Wizard, Fighter, Archer, Thief, Healer, etc? Then, what defines a class? The numbers in Intelligence, Strength, Dexterity and Wisdom? If that is the case, then your preset for a Wizard could be as simple as a textfile called "Wizard" that contains the lines,
Strength: 8 (or something low, for a wizard)
Dexterity: 9 (or something low, for a wizard)
Intelligence: 18 (or something else high, for a wizard)
... You get the point.
Then you can load that into Unity using TextAsset (see http://unity3d.com/support/documentation/ScriptReference/TextAsset.html) and then parse the numbers out of the file. That would be one way to save a preset. On the technical side, there are multiple ways of doing so. You don't have to save it in a textfile, it could also be hardcoded values in your source code, or it could be rows in an SQL table, to think broadly.
On an unrelated note, I get the impression this is a classic case of "I just started Unity, now let's make a game that beats World of Warcraft in a week". ;) Don't take this the wrong way, I'm not trying to shoot you or your idea down. But you might benefit a lot from following some of the many tutorials out there, and then maybe coding a smaller project first. Having done so, you'll realize completely on your own that storing and loading character presets is pretty simple.