// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/TextBlock.h"
#include "Blueprint/UserWidget.h"
#include "UINew.generated.h"
/**
*
*/
UCLASS()
class ROLLABALL2_API UUINew : public UUserWidget
{
GENERATED_BODY()
public:
virtual void NativeConstruct() override;
public:
//UPROPERTY(BlueprintReadOnly,meta =(BindWidget))
UPROPERTY(meta = (BindWidget))
class UTextBlock* ScoreValue;
UFUNCTION()
void SetScoreValue(float score);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "Components/TextBlock.h"
#include "Blueprint/UserWidget.h"
#include "ScoreManager.h"
#include "UINew.h"
void UUINew::SetScoreValue(float score)
{
UE_LOG(LogTemp, Warning, TEXT("Hello set score is %f"),score);
//ScoreValue->SetText(FText::AsNumber(score));
//ScoreValue->SetText(FText::FromString(FString::SanitizeFloat(score)));
//ScoreValue= (UTextBlock*)this->GetWidgetFromName("ScoreValue");
//ScoreValue->SetText(FText::FromString(FString::SanitizeFloat(score)));
//ScoreValue->SetText(TEXT("Hello"));
//ScoreValue->Text=FText::AsNumber(score2);
//FText text= ScoreValue->GetText();
if (ScoreValue) {
UE_LOG(LogTemp, Warning, TEXT("Hello score value is not null"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Hello score value is>> null"));
}
}
void UUINew::NativeConstruct()
{
Super::NativeConstruct();
SetScoreValue(2);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "MySaveGame.h"
#include "LevelManager.h"
#include "UINew.h"
#include "Components/TextBlock.h"
#include "Internationalization/Text.h"
#include "UserWidget.h"
#include "GameFramework/Actor.h"
#include "ScoreManager.generated.h"
UCLASS()
class ROLLABALL2_API AScoreManager : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AScoreManager();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintPure, Category = "Score")
float GetCurrentScore();
UFUNCTION(BlueprintCallable, Category = "Score")
void UpdateScore(float score);
UPROPERTY(EditAnywhere, meta = (BindWidget))
UUserWidget* Hud;
UPROPERTY(EditAnywhere)
UUINew* UINew;
UFUNCTION()
void SaveData();
UPROPERTY(EditAnywhere)
ALevelManager* LevelManager;
private:
UPROPERTY(EditAnywhere, Category = "Score")
float Score;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "ScoreManager.h"
#include "Blueprint/UserWidget.h"
#include
// Sets default values
AScoreManager::AScoreManager()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AScoreManager::BeginPlay()
{
Super::BeginPlay();
if (UMySaveGame* LoadedGame = Cast<UMySaveGame>(UGameplayStatics::LoadGameFromSlot(TEXT("PlayerScore"), 0)))
{
// // The operation was successful, so LoadedGame now contains the data we saved earlier.
Score = LoadedGame->PlayerScore;
// UE_LOG(LogTemp, Warning, TEXT("Player Score is : %f"), Score);
//Text = FText::FromString(FString::SanitizeFloat(Score));
// ScoreValue = (UTextBlock*)Hud->gettext(TEXT("ScoreValue"));
//ScoreValue = (UTextBlock*) Hud->GetWidgetFromName(TEXT("ScoreValue"));
//ScoreValue->SetText(FText::FromString(FString::SanitizeFloat(Score)));
// UINew->ScoreValue->SetText(FText::AsNumber(Score));
}
//UINew = Cast(GetWorld());
UINew->SetScoreValue(Score);
FTimerHandle UnusedHandle;
GetWorld()->GetTimerManager().SetTimer(UnusedHandle, [this]() {
UINew->SetScoreValue(Score);
},2,1);
//FOutputDeviceNull ar;
//Hud->CallFunctionByNameWithArguments(TEXT("Get_ScoreValue_Text"), ar, NULL, false);
}
// Called every frame
void AScoreManager::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
float AScoreManager::GetCurrentScore()
{
return Score;
}
void AScoreManager::UpdateScore(float score)
{
Score += score;
//UE_LOG(LogTemp, Warning, TEXT("Score is %f"), Score);
FOutputDeviceNull ar;
Hud->CallFunctionByNameWithArguments(TEXT("Get_ScoreValue_Text"),ar,NULL,false);
//UINew->SetScoreValue(Score);
LevelManager->CheckIsLevelComplete();
SaveData();
}
void AScoreManager::SaveData()
{
if (UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass())))
{
SaveGameInstance->PlayerScore = Score;
if (UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveGameInstance->SaveSlotName, SaveGameInstance->UserIndex))
{
//UE_LOG(LogTemp, Warning, TEXT("Data Saved"));
}
}
}
i want to access SetScoreValue() which is in uinew class. when i access it from scoremanager then i get scorevalue null which is a UTextBlock. and when i run SetScoreValue from NativeConstruct() of class uinew then it works so what should i do?