I am trying to get the slate resources made in editor to use in my slate widget.
I followed this guide https://wiki.unrealengine.com/Slate,Loading_Styles%26_Resources
Currently my widget class looks like this
.h
#include "Slate.h"
#include "SlateGameResources.h"
class FMyUIResources
{
public:
FMyUIResources() : m_Path("/Game/UI"), MyUIResources(NULL) { };
/*Loads resources and registers them with Slate*/
/*Do this before trying to use resources*/
void Initialize();
/*cleanup*/
/*Do this when shutting down game module*/
void Shutdown();
/*reloads textures used by slate renderer*/
/*Does nothing at the moment*/
void ReloadTextures();
/*Give caller a pointer to our FSlateGameResources*/
TSharedPtr<FSlateGameResources> GetSlateGameResources();
protected:
/*Creates resources*/
TSharedRef<class FSlateGameResources> Create();
/*Defined in Cpp file, change as needed*/
const FString m_Path;
/*Poitner to game resources, Initialize() before using*/
TSharedPtr<FSlateGameResources> MyUIResources;
};
class SGameWidget : public SCompoundWidget
{
SLATE_BEGIN_ARGS(SGameWidget)
: _BallHUD()
{
}
SLATE_ARGUMENT(TWeakObjectPtr<class ABallHUD>, BallHUD);
SLATE_END_ARGS()
public:
void Construct(const FArguments& args);
private:
FReply JumpClicked();
TWeakObjectPtr<class ABallHUD> BallHUD;
};
.cpp
#include "Ball.h"
#include "BallBall.h"
#include "GameWidget.h"
#include "BallHUD.h"
void SGameWidget::Construct(const FArguments& args)
{
BallHUD = args._BallHUD;
ChildSlot
[
SNew(SOverlay)
+ SOverlay::Slot()
.HAlign(HAlign_Center)
.VAlign(VAlign_Bottom)
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.Padding(FMargin(0,0,0,16))
[
SNew(SButton)
.Text(FText::FromString("Jump"))
.OnClicked(this, &SGameWidget::JumpClicked)
]
]
];
}
FReply SGameWidget::JumpClicked()
{
BallHUD->JumpTriggered();
return FReply::Handled();
}
// Gaba
void FMyUIResources::Initialize()
{
if (!MyUIResources.IsValid())
{
MyUIResources = Create();
FSlateStyleRegistry::RegisterSlateStyle(*MyUIResources);
}
}
TSharedPtr<FSlateGameResources> FMyUIResources::GetSlateGameResources()
{
return MyUIResources;
}
TSharedRef<class FSlateGameResources> FMyUIResources::Create()
{
return FSlateGameResources::New(FName("MyUIResources"), m_Path, m_Path);
}
/*Unregister resources/styles with Slate, cleanup, free memory*/
void FMyUIResources::Shutdown()
{
//Unregister *MyUIResources with Slate
FSlateStyleRegistry::UnRegisterSlateStyle(*MyUIResources);
//Debugging
ensure(MyUIResources.IsUnique());
//Removes reference to resources, decrements refcount, destroys resources if refcount=0
//Do this to all SharedPtrs on Shutdown() or SomethingSimilar() to avoid memory leak
MyUIResources.Reset();
}
I get not errors if I compile with this.
However if I edit my game module class. Which currently before editing looks like this
.h
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#ifndef __BALL_H__
#define __BALL_H__
#include "Engine.h"
#endif
.cpp
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#include "Ball.h"
IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, Ball, "Ball" );
To something like this
.h
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Engine.h"
#include "SlateClasses/GameWidget.h"
class FMyProject : public FDefaultGameModuleImpl
{
public:
////////////////////////////////////////////////////////////////////////////////////////////////////
/////Called when GameModule is loaded, load any resources game may need here
void StartupModule();
////////////////////////////////////////////////////////////////////////////////////////////////////
/////Called when GameModule is unloaded, before shutdown, unload resources/cleanup here
void ShutdownModule();
////////////////////////////////////////////////////////////////////////////////////////////////////
/////Give a handle to MyUIResources to anyone who asks
TSharedPtr<FSlateGameResources> GetSlateGameResources();
protected:
////////////////////////////////////////////////////////////////////////////////////////////////////
/////Data Structure and Interface for maintaining SlateGameResources on Game to Game basis
FMyUIResources MyUIResources;
};
.cpp
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#include "Ball.h"
#include "Ball.generated.inl"
IMPLEMENT_PRIMARY_GAME_MODULE(FBall, Ball, "Ball");
void FBall::StartupModule()
{
FDefaultGameModuleImpl::StartupModule();
/*Loads resources and registers them with Slate*/
/*Do this before trying to use resources*/
MyUIResources.Initialize();
/*TODO: Anything else game module might need to do on load*/
}
void FBall::ShutdownModule()
{
FDefaultGameModuleImpl::ShutdownModule();
/*Unregister resources/styles with Slate, cleanup, free memory*/
MyUIResources.Shutdown();
/*Cleanup/free any resources here*/
}
/*First defined here, no need to call parent*/
/*Give caller a pointer to our FSlateGameResources*/
TSharedPtr<FSlateGameResources> FBall::GetSlateGameResources()
{
/*Give caller a pointer to our FSlateGameResources*/
/*Giving strong pointer, helps gurantee access to resources*/
return MyUIResources.GetSlateGameResources();
}
It gives me a lot of errors
I have added it in this file