Comments (14)
Actually Setting The Static Value To Null Inside The Initialize Method is a lot more effective. That way it'll only become null when it first spawns into the world.
public override void Initialize(FObjectInitializer initializer)
{
//Set ThisInstance To Null, Otherwise Value Doesn't Get Destroyed and Will Crash Engine.
ThisInstance = null;
}
from usharp.
Yea USharp isn't loaded per game instance / world, there is only ever one USharp loaded. You need to be careful with using static members. I wouldn't recommend using the Initialize method as that is called every time a new object of that type is created.
I think it would be nice to have something which allows you to work with statics on a per world basis or perhaps on a PIE basis.
from usharp.
That's a good point! I feel like I'm over thinking my architechure, but I really wanted a singleton component similar to those in unity. I actually set ThisInstance to null in both the Initialize and EndPlay methods. But I use a boolean flag to dictate I can only set the value when the game hasn't ended.
[UPropertyIngore]
protected static bool bCanSetInstance;
public override void Initialize(FObjectInitializer initializer)
{
//Set ThisInstance To Null, Otherwise Value Doesn't Get Destroyed and Will Crash Engine.
ThisInstance = null;
bCanSetInstance = true;
}
protected override void ReceiveEndPlay_Implementation(EEndPlayReason EndPlayReason)
{
bCanSetInstance = false;
ThisInstance = null;
}
public static BowlGameModeComponent GetInstance(UObject worldContextObject)
{
if(ThisInstance == null && bCanSetInstance)
{
ThisInstance = UGameplayStatics.GetGameMode(worldContextObject).GetComponentByClass<BowlGameModeComponent>();
}
return ThisInstance;
}
from usharp.
I was considering creating a Singleton class, that any actor or component base class can inherit from.
public class UActorComponentSingleton<T> where T : UActorComponent
{
}
public class BowlGameModeComponent : UActorComponentSingleton<BowlGameModeComponent>
{
}
from usharp.
I don't know if USharp would allow inherited c# classes to be blueprintable.
from usharp.
I'm not sure singleton actors make sense since as actors are inherently an instanced based thing. I haven't looked into singletons yet for USharp but I guess it would be best to follow how its done with C++.
Doing it the way in the link sounds reasonable, you would have one object which would control your global objects, that way you don't need to make anything static and it should work between runs with PIE.
There is also UGameInstance which seems to have one instance for the duration of the game / PIE. The added benefit on UGameInstance is that is has a ReceiveShutdown event.
As for blueprintable inheritance yes that should work. If it doesn't for any reason I'll have to look into it and fix it up.
from usharp.
Got a nasty error on hot-reload. I'll try to fix it!
from usharp.
Yeah the same error keeps popping up. It doesn't matter if I set my singleton class to UClass or not.
from usharp.
It seems like Classes With Generics, or inheriting from Generic Classes cannot be a blueprint class.
from usharp.
[UClass, Blueprintable, BlueprintType]
public class MyActorComponentSingleton<T> : UActorComponent where T : UActorComponent
{
}
Cannot create blueprint from this class.
from usharp.
Yea generic classes aren't supported. That is a limitation of Unreal, you can't make generic classes in C++ either. That isn't something that could ever be supported by USharp. I'll make a note to print an error in AssemblyRewriter when it sees a generic class.
from usharp.
That's a good point
from usharp.
Edit: Actually It Only Partially Works, I'm figuring it out.
In the Singleton class, if I give all my methods the attribute [UFunctionIgnore], it prevents the engine from crashing when overriding some of the methods in the inheriting class.
It doesn't work for begin play or end play methods.
And I used the [UClassIgnore] Attribute for the singleton class
And I also don't get any of the actor component properties or methods when I use blueprints, so no point inheriting from a singleton class.
from usharp.
Using a WorldStaticVar fixes this issue perfectly. This was recently added, it's a generic class which automatically destroys the value when the world gets deleted. Here's how I set mine up.
protected static WorldStaticVar<BowlGameModeComponent> ThisInstance = new WorldStaticVar<BowlGameModeComponent>();
public static BowlGameModeComponent GetInstance(UObject worldContextObject)
{
if(ThisInstance.Get(worldContextObject) == null){
ThisInstance.Set(worldContextObject, UGameplayStatics.GetGameMode(worldContextObject).GetComponentByClass<BowlGameModeComponent>());
}
return ThisInstance.Get(worldContextObject);
}
from usharp.
Related Issues (20)
- Question about CodeGenerator HOT 3
- crash when overriding some of my C++ native event and blueprint implementable event HOT 8
- Losing reference to C# class when restarting UE4 HOT 1
- Question: Creating mesh with USharp HOT 3
- Error and crash after hotreload with disabled USharpMinHotReload HOT 1
- FRandRange error!
- USharp doesn't compile HOT 11
- PluginInstaller can't find MSBuild with VisualStudio2019 Professional
- Error in Unreal Editor when recompiling MyProject.Managed.dll
- How to get GetWorld() in C#? HOT 1
- Support for default values on C# auto properties exposed to UE4 HOT 1
- Crash when hotreloading after changing the layout of a struct
- Replace method name string with nameof's
- Missing Engine Module ApexDestruction HOT 2
- UUserWidget -> GetWidgetFromName implementation? HOT 1
- [Bug] TMap's iterator is different between C# and C++ in UE 4.22 HOT 6
- Full dev integration HOT 4
- Class redirectors don't work HOT 1
- File.ReadAllText( ... ) crashes UE4 HOT 2
- Support for latent functions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from usharp.