Guys I’m struggling with a concept here, can someone enlighten me please.
I have a gen.cs file which say contains a function and some variables I want to use in every class in my game.
So I tell each class to base itself on gen class. Gen bases itself on MonoBehaviour so looks like this as:-
in Gen.cs
public class Gen : MonoBehaviour
{
public bool usingmouse = false;
public float NativeVerticalResolution;
public void GenStart()
{
NativeVerticalResolution = 768.0f;
usingmouse = true;
}
}
Then in every class I declare them as this:
public class LevelSelectScene : Gen
{
void Start()
{
GenStart();
}
}
The problem is, if I don’t call GenStart in each class the variables in Gen are not set up. I thought the act of subclassing Gen would have been enough. Why do I need to call GenStart in every class? What am I doing wrong?
If I have say 10 classes all derived from Gen does that mean I have 10 NativeVerticalResolution floats all taking up duplicate storage space?
Well I just like to have a load of helper functions and some handy global variables that every class can use.
I want the gen class to initialise just once for the entire app and then all classes can use functions from within it.
for example I have my own debug code, I like to set up am enum for what the device actually is etc. I even have my
own 2d point structure all in gen.cs
I want it to funtion abit like in standard C where you might include a misc.h or misc.c file to be a bucket for all your usefull
stuff you use everywhere that is class independent.
Alright then that’s what I taught you wanted todo.
Well instead of using inheritance for an utility class your much better of using a static utility class (Singleton)
That way you would just do
public static class Gen
{
public static bool usingmouse = false;
public static float NativeVerticalResolution = 768.0f;
public static void DoSomething(bool input, string name)
{
// Do Something Useful
}
}
Then in your game code you would do something like this:
public class LevelSelectScene : MonoBehaviour
{
void Start()
{
// Do level specific stuff
// Call utility function
Gen.DoSomething(true, 1234.0f);
}
}
Remember all of this is unchecked code that is written in the browser so there will be syntax errors.
Let me know if I misunderstood your intentions.
Do some research on the concept of the singleton pattern. Examples: unifycommunity.com
That seems to do the job thanks, one question though, how should I create my structures that are in gen? for example Gen used to look like this:
public static class Gen
{
public struct Point
{
public int x;
public int y;
public Point(int X,int Y)
{
x = X;
y = Y;
}
};
Point gridpos = new Point(0, 0);
}
Basically I want to be able to use the type or structure Point in every other class I have. Gen seemed the place to
put it but I’m not sure now based on what you have said.
That’s way I pointed to the examples and said that he should do some research on the singleton pattern.
But It does not really matter a static class will get the job done just fine unless your doing some threading, etc.
For the Point question, depending on how much utility code you have, it might be worth it to separate the utility code from unity.
Basicly create a class library outside of unity, and drop the assembly in unity.
Thanks everyone, I’m a 25 year c coder and the transition to c# isn’t as staight forward as it first seems.
It’s all a bit clearer now thanks to you lot so thanks again.