Giter VIP home page Giter VIP logo

Comments (14)

Tanner555 avatar Tanner555 commented on May 18, 2024

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.

pixeltris avatar pixeltris commented on May 18, 2024

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.

Tanner555 avatar Tanner555 commented on May 18, 2024

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.

Tanner555 avatar Tanner555 commented on May 18, 2024

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.

Tanner555 avatar Tanner555 commented on May 18, 2024

I don't know if USharp would allow inherited c# classes to be blueprintable.

from usharp.

pixeltris avatar pixeltris commented on May 18, 2024

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++.

https://wiki.unrealengine.com/Global_Data_Access,_Data_Storage_Class_Accessible_From_Any_CPP_or_BP_Class_During_Runtime

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.

Tanner555 avatar Tanner555 commented on May 18, 2024

Got a nasty error on hot-reload. I'll try to fix it!
assertionfailfrombuilderwhenusingsingletonclass

from usharp.

Tanner555 avatar Tanner555 commented on May 18, 2024

Yeah the same error keeps popping up. It doesn't matter if I set my singleton class to UClass or not.

from usharp.

Tanner555 avatar Tanner555 commented on May 18, 2024

It seems like Classes With Generics, or inheriting from Generic Classes cannot be a blueprint class.

from usharp.

Tanner555 avatar Tanner555 commented on May 18, 2024
[UClass, Blueprintable, BlueprintType]
public class MyActorComponentSingleton<T> : UActorComponent where T : UActorComponent
{

}

Cannot create blueprint from this class.

from usharp.

pixeltris avatar pixeltris commented on May 18, 2024

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.

Tanner555 avatar Tanner555 commented on May 18, 2024

That's a good point

from usharp.

Tanner555 avatar Tanner555 commented on May 18, 2024

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.

Tanner555 avatar Tanner555 commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.