OnNuiEvent
The script attached to this event fires when the player clicks on a NUI widget (that has an NuiId. It must be set using SetEventScript (for instance in OnModuleLoad).
Trigger
When a NUI widget is interacted with.
Function(s)
Setup
Note: This can be setup in Module Properties (as of Patch: Patches#1.87). Previous versions would need to be setup via Script (see below)
Module Properties
In the Toolset goto Edit->Module Propertes->Events. There is a drop down for "OnNuiEvent". You can select your nui event script there.
SetEventScript
Alternatively, this could also be done in scripting, like so :
Remarks
This new EE NUI allows custom GUI displays and this allows module to process any interactions with those GUIs.
Known Bugs
This is NOT a bug, but a common mistake is not assigning an NuiId to a Widgets. Events will not fire for a widget if it doesn't have a NuiId.
Version
See the main page for NUI Version Information
Example
This sets up a global NUI event handler which then passes off to other functions. Something like this would be useful if you have more than one NUI in your module.
void main() {
object oPlayer = NuiGetEventPlayer();
int nToken = NuiGetEventWindow();
string sEvent = NuiGetEventType();
string sElement = NuiGetEventElement();
int nIndex = NuiGetEventArrayIndex();
string sWindowId = NuiGetWindowId(oPlayer, nToken);
// Debug information for when first setting up.
SendMessageToPC(GetFirstPC(), "Receive an OnNuiEvent. Player: " + GetName(oPlayer) + " nToken: " + IntToString(nToken) + " sEvent: " + sEvent + " sElement: " + sElement + " nIndex: " + IntToString(nIndex) + " sWindowId: " + sWindowId);
// If this is my window ID, let my function handle it.
if (sWindowId == "MyNuiWindowId")
{
MyNuiWindow_EventHandler(oPlayer, nToken, sEvent, sElement, nIndex, sWindowId);
return;
}
// Or an alternative way to say the same thing. If it's handled, it will return TRUE.
// But this function must be well behaved and return TRUE if it's meant to be handled there.
if (MyOTHERNuiWindow_EventHandler(oPlayer, nToken, sEvent, sElement, nIndex, sWindowId))
{
return;
}
// Or events can be handled here directly.
// This will print "Test!" if a button is clicked on Button ID "mynuitestwindow_button_test" that belongs to the "MyNuiTestWindow" window.
if (sEvent == "mouseup" && sWindowId == "MyNuiTestWindow" && sElement == "mynuitestwindow_button_test")
{
SendMessageToPC(GetFirstPC(), "Test!");
return;
}
// Or lastly, you could run a script on an event. This is sometimes useful for 3rd party NUIs that expect to the be main and only OnNuiEvent handler.
if (sWindowId == "MyNuiWindowId_Number4") {
ExecuteScript("num4_evthdler");
return;
}
// This only displays if we get an event that wasn't handled above.
SendMessageToPC(GetFirstPC(), "WARNING: Receive an OnNuiEvent, but nothing handled (claimed) it. Please check. Player: " + GetName(oPlayer) + " nToken: " + IntToString(nToken) + " sEvent: " + sEvent + " sElement: " + sElement + " nIndex: " + IntToString(nIndex) + " sWindowId: " + sWindowId);
}
See Also
functions: |