This code works, but it’s bad idea to repeat it 100 times with 100 functions (one per button). And I don’t know how many buttons will spawn, it’s based on number of .sav files. I don’t know which button called that function so I don’t know which save file should I load!
Is it possible to return Name/Index of the clicked button with OnClicked? And then use that index in OnClick_button_load_game(int32 index) function to determine which button was clicked. For example OnValueChanged Event returns float Value by default(USlider) and it’s easy to access Value this way.
Well what you could try, even if it’s not beautiful, you can wrap a level of indirection on your OnClicked delegate. What I mean by that, you define a new type of delegate that return a value. this new type will do an internal invoke to your another delegate that will call your function load_save.
With that level of indirection you’ll be able to catch the actor the Onclicked came from.
#include "FPS4_8.h"
#include "DemoDelegateVolume.h"
#include "Engine.h"
#include "FPS4_8Character.h"
ADemoDelegateVolume::ADemoDelegateVolume()
{
OnActorBeginOverlap.AddDynamic(this, &ADemoDelegateVolume::OnBeginOverlap);
//Bind function
hello.BindUObject(this, &ADemoDelegateVolume::HelloFromCharacter);
}
void ADemoDelegateVolume::OnBeginOverlap(AActor* overlappingActor)
{
AFPS4_8Character* character = Cast<AFPS4_8Character>(overlappingActor);
if (character)
{
hello.Execute(overlappingActor->GetName());
}
}
void ADemoDelegateVolume::HelloFromCharacter(FString characterName)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString("Hello from character : ").Append(characterName));
}
Now some details.
I did based on a volume.
I defined a new BeginOverlap that I replace the default one with. (maybe not required in your case).
I define a delegate with one parameter in my case a FName.
I’m a delegate instance on my volume to use it.
In my volume constructor I replace the OnOverlap function with my own with the addDynamic macro.
After that I bind the delegate instance to the function with 1 parameter I want ( in your case it will be OnClick_button_load_game with the parameter you want)
After on my own BeginOverlap, I invoke my delegate bind to the function with one parameter as you can see.
For more infos on delegate you can here : Delegate doc
void UMasterUI_Menu::OnButtonClicked() //same for all buttons, binded inside while loop at runtime
{
testdelegate.Execute(/*still no idea how i can get clicked button here*/);
}
Please, help me. Maybe there is another way of doing that.
For now it’s simple UScrollBox with UButtons spawned in c++ code(default class). I have widget blueprint too, with some panel widgets (it’s easier to make good menu design here).
Ok thanks for that info, what I propose you is to create your own UButton subclass (that contains slate stuff) so you will be able to define your delegate attach to the Onclicked Event on your button and with that method, you’ll be able to catch the infos you need and use the code I shown you.
Did you follow me ?
I could also make an example for you if you need (with button this time to stick to your problem)
Hello, i’m building your example and I wanna know your UMasterUI_Menu is a custom class you made?
What’s the type of it? I just want to know a bit of your achitecture to stick as most as possible on your situation.
For the moment I builded a custom UserWidget that has a ScrollBox but before going any further, I’d like to know your actual architecture related to your UI.
Did you put your UMasterUI_Menu on the gameMode, a customHUD or elsewhere?
Also can you send me a partial header and source (all I want, it’s your constructor and the init) of your UMasterUI_Menu. My example is a bit fuzzy and Don’t want to lost you. If you don’t want, I’ll explain the best I can.