Hello. First time poster on here, hope I am doing it properly.
I am having an extremely hard time trying to figure this out.
What I am aiming to do here is create semi-random number of rows, with semi-random number of buttons in it.
There’s a boolean that gets checked everytime a button is pressed and if that boolean is true, it unlocks the next button ( First button has it on true on event construct ). As shown in the video, first row works just fine, while all the other rows kinda work. When pressing for example 1st button on 3d row, it unlocks 2nd button from 2nd row and so on.
I can’t for the life of me figure out why this is happening or if there would be a better way of doing this.
Any help is appreciated
Upon constructing the button you are Looping through the RowContainer array. The Foreach output pin will keep a ref to its last value, being the last element of the array at that time. I assume you’re constructing buttons in RowContainer’s construction script, meaning at that time the current RowContainer element has not yet been added to the RowContainer array, so the last element of the array contains the previous row, which is being stored into that ArrayElement pin.
Generally, it’s considered bad practice to :
- use the output pins of For/Foeach loop output pins outside of the LoopBody
- use the output pins of one execution flow (Construct) during a different execution flow (OnClicked). While output pins of exec nodes do behave like global variables in practice, it’s generally not a great idea to use them that way.
Here you have a much simpler way to do things. You even did it partially already, with that ClickedRoomIndex variable.
- in your Button widget, add a variable of type RowContainer widget reference, make it expose on spawn.
- now when generating button, an additional pin should appear in CreateWidget for that variable. Connect a “self” node (self being the row container) to it
- now your button can access its owning row container directly through that variable. No need to access game mode and reach to the row containers array.
Also, when generating buttons, you could connect directly the index from the ForLoop, into the ClickedRoomIndex pin in CreateWidget node. The “NextButtonIndex” counter is unnecessary.
Just now got the time to check this out.
I can’t thank you enough. I’ve been at it for so many days and it was this simple actually.
Thank you so much for the extra advices!