- Home /
Conversion of a static class to struct?
Hello Devs,
I am right now developing for mobile and was wondering to create a struct which would have certain level information. That information could be used anytime during the gameplay by any other script without creating an instance.
For example, lets say that I want to know the LevelScore then I could just use
float Percent = LevelInformation.GetCurrentLevelScore()
At the same time, If I want to set the level score - I could
LevelInformation.SetCurrentLevelScore (1234);
from any script.
With above methodology I hope I am right.
Moving to the next section, I have created a static class. In order use the stack instead of heap I want to convert the static class to a struct. I just need a help from you guys on that. I am blind on that.
using System.Collections;
public static class LevelInformation
{
private static bool isPAUSED;
private static bool isITSTARTROOM;
private static bool isITENDROOM;
private static bool isLEVELCOMPLETE;
private static bool isLEVELCOMPLETIONSUCCESS;
private static bool isHEADSTILLON;
private static bool isLEFTLIMBSTILLON;
private static bool isRIGHTLIMBSTILLON;
private static bool isMUSICON;
private static bool isSOUNDEFFECTSON;
private static float GameCompletionPercent;
private static float LevelMetersCovered;
private static float CurrentMummyVelocity;
private static int ThisLevelScore;
// Initialize during the start of the level.
public static void InitializeAtTheStartOfLevel()
{
}
// PAUSE recognition.
public static bool IsTheLevelPaused()
{
return isPAUSED;
}
public static void SetCurrentLevelScore (int s)
{
ThisLevelScore = s;
}
public static int GetCurrentLevelScore()
{
return ThisLevelScore;
}
}
Any sort of help is appreciated.
Thank you.
That's not what structs are for. You do want to use a class for this. While there are exceptions, a general rule of thumb is that a struct should be 16 bytes or fewer, and behave "like an int" (in that it would make sense to add them together and so on; for example, a Vector3--which is a struct). The stack/heap thing is irrelevant in this case.
Thank you Eric. This is the first time I am trying something like this. So is the above class good? I mean is there anything you think I should do concerning my requirement.
It looks like it should function the way you want, though I'd recommend sticking with the Unity convention of always using lowercase for variable names.
Eric Thank you. And regarding the na$$anonymous$$g convention - doyoumeanlikethis or doYOU$$anonymous$$EANLI$$anonymous$$ETHIS? And could you please enlighten me the benefits on that?
Sorry, I mean the initial letter. It makes things easier to keep straight; for example the difference between transform and Transform.
Answer by Eric5h5 · Apr 30, 2013 at 01:57 AM
That's not what structs are for. You do want to use a class for this. While there are exceptions, a general rule of thumb is that a struct should be 16 bytes or fewer, and behave "like an int" (in that it would make sense to add them together and so on; for example, a Vector3--which is a struct). The stack/heap thing is irrelevant in this case.