Trigger click event on GUI Element?

Hi.

I understand this might be unconventional, but is there a way to force trigger click event on the GUI element programmatically?

1 Like

Would it be unacceptable to do something like this?

const handleClick = () => console.log('clicked')
rect1.onPointerClickObservable.add(handleClick);
// run programatically
handleClick()

If for some reason that wouldn’t work, maybe you can get the 2d x and y coordinates of the GUI mesh like in this PG:

Then you can trigger a mouseEvent at those coordinates:

But also I am new to Babylon so maybe there is an easier way.

1 Like

I’m not sure I understand this correctly. Could you explain ā€˜force trigger click event’? What is the context? Do you want to force the user to click on the GUI control - or - do you want to trigger the event from the GUI control whenever the user clicks anywhere or somewhere else in the scene? And then, why would you want to force trigger the event from the GUI anyway. You could just run it in a function and add the visual representation of the clicked GUI control. Adding to this, you could simply have (as I believe suggested above) just a variable of ā€˜isClicked’ when running the function.

EDIT: Forgot to mention that ā€˜there’s what you CAN do and what you SHOULD do’. Again, I have no clue about the context or whether it’s Option A (force click on control) or Option B (trigger event from control when clicking outside)…but just BEWARE, If it is option B, there’s more than just the tech aspect (and also why this is, as you say ā€˜unconventional’). Imagine the button would be like a consent ā€˜I AGREE’. It might be obvious to say in this case but I’m gonna say it anyway (just to make this clear). Implementing a behavior where clicking outside this button would still trigger the related event is illegal. And then, less obvious, even in the case of simple ā€˜game action’, it is still likely to decrease ā€˜trust’ and affect the CX.

2 Likes

Hey guys. Thank you for you answers. I redesigned my code to avoid this issue, so I will not be attempting the force click implementation

I tried to ā€œhackā€ the process a bit, and kind of simulate what user would do in the app, so I can recreate the state of the app as I wish. A lot of stuff is happening on clicks on GUI elements, and many other events, so I thought I could force click my way to avoid state flow redesign.

Thanks guys, I will mark the issue as solved.

2 Likes