How to reference a monobehavior class in abstract class - c#

I recently decided to make a command console for my game, and then proceeded to make some groundwork. My issue is I cannot use it to change any relevant variables, as I have gotten stuck trying to get references to the classes where said variables are stored.
I have an abstract class for my command:
public abstract class Command
{
public abstract void Execute(string[] args);
}
Then I have a class deriving from above class for my command
public class RunesAdd : Command
{
public override void Execute(string[] args)
{
int number;
if(args.Length == 1 && int.TryParse(args[0], out number))
{
Debug.Log(number);
RunCtr.runes += number;
}
else
{
ConCtr.addLogEntry("Incorrect syntax, correct syntax is: runes.add <runes>");
}
}
}
and finally my registry of commands
public class CommandRegistry
{
private Dictionary<string, Command> _commands;
public CommandRegistry()
{
_commands = new Dictionary<string, Command>();
}
public void RegisterCommand(string name, Command command)
{
if (_commands.ContainsKey(name))
{
Debug.Log("Created command already exists");
}
_commands[name] = command;
}
public void RegisterAllCommands()
{
RegisterCommand("testcommand", new TestCommand());
RegisterCommand("runes.add", new RunesAdd());
}
public bool ExecuteCommand(string commandName, string[] args)
{
if (_commands.ContainsKey(commandName) == false)
return false;
_commands[commandName].Execute(args);
return true;
}
}
My problem is that I am unable to get a reference to my class with the variable for runes. I first tried to get a reference to the class in the Command class, so that those variables would be available in all children, but in order to do that I must make a method to actually assign those references, which would look like this:
public void GetReferences()
{
controllerObject = GameObject.FindGameObjectWithTag("Controller Object");
RunCtr = controllerObject.GetComponent<Runes_Controller>();
ConCtr = controllerObject.GetComponent<Console_Controller>();
}
The issue here is that since I cannot get a reference the Command class (due to it being abstract) in any of my monobehavior scripts which have the void Start() method, I cannot actually execute this method to assign the references. I then tried to make another class called GetReferences, which looks like this:
public class GetReferences
{
public GameObject controllerObject;
public Runes_Controller RunCtr;
public Console_Controller ConCtr;
public void GetReferencesMethod()
{
controllerObject = GameObject.FindGameObjectWithTag("Controller Object");
RunCtr = controllerObject.GetComponent<Runes_Controller>();
ConCtr = controllerObject.GetComponent<Console_Controller>();
}
}
Then I made the Command class derive from my GetReferences class, called the GetReferencesMethod() from a monobehavior script on start. Doing this I no longer get an error for not having assigned my classes to references, but whenever I try to edit the values it just does nothing. I have been searching the web for 2 hours now, but no dice. If I explained myself poorly please let me know. Any help is much appreciated, and thanks in advance!

Ok from what I understand is that you are trying to get your Command class using the GetComponent<> method. I might be wrong on this, so correct me if I am wrong.
If it is, then the issue is GetComponent<> only works with MonoBehaviour derived classes. Meaning you have to implement your class as a MonoBehaviour, which should be as simple as this:
public abstract class Command : MonoBehaviour {...}
EDIT
After reading your comments I believe you can use of the a Singleton pattern.
If you place your RuneController & CommandController on the same object and add another class called GameManager or InGameManager.
Then you can use a singleton pattern to access it.
public class GameManager
{
public GameManager Instance { get; private set; }
public RuneController RuneController { get; private set; }
public CommandController CommandController { get; private set; }
void Awake ()
{
// If there is an instance, and it's not me, delete myself.
if (Instance != null && Instance != this)
{
Destroy(this);
}
else
{
Instance = this;
}
}
void Start()
{
this.RuneController = GetComponent<RuneController>();
this.CommandController = GetComponent<CommandController>()
}
}
So the usage will look as follow:
GameManager.Instance.RuneController.Execute(command);

Related

Trying to change BattleState globally

Hi I'm a completely new to coding and am trying to create a card game. I've watched some tutorials and tried to take things into my own hands but cant seem to figure out something. I currently have a BattleState set up;
public enum BattleState { START, PLAYERMAINPHASE, PLAYERBATTLEPHASE, PLAYERENCORESTEP, ENEMYTURN, WON, LOST }
and would like it so when i change the BattleState with a script, it changes it for every other script that references this BattleState. Sorry for the bad wording. Coding is rough :/
You can use interfaces, create an interface such as IBattleStateChanger and have a method on it
interface IBattleStateChanger{
void ChangeBattleState(YourClass.BattleState state);
}
Then on every script you want the value to change implement this interface as
ClassExample : IBattleStateChanger {}
This will then force you to create a method in the script to change the state
After that, whenever you want to change the value globally on the scripts where you implemented this interface, you can do a foreach loop finding each type of this interface such as
BattleState newState = BattleState.START;
foreach (var obj in FindObjectsOfType<IBattleStateChanger>){
obj.SetBattleState(newState);
}
You could use a static event and attach listeners/callbacks to it like e.g.
public enum BattleState
{
START, PLAYERMAINPHASE, PLAYERBATTLEPHASE, PLAYERENCORESTEP, ENEMYTURN, WON, LOST
}
public static class BattleStateMgr
{
private static BattleState _state;
public static BattleState State => _state;
public static event System.Action<BattleState> OnStateChange;
public static ChangeState(BattleState s)
{
_state = s;
OnStateChange?.Invoke(_state);
}
}
public class OtherScript : MonoBehaviour
{
private void Awake()
{
BattleStateMgr.OnStateChagne += OnBattleStateChange;
}
private void OnDestroy()
{
BattleStateMgr.OnStateChagne -= OnBattleStateChange;
}
private void OnBattleStateChange(BatlleState newState)
{
Debug.Log($"Changed Battle State to{newState}", this);
}
}
I believe you are confused about the scope of your variable. Each script you place an instance of the enum Battlestate, is a local version of that enum. If you want the reference to be global, you will need to have a central point where all scripts can grab this reference.
public class BattleManager : MonoBehaviour
{
private BattleState battleState;
// setter / getters
public BattleState GetBattleState(){return battleState; }
public void SetBattleState(BattleState state){ battleState = state; }
}
You are going to want to make a single script that holds the only reference to your enum Battlestate, then have your other scripts reference the variable.
public class OtherScript : MonoBehaviour
{
// assign this reference in the inspector
[SerializeField] private BattleManager bm = null;
private void YourFunction()
{
if(bm.GetBattleState() == BattleState.randomStateHere)
{
// run logic here
}
}
}
There are a number of ways to go about doing this, but the easiest would most likely be by declaring the variable static.
public class BattleManager : MonoBehaviour
{
private static BattleState battleState;
// setter / getters
public static BattleState GetBattleState(){return battleState; }
public static void SetBattleState(BattleState state){ battleState = state; }
}
public class OtherScript : MonoBehaviour
{
private void YourFunction()
{
if(BattleManager.GetBattleState() == BattleState.randomStateHere)
{
// run logic here
}
}
}
I do not know how many scripts you need to access this variable, but if it is only a handful, I would instead assign references to the script that holds the enum to each of the scripts that need it. I would avoid simply using static as it is the easy approach but creates what is called a code smell. The reason for this is OOP (object-oriented programming) by design should generally not have mutable global variables.
If you have a single instance of an object that manages all of your battle activity and a lot of scripts need to access it, you can look into the Singleton pattern. As you are new to programming, I would not implement this pattern until you understand the time and place to properly use it. You can also completely avoid using it by properly assigning the references you need in the inspector or by using a Object.FindObjectOfType in either Start or Awake.

Creating System.Object instance in Unity

I'm experiencing a sort of bug in Unity, probably due to the fact I'm almost new to it:
I have a MonoBehaviour object that correctly lives in memory.
For sake of code organization, this object have two members of standard System.Object classes which needs to be created by a new call.
class A
{
// ...
}
class B
{
// ...
}
class Status : MonoBehaviour
{
A m_AVar;
B m_BVar;
public A AVar
{
get {return m_AVar;}
protected set { m_AVar = value; }
}
public B BVar
{
get { return m_BVar; }
protected set { m_BVar = value; }
}
void Awake()
{
// SingletonImplementation
}
void Start()
{
m_AVar = new A();
m_BVar = new B();
}
At some point in the game someone decides to call my Status.ExecuteSomeAction():
public void ExecuteSomeAction()
{
AVar.DoSome();
BVar.DoSomethingElse();
}
and everything go fine. While at the end by a UIButton.OnClickEvent:
public void ExecuteOnClickAction()
{
AVar.Foo();
}
But no matter what AVar result null. Reading left and right I have the feeling that there's something under the hood with those System.Object which I still don't get.
Where am I doing wrong?
I had a similar problem myself some time ago.
When the singleton pattern creates a new instance of this class (Status) then the links to the UI-Objects won't be created.
To solve this problem create a class which is responsible for handling the UI. This class will then call your Status class.
Another way is to change the singleton pattern to your needs. In my case I simply wrote this:
public static Status Instance { get; set; }
public Awake()
{
Instance = this;
}
This may be a bit sloppy because I will get problems if there is more than one instance of this class but it does the job.

Inheritance in c# and object creation in c#

I have classes as follow one is SuperClass which is inherited by ChildClass and Child1Class
public class SuperClass
{
public new int Superclassprop = 2;
public virtual void play()
{
Console.WriteLine("SuperClass");
}
}
public class ChildClass : SuperClass
{
public new int Childclassprop = 2;
public override void play()
{
Console.WriteLine("ChildClass");
}
}
public class Child1Class : SuperClass
{
public new int Childclassprop = 3;
public override void play()
{
Console.WriteLine("Child1Class");
}
}
Now when i create an object something like below i don't understand what is the difference between these. i had read a huge bunch of blogs related to this but i didn't find any justifiable answer please help me to understand what actually is happening here or suggest me a good blog or article including on SO where i can understand a whole concept behind this why we need this where the actual real time use of these concept?
SuperClass obj = new SuperClass();
SuperClass obj1 = new ChildClass();
I have attached screenshot of watch which is generating on Run-Time why there is a obj1 consisting all properties but i can access only SuperClassprop?
Thanks in advance any help will be really appreciated.
Here is the more practical example of your topic:
using System;
public class Music
{
public virtual string play()
{
return "Play Music";
}
}
public class Drum : Music
{
public override string play()
{
return "Play Drums";
}
}
public class Piano : Music
{
public override string play()
{
return "Play Piano";
}
}
public class PlayMusicService
{
private readonly Music _musicContext;
public PlayMusicService(Music musicContext)
{
this._musicContext = musicContext;
}
public string PlayAlbum()
{
return _musicContext.play();
}
}
public class Program
{
public static void Main()
{
string whatPlayed = "";
Drum drums = new Drum();
PlayMusicService music1 = new PlayMusicService(new Drum());
whatPlayed = music1.PlayAlbum();
Console.WriteLine(whatPlayed);
Piano piano = new Piano();
PlayMusicService music2 = new PlayMusicService(new Piano());
whatPlayed = music2.PlayAlbum();
Console.WriteLine(whatPlayed);
}
}
Output:
Play Drums
Play Piano
i don't understand what is the difference between these.
One of the main differences is the constructor call
SuperClass obj = new SuperClass();
SuperClass obj1 = new ChildClass();
In the case of obj1 the ChildClass constructor is called after the SuperClass constructor and the field and property initialisation is done also for the property Childclassprop
consisting all properties but i can access only SuperClassprop?
The variable obj1 is still of type SuperClassprop so at compile time you are only allowed to see and use those variables that belong to this class. If you want to actually access the variables of ChildClass you will have to cast it to the proper type:
var r = (obj1 as ChildClass).Childclassproput;
why we need this where the actual real time use of these concept?
One scenario that comes to my mind is : it might be that at compile time it is not clear which class has to be instantiated. But this is decided at runtime. But you need already a variable to write the call of the specific play() method. At runtime it will be decided which method is called in the end.
SuperClass obj = new SuperClass();
bool condition = false;
if (condition)
{
obj = new ChildClass();
}
else
{
obj = new ChildClass1();
}
// now just call the method and the proper method will be called
obj.play();

specify a object on a public method

I have the following method
public partial class formTabelasPsi : Form
{
private Form1 Opener { get; set; }
public formTabelasPsi(Form1 opener)
{
this.Opener = opener;
InitializeComponent();
}
public static void publicmethod1(string path)
{
//some code related to path
}
}
I want publicmethod1 to check a checkbox whenever this formTabelasPsi runs it.
I tried to specify it using formTabelasPsi.checkBox1.Checked = true; but the code says a object reference is required.
Maybe this is a newbiez question for most of you, but honestly, as a amateur programmer I didn't find this clearly anywhere.
The checkbox belongs to an instance of that form, you need to reference that instance in order to update it
public void publicmethod1(string path)
{
this.checkBox1.Checked = true;
}
The method also needs to belong to an instance of the form, you can find out more about instances here

how to findout which Class/Object is calling current winForm/object in c#

hey guys, i have 3 winForms named carForm,parForm and updateForm, so there's updateForm.show() method in both carForm n parForm, while m in updateForm i want to know which class/form has called updateForm, so that i can update the respected class db. Currently i'm setting up a public global variable to verify that which form is calling updateForm..but i was thinkin' is there's another way to do this, i guess Reflection can solve this issue, but i'm not able to solve it, here's my code
///carForm
public class carForm:Form
{
Program.globalvariable="CAR"; //global variable
UpdateFrom updateForm=new UpdateForm();
updateForm.Show();
}
///parForm
public class parForm:Form
{
Program.globalvariable="PAR";
UpdateFrom updateForm=new UpdateForm
updateForm.Show();
}
///updateForm
public class updateForm:Form
{
if(Program.globalvariable=="CAR")
///code for update CAR db table
else if(Program.globalvariable=="PAR")
///code for update PAR db table
Type obj = GetType(); //This is what i was tryin' using Reflection but giving error
}
so if i get the calling Class/Objects info, i can update respected DB table,
can ne1 know hw to do this with Reflection,
Put the argument in a constructor of updateForm
///carForm
public class carForm:Form
{
UpdateFrom updateForm=new UpdateForm("CAR");
updateForm.Show();
}
///parForm
public class parForm:Form
{
UpdateFrom updateForm=new UpdateForm("PAR");
updateForm.Show();
}
///updateForm
public class updateForm:Form
{
private readonly string _key;
public updateForm(string key)
{
_key = key;
}
public void SomeMethod()
{
// check for _key here.
}
}
Edit:
If you want to have the actual type you can pass it directly, no need for reflection.
///carForm
public class carForm:Form
{
UpdateFrom updateForm=new UpdateForm(this.GetType());
updateForm.Show();
}
///parForm
public class parForm:Form
{
UpdateFrom updateForm=new UpdateForm(this.GetType());
updateForm.Show();
}
///updateForm
public class updateForm:Form
{
private readonly Type _type;
public updateForm(Type type)
{
_type = type;
}
public void SomeMethod()
{
// check for _type here.
}
}
Edit 2:
But in general, passing the type like this smells like bad code. Your control flow will probably end up like a bowl of spaghetti.
If you want the updateForm to update some values on the other forms you should
Send all relevant information about what questions/titles/etc to show in the updateForm in the constructor of the updateForm.
In the updateForm, save relevant "answers" to public properties of updateForm
Set DialogResult in updateForm to OK or Cancel depending on how you exit updatForm
Call updateForm like this: if (updateForm.ShowDialog == DialogResult.OK) {// read all properties from updateForm}
You'd probably have to look at the StackTrace class if you wanted to automatically get that kind of information. Not sure about the performance of using it though...

Categories