[C++] WorldContextObject Parameter

Hi, I’m trying to set the world’s time dilation, but I’m having an issue with one of the parameters it wants. Here’s the function declaration inside GameplayStatistics:

static void SetGlobalTimeDilation(UObject* WorldContextObject, float TimeDilation);

I’m using this in my Game class, and I’m not sure what to use as the first parameter. I’ve tried self, GetWorld(), and more. It has compiled with a few parameters I’ve tried, but the editor crashes on startup. What should I use as the first parameter?

Thanks! :slight_smile:

The WorldContextObject can be anything with an implemented GetWorld() function. Most frequently this will be an Actor, but GetWorld() should have worked just fine.

Can you provide some details on the crash?

I compile the code in VS 2013 Express, then open the editor through UE4Editor.exe (compiled from source). The loading screen opens for a couple of seconds, and then closes without warning. No error message or anything.

I have used

UGameplayStatics::SetGlobalTimeDilation(this, 0.01f);

in both my pawn class and my game mode class, both cause the same result.

Edit: The compiler throws no errors, and I have checked that the editor opens and runs without with the SetGlobalTimeDilation function call.

You should be able to find the call stack for the crash in the saved folders, and if you ran in the debugger it should stop on the crash line.

That said, based on your description I’m assuming that you’re calling this in the constructor or something similar. That won’t be a safe place to call from as the class default object (CDO) will never be in a world and gets constructed very early. But even for other instances of the class they won’t know what world they are in until much further in the object life cycle.

I’d look at making the call in BeginPlay as a reasonable option

That said, based on your description
I’m assuming that you’re calling this
in the constructor or something
similar. That won’t be a safe place to
call from as the class default object
(CDO) will never be in a world and
gets constructed very early. But even
for other instances of the class they
won’t know what world they are in
until much further in the object life
cycle.

I’d look at making the call in
BeginPlay as a reasonable option

Of course, that was exactly it. Thank you! :slight_smile: