- Home /
A quick question C#
So I'm trying to program a questing system.... And i have a question i want to program a quest definition file in the sense i can enable the bool when i'm programming a quest and i suddenly have access to several different variables.. How do i do this in C#? I already have a quest code going i just need help
using UnityEngine;
using System.Collections;
public class Quests : MonoBehaviour {
public static bool Questing;
// Update is called once per frame
public static void OnGui () {
// Is Questing Allowed?
if(Questing == false){
GUI.Box(new Rect(10,10,10,10),"No Quests!!!");
}
if (Questing == true){
float timer;
Money.enabled = true;
}
}
void rewards () {
}
}
define already your different variables and just add onto them and when you are done just nullify them till you need them late again.
$$anonymous$$ore information about your goals would be helpful. For instance, what information do you want access to when questing? When you say "lots of variables" it makes me think that you might want to define a class for a quest in progress that has some success condition and some variables to track progress.
This is unrelated to your question, but just a general coding tip.
if(Questing) //is the same as if(Questing == true)
if(!Questing)//is the same as if(Questing == false)
Answer by Em3rgency · Jul 02, 2013 at 03:07 PM
Ok I understand your problem now. You should read up on variable scope http://www.codecandle.com/articles/csharp/variables/variable-scope.html
You set up the questing bool as a public variable inside your class. While you set up float timer as a variable inside a method(function) of your class.
Nothing outside a class can see whats inside a classes functions, they can only see whats set to public in the class itself. So, to access the timer, change your code to this
using UnityEngine;
using System.Collections;
public class Quests : MonoBehaviour
{
public static bool Questing;
public float timer;
public static void OnGui ()
{
// Is Questing Allowed?
if(Questing == false)
{
GUI.Box(new Rect(10,10,10,10),"No Quests!!!");
}
if (Questing == true)
{
Money.enabled = true;
}
}
void rewards ()
{
}
}
However, this is considered bad programming practice. Ideally, you want to make all the variables private and create get/set public methods for each one.
using UnityEngine;
using System.Collections;
public class Quests : MonoBehaviour
{
private bool Questing;
private float timer;
void OnGui ()
{
//Shortened code
}
void rewards ()
{
}
public bool getQuesting()
{
return Questing;
}
public float getTimer()
{
return timer;
}
public void setQuesting(bool newValue)
{
Questing = newValue;
}
public void setTimer(float newValue)
{
timer = newValue;
}
}
Now the only way to read/change the private classes variables are by using special public methods. This ensures that you never make any mistakes, and is just considered proper coding.
Oh ok that makes so much sense... I get it so ins$$anonymous$$d of having one
variable bool questing that loads a giant pile of variable it's better practice and actually works to have separate class variable that I can load in separate pieces. So if I decide to have a timer I just load the timer class if i want questing to be true i just load the questing class...
Wait... what? They're in the same class. The good practice is making a FUNCTION to get/set them ins$$anonymous$$d of doing it directly. Questing and timer are still variables, not classes. Quests is the only class here.
I think you misread my answer. The 2 codes I posted, are the same code. First one is what you wanted, without anything extra. The second one is how it SHOULD be done, with the extra functions.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help on a basic script 2 Answers
Complete beginner. Where to start? 0 Answers
Missile launcher is malfunctioning 2 Answers