Toon Tank Final challenge menu implementation

I try to implement menu and I’m not sure of the step!

I created a C++ UserWidget and a child widget blueprint and created a menu with a start game button to begin. In the TankGameModeBase I created a blueprint implementable event GameMenu() so I can call it in BeginPlay. So after these steps everythings work but I’m not sure what to do after do I create a reference to TankGameModeBase in my UserWidget to call the HandleGameStart function?
Here what I had done in my UserWidget:

#include "MainMenuWidget.h"
#include "Components/Button.h"
#include "ToonTanks/GameModes/TankGameModeBase.h"

bool UMainMenuWidget::Initialize()
{
	Super::Initialize();
	UWorld* TheWorld = GetWorld();
	if (TheWorld != nullptr)
	{
		//How do cast to Game mode?
		GameMode = Cast<ATankGameModeBase>(UGameplayStatics::GetGameMode(TheWorld));
		StartGame->OnClicked.AddDynamic(this, &UMainMenuWidget::StartButtonClicked);

		return true;
	}
	
}

void UMainMenuWidget::StartButtonClicked()
{
	UE_LOG(LogTemp, Warning, TEXT("button working"));	
}

Thank you for all your assistance and great thanks to GameDev.Tv for this course! The best I followed on Udemy!

Is that not working?

P.S. your Initialize function doesn’t return a value if TheWorld == nullptr

finally it was missing #include “Kismet/GameplayStatics.h”
still some beginner error :joy:
Is their another way to call the HandleGameStart() of TankGameModeBase?
Something like a blueprint event call or something like that. I will continue some tests and research! Thanks for your reply!

I finally got some results but I can’t remove my menu after opening a level.

I created a “plain” level for the main menu with a custom Menu Game Mode. I override it in the level detail. When I click on Start button it open level 1 with the tank but the menu is still visible.



I override the game level with the BP_GameModeBase but no change.

I add RemoveParent() function in c++ OnClicked button function, it work when I don’t add the open level function to the button in blueprint. I try to add it in blueprint to but still no difference.

Thanks for your help!

And if you remove it from parent before calling open level?

No same thing it do not hide the menu. Each time I click on start game it reload the level and I think the menu is reloaded with it but their is no call of it in the game mode of the open level! I will continue to test more things today!

Finally I erase my first User Widget and child and re code a new one with same pattern that work in blueprint. Finally got it! I made a Menu Game Mode and a blank Level with this Game Mode like I have done before.
My Menu header file:

#pragma once

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

/**
 * 
 */
UCLASS()
class TOONTANKS_API UMenuMaster : public UUserWidget
{
	GENERATED_BODY()
protected:

	virtual bool Initialize();

	UPROPERTY(EditAnywhere)
	FName LevelToLoad;
	UPROPERTY(meta = (BindWidget))
	class UButton* StartGame;
	UPROPERTY(meta = (BindWidget))
	class UButton* ExitGame;
	UFUNCTION()
	void StartButtonClicked();
	UFUNCTION()
	void ExitButtonClicked();
};

And my Menu cpp:

#include "MenuMaster.h"
#include "Components/Button.h"
#include "Kismet/GameplayStatics.h"


bool UMenuMaster::Initialize()
{
	
	Super::Initialize();
	if (StartGame != nullptr && ExitGame != nullptr)
	{
		StartGame->OnClicked.AddDynamic(this, &UMenuMaster::StartButtonClicked);
		ExitGame->OnClicked.AddDynamic(this, &UMenuMaster::ExitButtonClicked);		
	}
	return true;
}

void UMenuMaster::StartButtonClicked()
{
		UGameplayStatics::OpenLevel(this, LevelToLoad, true);
}

void UMenuMaster::ExitButtonClicked()
{
	GetWorld()->GetFirstPlayerController()->ConsoleCommand("quit");
}

It was a really big challenge :sweat_smile:

And thanks DanM for your dedication to the community! Your support encourage me to continue and I made it!

1 Like

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

Privacy & Terms