Tick in Game Instance... or UObject?

I really need to make something that check the battle situation.

At first, I tried to add Tick in Custom Game Instance by Rama…

but it causes a lot of troubles…

thus I tried to implement Tick in UObject…

but UObject does not Tick…

is there possible way other than Actor?

I am also thinking about Timer… if it is possible (might be better)

1 Like

Here is a solution :

.h :



UCLASS()
class MYPROJECT_API UMyObject : public UObject, public FTickableGameObject
{
	GENERATED_BODY()

public:
UFUNCTION(BlueprintCallable, Category = Test)
void CallTimer();

void TestTimer();

void Tick(float DeltaTime) override;
bool IsTickable() const override;
bool IsTickableInEditor() const override;
bool IsTickableWhenPaused() const override;
TStatId GetStatId() const override;

UWorld* GetWorld() const override;

float TestCounter;
};


.cpp



void UMyObject::CallTimer()
{
	FTimerHandle tHandle;
	const float Delay = 1.0f;
	GetWorld()->GetTimerManager().SetTimer(tHandle, this, &UMyObject::TestTimer, Delay, false);
}

void UMyObject::TestTimer()
{
	GEngine->AddOnScreenDebugMessage(1, 2, FColor::Red, "Hello World");
}

void UMyObject::Tick(float DeltaTime)
{
	TestCounter += DeltaTime;

	GEngine->AddOnScreenDebugMessage(0, 0, FColor::Green, FString::SanitizeFloat(TestCounter));

}

bool UMyObject::IsTickable() const
{
	return true;
}

bool UMyObject::IsTickableInEditor() const
{
	return false;
}

bool UMyObject::IsTickableWhenPaused() const
{
	return false;
}

TStatId UMyObject::GetStatId() const
{
	return TStatId();
}

UWorld* UMyObject::GetWorld() const
{
	return GetOuter()->GetWorld();
}


8 Likes

Wow! that’s awesome, i gonna try this code. thank you so much!:smiley:

This worked like a charm! Thanks so much!

I was looking for this, too. Thanks. I’ve got a question though: I haven’t seen this sort of “multiple inheritance” for object+struct yet, only for interfaces. Can you do this with every struct, integrating it into any UObject, or is this a kind of special case?

GameMode() sounds like the logical place for checking game state. But a custom object would work as well.

It is not a struct, it is a class. See here: https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/FTickableGameObject/