![]() |
![]() |
|||
|
||||
|
|||||||
| Register | Forum Rules | FAQ | Search | Today's Posts | Mark Forums Read |
| Welcome Guest Visitor! Please Register, It's Free and Fun To Participate! | |
|
The EXTREME Overclocking Forums are a place for people to learn how to overclock and tweak their PC's components like the CPU, memory (RAM), or video card in order to gain the maximum performance out of their system. There are lots of discussions about new processors, graphics cards, cooling products, power supplies, cases, and so much more!
You are currently viewing our boards as a "guest" which gives you limited access to view most discussions. You need to register before you can post: click the register link to proceed. Before you register, please read the forum rules. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own pictures, and access many other special features. Registration is fast, simple, and absolutely free! To start viewing messages, select the forum that you want to visit from the selection below. After you have registered and read the forum rules, you can check out the FAQ for more information on using the forum. We hope you enjoy your stay here! Note To Spammers: We do not allow unsolicited advertising! Spam is usually reported & deleted within minutes of it being posted, so don't waste your time (or ours)! |
|
| Please Register to Post a Reply |
|
|
Thread Tools |
|
|
#1 | ||||
|
Sith Lord
Senior Member
|
C# help using a variable from one class/script file in another
I dont get errors but when you put points in vitality ect.. the Health, Stamina, mana doesnt update. It did update fine before i added the switch. I seems like it should work from what i know of programming. Any help is appreciated |
||||
|
|
|
|
#2 | ||||
|
HealerMage
Senior Member
|
Care to zip the source up bud?
I know what unity is, though not used it myself it. Just interested to see if I can debug it for you. I can't see anything just reading it myself, so I'll chuck it into vs for you and look at it properly. Assuming you didn't fix it already. |
||||
|
|
|
|
#3 | ||||
|
Abandon All Hope!
Senior Member
|
First off I noticed spaces in some of your case lines between the ""... eg, " Rogue", this will fail because of the additional space...
" Rogue" does not equal "Rogue". Over the years I've seen .net sometimes have issues comparing strings so I would change your 'player current class' variable from a string to an enum... Code:
public enum PlayerClass : byte
{
[Description("Warrior")]
Warrior = 1,
[Description("Archer")]
Archer = 2,
[Description("Rogue")]
Rogue = 3,
[Description("Wizard")]
Wizard = 4,
[Description("Priest")]
Priest = 5
}
// change CharacterGenerator.curClass type to 'PlayerClass'
public static PlayerClass curClass { get; set; }
Then alter your switch statement: Code:
switch (CharacterGenerator.curClass)
{
case PlayerClass.Warrior:
break;
case PlayerClass.Archer:
break;
case PlayerClass.Wizard:
break;
case PlayerClass.Rogue:
break;
case PlayerClass.Priest:
break;
}
Additional Comment: IMO - I think storing the current class of the player in a global static variable in your 'CharacterGenerator' object isn't a good practice. I would store the current class of the player in your 'PlayerCharacter' object instead so it can be referenced 'playerCharacter.curClass' for example. Additional Comment: Just thought of something else... If you were using 'curClass' as a string so you can output it to the HUD you can still do as such using the new 'PlayerClass' enum by a couple of methods... easiest way is to use: Code:
CharacterGenerator.curClass.ToString(); ![]() The next thing is, "How do I show the description of an enum element?" well, I personally like using an 'extension method': Create an 'Extensions' class somewhere in your project: Code:
public static class Extensions
{
public static string ToDescription(this Enum en)
{
Type type = en.GetType();
MemberInfo[] memberInfo = type.GetMember(en.ToString());
if (memberInfo != null && memberInfo.Length > 0)
{
object attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return (attributes[0] as DescriptionAttribute).Description;
}
return en.ToString();
}
}
Code:
CharacterGenerator.curClass.ToDescription();
Last edited by sysigy : 08-20-2012 at 04:47 AM. Reason: Automerged Doublepost |
||||
|
|
|
|
#4 | ||||
|
Rawker Kitteh
Moderator
|
Awesome post.Remember: Unnecessary global variables in a strongly-typed OO language make Bjarne Stroustrup cry. |
||||
|
|
|
|
| Please Register to Post a Reply |
| Thread Tools | |
|
|