C
C#3w ago
FiftyTifty

✅ Creating a single global object that can be accessed by other classes?

I'm someone who comes from using Pascal and Lua, so bear with me here. I want to create a single nested list with custom properties, which I can then iterate through. But it's not intuitive how we actually do that. To demonstrate, here is a snippet of the code I'm working on for Bannerlord:
C#
namespace TeachSpouses
{

public class tsCharacterToTeach
{
public Hero Student;
public int NumLevelsCanBeTaught;
}

public class tsListCharactersToTeach
{
public SkillObject Skill;
public List<tsCharacterToTeach> Students;
}

public class tsClassroom
{
public List<tsListCharactersToTeach> Teaching;
}

//...bunch of other classes
public class Util
{

public static bool bFoundSpousesExpanded;
public static void ShowMessage(string strToShow)
{

InformationManager.DisplayMessage(new InformationMessage("TeachSpouses - " + strToShow));

}
//..Even more classes
C#
namespace TeachSpouses
{

public class tsCharacterToTeach
{
public Hero Student;
public int NumLevelsCanBeTaught;
}

public class tsListCharactersToTeach
{
public SkillObject Skill;
public List<tsCharacterToTeach> Students;
}

public class tsClassroom
{
public List<tsListCharactersToTeach> Teaching;
}

//...bunch of other classes
public class Util
{

public static bool bFoundSpousesExpanded;
public static void ShowMessage(string strToShow)
{

InformationManager.DisplayMessage(new InformationMessage("TeachSpouses - " + strToShow));

}
//..Even more classes
I only need one instance of tsClassroom, and I'll handle clearing it out and refilling it. But a single instance would be a public static, which can't have objects assigned to it right? public static List<tsClassroom> = new List<tsClassroom> is not supported in C#, so I'm a bit lost, even if it's obvious to others 🐤
23 Replies
ero
ero3w ago
i'm not sure why you say public static List<tsClassroom> = new List<tsClassroom> is not supported. that's perfectly valid c#. do you have some error message?
FiftyTifty
FiftyTiftyOP3w ago
Yeah it's a weird one. Like it just breaks C#
No description
FiftyTifty
FiftyTiftyOP3w ago
And without the brackets, it says there needs to be brackets.
No description
ero
ero3w ago
well you need to declare members in types there's no such thing as a top level, or "global" member
FiftyTifty
FiftyTiftyOP3w ago
Yeah that lost me already. I know we can assign basic variables like ints and floats, and access them easily by just doing e.g:int iBaseCost = tsSettings.tsBase Hang on maybe I'm on the verge of figuring this out
FiftyTifty
FiftyTiftyOP3w ago
Aha! So it's like this? Got to put it in a static class
No description
ero
ero3w ago
for example, yes static members can be in non-static classes as well for example
class Classroom
{
public static Classroom Instance { get; } = new();
}
class Classroom
{
public static Classroom Instance { get; } = new();
}
(we don't use hungarian notation in c#)
FiftyTifty
FiftyTiftyOP3w ago
Banging. And I take it we need to create the objects inside it too? So later do tsSettings.Classroom.Teaching = new List<tsListCharactersToTeach>
ero
ero3w ago
you can use an object initializer
FiftyTifty
FiftyTiftyOP3w ago
Yeah I noticed the different syntax. For me, I get lost and distracted very easily. So having the variable type at the start of the name helps keep me steady
ero
ero3w ago
c# is statically typed, every variable already has a specific type it's not like in lua where you genuinely need the name to keep track of the type
FiftyTifty
FiftyTiftyOP3w ago
Aye I meant more for me typing out code. Pascal is the same Easily over half of my issues are me mixing up types because I got careless or distracted
ero
ero3w ago
you can't mix up types in c# is what i'm saying and even while typing you can view the type of any variable you use
ero
ero3w ago
No description
ero
ero3w ago
just saying in case you plan on creating bigger projects with contributors they'll likely be very confused about the hungarian notation choice not that it matters while you're just learning
class Classroom
{
public static Classroom Instance { get; } = new()
{
Teaching = []
};
}
class Classroom
{
public static Classroom Instance { get; } = new()
{
Teaching = []
};
}
or
class Classroom
{
public static Classroom Instance { get; } = new Classroom
{
Teaching = new List<tsListCharactersToTeach>()
};
}
class Classroom
{
public static Classroom Instance { get; } = new Classroom
{
Teaching = new List<tsListCharactersToTeach>()
};
}
to be more explicit you can also just make tsClassroom static itself
FiftyTifty
FiftyTiftyOP3w ago
You are a wizard god damn. This is magic
ero
ero3w ago
but it's sort of bad practice to make everything static
FiftyTifty
FiftyTiftyOP3w ago
Oh aye I've not made everything static. Just the stuff I only need once. The game I'm modding is filled with non-static objects, and I'm doing stuff to them
ero
ero3w ago
you wouldn't really need a static Classroom, imo if you need one, you'd instantiate it in Program.Main or something and pass it around as needed
FiftyTifty
FiftyTiftyOP3w ago
Yeah there needs to be one, but just one. Passing things around is a mess, because of how events work and other weird stuff. There will be proper ways to go about it but I'm a baby bird with C# atm
ero
ero3w ago
it's not "wrong" to use a singleton pattern for this, but it's more "oop-like" not to use it and instead pass around instances whichever works for you for now, just be aware $close
MODiX
MODiX3w ago
If you have no further questions, please use /close to mark the forum thread as answered
FiftyTifty
FiftyTiftyOP3w ago
I owe you my life

Did you find this page helpful?