How are Tick functions for slate widgets written?

I’m attempting to write a Tick function for a slate widget. Will you provide an example of an operational tick function within a slate widget?

why yes it just so happens I can! Follow the example exactly, any misspelling or omissions will cause the code to error.
.h file:

class SLoadingScreenOne : public SCompoundWidget
{
public:
	SLATE_BEGIN_ARGS(SLoadingScreenOne) {}

	SLATE_ARGUMENT(TWeakObjectPtr<class ATestHud>, OwningHUD)

	SLATE_ARGUMENT(APlayerController*, playerOnePlayerController)

	SLATE_END_ARGS()

	/** Constructs this widget with InArgs */
	void Construct(const FArguments& InArgs);
	
	void Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime);

    //...
};

.cpp file:

void SLoadingScreenOne::Construct(const FArguments& InArgs)
{
	SetCanTick(true);
	bCanSupportFocus = true;

	OwningHUD = InArgs._OwningHUD;
	playerOnePlayerController = InArgs._playerOnePlayerController;
    //...
}

void SLoadingScreenOne::Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime)
{
	SCompoundWidget::Tick(AllottedGeometry, InCurrentTime, InDeltaTime);

	//.. your tick code here
}

I got the player controller in my hud class and passed it into the widget on creation.

Don’t forget to implement ReleaseSlateResources for your userwidget wrapper or you will get a memory leak.

1 Like

hwhat
I couldn’t find any specific ReleaseSlateResources function or class. could you link me into a documentation

You need to release the slate resource from within the widget.

Take a look at the progress bar engine implementation as an example. The engine will warn you about the memory leak if you add your wiget to a canvas

Outer Widget that uses the custom slate:

.h


#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "WLoadingScreenOne.generated.h"

class SLoadingScreenOne;
/**
 * 
 */
UCLASS()
class MYPROJECT_API UWLoadingScreenOne : public UUserWidget
{
	GENERATED_BODY()
	
public:
	virtual void ReleaseSlateResources(bool bReleaseChildren) override;

protected:
	/** Native Slate Widget */
	TSharedPtr<SLoadingScreenOne> MyLoadingScreen;

	virtual TSharedRef<SWidget> RebuildWidget() override;

};

.cpp

#include "WLoadingScreenOne.h"
#include "SLoadingScreenOne.h"

TSharedRef<SWidget> UWLoadingScreenOne::RebuildWidget()
{
	MyLoadingScreen = SNew(SLoadingScreenOne);

	return MyLoadingScreen.ToSharedRef();
}

void UWLoadingScreenOne::ReleaseSlateResources(bool bReleaseChildren)
{
	Super::ReleaseSlateResources(bReleaseChildren);

	MyLoadingScreen.Reset();
}
1 Like

thank you for mentioning this! My widget code inherits from SWidget, which does not contain a ReleaseSlateResources function nor a RebuildWidget function. I have to imagine releasingSlateResources is a non issue for descendents of SWidget, but I do have my functional Swidgets nested in blank Swidgets when adding to and removing from viewport.

Thank you for saying something I’ll keep an eye out

It’ll come in handy if you decide to put it inside of a UserWidget in the future :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.