Empty title 003

Empty content…

Here are two ways:

  1. Pass a pointer to the Application object into the constructor of Char. Since MyApp is essentially a singleton created as a local variable in main(), you’ll never run into problems with memory safety unless you try to delete it, or try to store the pointer in a smart pointer (shared or unique, which will delete it when the ptr gets destroyed).
  2. Make MyApp an Urho3D::Subsystem, then just call GetSubsystem<MyApp>() whenever you want it. I have been pondering if this is architecturally sound or basically flawed/dangerous OO. Note: executing RemoveSubsystem<MyApp>() will crash your program.

The correct way to do it (pardon my edits viewers) is to add this macro to your application class declaration:

class MyApp final : public Application
{
    URHO3D_OBJECT(MyApp, Application);
    ...
}

Then in the constructor register as a subsystem:

MyApp::MyApp(Context *context) : Application{context}
{
    context_->RegisterSubsystem(this);
}

Then use it as a standard subsystem:

GetSubsystem<MyApp>()->Test();

Note: in your example App::Test() could be declared static, which would allow you to call it from anywhere without needing an instance of App.

2 Likes

You could also do DynamicCast<App>(GetSubsystem<Application>())->Test().

1 Like

Yeah it’s pretty verbose though. Adding the macro is super easy

1 Like

Also, welcome to the forums! :confetti_ball: :slightly_smiling_face:

1 Like