EXTREME Overclocking Forums
Home | Reviews | Forums | Downloads | $ EXTREME Deals $ | RealTime Pricing | Free Magazines | Gear | Folding Stats Newsletter | Contact Us


Go Back   EXTREME Overclocking Forums > Software Discussion > Programming, DBMS, HTML, Web Related
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
Old 06-29-2012, 10:58 AM   #1
Astaldoath
Sith Lord
Astaldoath's Avatar
Senior Member
 
Posts: 716
Last Seen: 05-18-2013
iTrader: 1 / 100%
C# help using a variable from one class/script file in another

Basically what im trying to do is use public static string curClass; as a variable to hold the players class when a button is pushed. Then in the next script file, im using it in a switch to alter the amount of hit points the player gets per point of constitution.

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

Code:
private void SetupVitalModifiers() {
		
		switch (CharacterGenerator.curClass) {
		
			case "Warrior":	//Warrior
				//Health
				GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 2));
				//Stamina
				GetVital((int)VitalName.Stamina).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 1));
				//Mana
				GetVital((int)VitalName.Mana).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Wisdom), .5f)); 
				break;
			
		case "Archer":
				//Health
				GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 1));
				//Stamina
				GetVital((int)VitalName.Stamina).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 2));
				//Mana
				GetVital((int)VitalName.Mana).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Wisdom), .5f)); 
				break;

		case " Rogue":
				//Health
				GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 1));
				//Stamina
				GetVital((int)VitalName.Stamina).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 2));
				//Mana
				GetVital((int)VitalName.Mana).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Wisdom), .5f)); 
				break;
			
			case "Wizard":
				//Health
				GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 1));
				//Stamina
				GetVital((int)VitalName.Stamina).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), .5f));
				//Mana
				GetVital((int)VitalName.Mana).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Wisdom), 2)); 
				break;
			
			case " Priest":
				//Health
				GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 1));
				//Stamina
				GetVital((int)VitalName.Stamina).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), .5f));
				//Mana
				GetVital((int)VitalName.Mana).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Wisdom), 2)); 
				break;
		}
	}


Code:
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public class CharacterGenerator : MonoBehaviour {
	private PlayerCharacter _toon;
	private const int STARTING_POINTS = 350;
	private const int MINSTARTING_ATTRIBUTE_VALUE = 10;
	private const int STARTING_VALUE = 50;
	private int pointsLeft;
	public static string curClass;
		
	// Use this for initialization
	void Start () {
		_toon = new PlayerCharacter();
		_toon.Awake ();
		
		pointsLeft = STARTING_POINTS;	
		curClass = "  None";
		
		for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
			_toon.GetPrimaryAttribute(cnt).BaseValue = STARTING_VALUE;
			pointsLeft -=(STARTING_VALUE - MINSTARTING_ATTRIBUTE_VALUE);
		}
		_toon.StatUpdate();
	}
	
	// Update is called once per frame
	void Update () {

	}
	
	void OnGUI() {
		DisplayName();
		DisplayPoints();
		DisplayAttributes();	
		DisplayVitals();
		DisplaySkills();
		DisplayClass();
	}
	
	
	private void DisplayName() {
		GUI.Label(new Rect(10, 10, 50, 25), "Name:");
		_toon.Name = GUI.TextField(new Rect(65, 10, 200, 25), _toon.Name);
	}
	
	private void DisplayAttributes() {
		for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
			GUI.Label(new Rect(10, 40 +(cnt * 25), 100, 25),((AttributeName)cnt).ToString() );
			GUI.Label(new Rect(115, 40 +(cnt * 25), 30, 25), _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());			
			if(GUI.Button(new Rect(150,40 +(cnt * 25), 25, 25), "-")) {
				if(_toon.GetPrimaryAttribute(cnt).BaseValue > MINSTARTING_ATTRIBUTE_VALUE) {
					_toon.GetPrimaryAttribute(cnt).BaseValue--;
					pointsLeft++;
					_toon.StatUpdate();
				}
			}
			if(GUI.Button(new Rect(180,40 +(cnt * 25), 25, 25), "+")) {
				if(pointsLeft > 0) {
					_toon.GetPrimaryAttribute(cnt).BaseValue++;
					pointsLeft--;
					_toon.StatUpdate();
				}
			}
		}
	}
	
	private void DisplayVitals() {
		for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
			GUI.Label(new Rect(10, 40 + ((cnt + 7) * 25), 100, 25),((VitalName)cnt).ToString());
			GUI.Label(new Rect(115, 40 + ((cnt + 7) * 25), 30, 25), _toon.GetVital(cnt).AdjustedBaseValue.ToString());			
		}
	}
	
	private void DisplaySkills() {
		for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
			GUI.Label(new Rect(300, 40 + (cnt * 25), 100, 25),((SkillName)cnt).ToString());
			GUI.Label(new Rect(405, 40 + (cnt * 25), 30, 25), _toon.GetSkill(cnt).AdjustedBaseValue.ToString());			
		}
	}
	
	private void DisplayPoints() {
		GUI.Label(new Rect(300, 13, 200, 25), "Points Available: " + pointsLeft.ToString());	
	}
	

	
	//private void DisplayClass () {
		//for(int cnt = 0; cnt < Enum.GetValues(typeof(CharacterClassName)).Length; cnt++) {
		//GUI.Button(new Rect(500, 40 + (cnt * 28), 150, 25),((CharacterClassName)cnt).ToString());
		//}
		//GUI.Label(new Rect(500, 12, 60, 25), "Class: "); //left,top,width,height	
	//}
	
	private void DisplayClass () {
		
		if(GUI.Button(new Rect(500, 40 + 28, 150, 25),"Warrior")) {
			curClass = "Warrior";
		}

		if(GUI.Button(new Rect(500, 65 + 28, 150, 25),"Archer")) {
			curClass = "Archer";
		}
		
		if(GUI.Button(new Rect(500, 90 + 28, 150, 25),"Rogue")) {
			curClass = " Rogue";
		}
		
		if(GUI.Button(new Rect(500, 115 + 28, 150, 25),"Wizard")) {
			curClass = "Wizard";
		}
		
		if(GUI.Button(new Rect(500, 140 + 28, 150, 25),"Priest")) {
			curClass = " Priest";
		}
		
		GUI.TextField(new Rect(555, 12, 40, 25), "Class"); //left,top,width,height
		GUI.TextField(new Rect(550, 40, 50, 25), curClass);
	}

}
United States  Offline
    Register to Reply to This Post
Old 08-11-2012, 11:43 AM   #2
karnautrahl
HealerMage
karnautrahl's Avatar
Senior Member
 
Posts: 815
Last Seen: 03-29-2013
Age: 35
From: UK Bristol
iTrader: 0 / 0%
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.
United Kingdom  Offline
    Register to Reply to This Post
Old 08-13-2012, 05:53 AM   #3
sysigy
Abandon All Hope!
sysigy's Avatar
Senior Member
 
Posts: 917
Last Seen: Today
Age: 31
iTrader: 0 / 0%
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; }
This will give you a benefit of making CharacterGenerator.curClass 'strongly typed' and help reduce the likeliness of syntax errors and typo's when dealing with strings.

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;
}
Try that and see if it fixes your problem.

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();
BUT, if you wanted a new class that had a space in the name, for example: "Super Warrior" enums don't allow spaces so the enum element would be 'SuperWarrior = 6' and then attaching the '[Description("Super Warrior")]' to it for the screen friendly text would solve your problem.... almost!

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();
    }
}
Then all you have to do is reference this namespace at the top of your code file e.g. 'using MyGame.Extensions;' with the other 'using' blocks and then you can do the following:
Code:
CharacterGenerator.curClass.ToDescription();

Last edited by sysigy : 08-20-2012 at 04:47 AM. Reason: Automerged Doublepost
United Kingdom  sysigy Folds For EOC!  Offline
    Register to Reply to This Post
Old 08-13-2012, 02:48 PM   #4
CodeWeasel
Rawker Kitteh
CodeWeasel's Avatar
Moderator
 
Posts: 901
Last Seen: Today
Age: 26
From: Off-center
iTrader: 0 / 0%
Awesome post.

Remember: Unnecessary global variables in a strongly-typed OO language make Bjarne Stroustrup cry.
United States  Online
    Register to Reply to This Post
Sponsored Links:
Please Register to Post a Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

All times are GMT -6. The time now is 05:45 AM.

Copyright ©2000 - 2011, Jelsoft Enterprises Ltd.
Powered by vBulletin
Copyright ©2000 - 2011, EXTREME Overclocking