Bind OnClicked Events to buttons created at runtime.
Hello Everyone ,
I have scrollbox with buttons created at runtime based on number of .sav files in game folder.
Each button represents one save file, that can be loaded. I'm trying to bind events to that buttons at runtime:
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.
I need c++ solution, not BP (i'm using c++ only).
asked Aug 08 '15 at 12:33 PM in C++ Programming
So each button has his delegate and when you clicked, it will fire up.
You could also make a delegate that return stuff from your button and after call the load delegate from these info.
Is that help you more ?
If not just tell me.
answered Aug 09 '15 at 06:03 PM
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.
Do you follow me?
Hello again XadE, I made an example for you to understand.
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
Is my example help you ?
If not, or if you need anything, just tell me.
answered Aug 08 '15 at 08:18 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here