C
C#9mo ago
2spooky2play

✅ Get type of current instance in a static method

public static int GetTypeId() {
return typeof(TypeOfInstance).Name.GetHashCode();
}
public static int GetTypeId() {
return typeof(TypeOfInstance).Name.GetHashCode();
}
how can i get the current type of the current class? this class will be inherited by other classes, and i need to get the hash code of the name of the class
42 Replies
Angius
Angius9mo ago
That's some weird stuff you're doing there Generally, the parent class has no knowledge of child classes You can give the base class an abstract or virtual property named Hash or something, and each child class will have to implement it But, yeah, curious why you even need to do that. I never see .GetHashCode() used outside of implementing equality operators/interfaces
2spooky2play
2spooky2playOP9mo ago
https://github.com/RyanDeivert555/GameObject-Framework/blob/main/include/component.h i want to translate this C++ code, but the code uses macros which arent in C#
GitHub
GameObject-Framework/include/component.h at main · RyanDeivert555/G...
A framework built on top of raylib that allows for the use of a game object system akin to Unity - RyanDeivert555/GameObject-Framework
2spooky2play
2spooky2playOP9mo ago
its a GameObject system i want to hash the names of the Components for easy storage in the game object
Jimmacle
Jimmacle9mo ago
if you call GetType() that will get the actual type of the instance, but you probably don't want to be using reflection
Angius
Angius9mo ago
Yeah, reflection is slow Wouldn't use it in a game
Jimmacle
Jimmacle9mo ago
and the hashcode of a type name is certainly not a good thing to choose as an identifier hashcodes aren't even guaranteed to be the same between application runs iirc
Angius
Angius9mo ago
Yep
2spooky2play
2spooky2playOP9mo ago
hmm i see but there are no macros in c#, so what should i do?
Jimmacle
Jimmacle9mo ago
if you need a type ID system, implement something that doesn't rely on how C# is implemented even relying on type names will make your game brittle in the face of refactoring if you want something that's like a macro, look into source generators that's about the closest you'll get alternatives would be abstract/virtual methods that you override in Component derivatives or an IComponent interface to require implementing them
2spooky2play
2spooky2playOP9mo ago
I only have the id thing so i can store the components in a Dictionary for easy lookup protected readonly Dictionary<int, Component> Components = []; this is in the GameObject class int is the id of the component
Angius
Angius9mo ago
Make the key a GUID maybe
2spooky2play
2spooky2playOP9mo ago
thats a good idea
Jimmacle
Jimmacle9mo ago
if it's only at runtime why not just use the type as the key
2spooky2play
2spooky2playOP9mo ago
wdym? one problem with that is the method cant be static. ideally i would be able to do something like this;
public bool RemoveComponent<T>() where T: Component {
return RemoveComponent(T.GetTypeId());
}
public bool RemoveComponent<T>() where T: Component {
return RemoveComponent(T.GetTypeId());
}
Angius
Angius9mo ago
So you can have only one component of each type?
2spooky2play
2spooky2playOP9mo ago
wdym
Angius
Angius9mo ago
If you want to be able to remove components by their type... what happens if there are multiple FooComponents? T would be FooComponent
2spooky2play
2spooky2playOP9mo ago
RemoveComponent<FooComponent>()
RemoveComponent<BarComponent>()
RemoveComponent<BazComponent>()
RemoveComponent<FooComponent>()
RemoveComponent<BarComponent>()
RemoveComponent<BazComponent>()
each game object can only have one type of each component. it cant have two FooComponents
Angius
Angius9mo ago
Ah, that would work then. Long as you ensure that it is the case
2spooky2play
2spooky2playOP9mo ago
public Component AddComponent(Component component) {
component.OnCreate();
var id = 0;
Components[id] = component;

return Components[id];
}
public Component AddComponent(Component component) {
component.OnCreate();
var id = 0;
Components[id] = component;

return Components[id];
}
(ignore the id part right now) this would ensure that, right? Components is a dictionary
Angius
Angius9mo ago
A dictionary cannot have conflicting keys
2spooky2play
2spooky2playOP9mo ago
ok i think i can work from here. thank you guys i want to make the ids static, but idk if thats possible
Angius
Angius9mo ago
Sure is
class Foo
{
public static Guid Id = Guid.NewGuid();
}
class Foo
{
public static Guid Id = Guid.NewGuid();
}
Do note, though, that it will be different each time the app runs
2spooky2play
2spooky2playOP9mo ago
derived classes will not have their own ids though
Angius
Angius9mo ago
It will stay the same during each individual run, though
2spooky2play
2spooky2playOP9mo ago
they will share the static id with the base class
Angius
Angius9mo ago
You can make it a property and make it abstract static abstract properties are a thing nowadays
2spooky2play
2spooky2playOP9mo ago
public static readonly abstract Guid Id; i get an error saying A static member cannot be marked as 'abstract'
Angius
Angius9mo ago
C#/.NET version?
2spooky2play
2spooky2playOP9mo ago
i downloaded .NET sdk 8.0 idk if that tells you what version i have
Angius
Angius9mo ago
Check the csproj
2spooky2play
2spooky2playOP9mo ago
its 6.0.0
Angius
Angius9mo ago
Oof, two versions old Yeah, no static abstract for you
2spooky2play
2spooky2playOP9mo ago
i want the latest version
Angius
Angius9mo ago
Change that net6.0 to net8.0 then
2spooky2play
2spooky2playOP9mo ago
wait no its 8.0, i was looking at a dependency
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Raylib-cs" Version="6.0.0" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Raylib-cs" Version="6.0.0" />
</ItemGroup>

</Project>
mb still have the error though
2spooky2play
2spooky2playOP9mo ago
damn
Angius
Angius9mo ago
Anyway, I gotta hit the hay now, so maybe Jim will be able to help some more
2spooky2play
2spooky2playOP9mo ago
thank you for your help
Angius
Angius9mo ago
Anytime
2spooky2play
2spooky2playOP9mo ago
ok, if i want to use Guid's, i need static abstract i think because right now each instance has a new id, which isnt what a want nevermind protected readonly Dictionary<Guid, IComponent> Components = []; cannot be done because of the static abstract
Want results from more Discord servers?
Add your server