From 00a47dcd08e9e2a77a7d12b4717dccd68c418df5 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Mon, 5 Feb 2024 00:40:40 +0100 Subject: [PATCH] Replace callback method --- [gameplay]/dialogs/readme.md | 84 +++++++++++++++++++--------------- [gameplay]/dialogs/sourceC.lua | 17 +++---- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/[gameplay]/dialogs/readme.md b/[gameplay]/dialogs/readme.md index 1e1fa31e9..1a94dbb38 100644 --- a/[gameplay]/dialogs/readme.md +++ b/[gameplay]/dialogs/readme.md @@ -47,72 +47,80 @@ This function creates a dialog box with a callback function. **Syntax** ```lua -messageBox( string messageTitle, string messageContent, function messageCallback, [ string messageIcon, string messageButton, int messageButtonDefault, string messageSound, int messageSoundVolume] ) +messageBox( string messageTitle, string messageContent, string messageCallback, [ string messageIcon, string messageButton, int messageButtonDefault, string messageSound, int messageSoundVolume] ) ``` **Example** > This example creates a dialog containing an information. ```lua -messageBox("Welcome", "Multi Theft Auto provides the best online Grand Theft Auto experience there is.", - function(callbackResult) - if callbackResult == "OK" then - print("Read") - end - end, -"INFO", "OK") +messageBox("Welcome", "Multi Theft Auto provides the best online Grand Theft Auto experience there is.", "callbackFunction", "INFO", "OK") +``` + +```lua +function callbackFunction(callbackResult) + if callbackResult == "OK" then + print("Read") + end +end ```
> This example creates a dialog containing a question. ```lua -messageBox("Save", "Are you sure you want to save your changes? This action will overwrite your previous saves.", - function(callbackResult) - if callbackResult == "YES" then - print("Save") - elseif callbackResult == "NO" then - print("Undo") - else - print("Cancel") - end - end, -"QUESTION", "YESNOCANCEL") +messageBox("Save", "Are you sure you want to save your changes? This action will overwrite your previous saves.", "callbackFunction", "QUESTION", "YESNOCANCEL") +``` + +```lua +function callbackFunction(callbackResult) + if callbackResult == "YES" then + print("Save") + elseif callbackResult == "NO" then + print("Undo") + else + print("Cancel") + end +end ```
> This example creates a dialog containing a warning. ```lua -messageBox("Mismatch", "The selected file does not exist. Would you like to try the download again?", - function(callbackResult) - if callbackResult == "ABORT" then - print("Abort") - elseif callbackResult == "RETRY" then - print("Retry") - else - print("Ignore") - end - end, -"WARNING", "ABORTRETRYIGNORE") +messageBox("Mismatch", "The selected file does not exist. Would you like to try the download again?", "callbackFunction", "WARNING", "ABORTRETRYIGNORE") +``` + +```lua +function callbackFunction(callbackResult) + if callbackResult == "ABORT" then + print("Abort") + elseif callbackResult == "RETRY" then + print("Retry") + else + print("Ignore") + end +end ```
> This example creates a dialog containing an error. ```lua -messageBox("Your wallet is empty", "You do not have enough money to purchase the selected vehicle.", - function(callbackResult) - if callbackResult == "Ok" then - print("Read") - end - end, -"ERROR", "OK") +messageBox("Your wallet is empty", "You do not have enough money to purchase the selected vehicle.", "callbackFunction", "ERROR", "OK") +``` + +```lua +function callbackFunction(callbackResult) + if callbackResult == "OK" then + print("Read") + end +end ``` **Arguments** > - messageTitle: A string specifing the title of the message (note that it will be always uppercase) > - messageContent: A string specifing the content of the message -> - messageCallback: A function that is called when the user presses a button. Use the callbackResult parameter to register the result. +> - messageCallback: A string specifying the name of the function that will be exported when a button is pressed. (Note that you must specify the function name as a string and specify the function as exportable in the meta.xml of the source.) > - messageIcon: A string specifing the icon of the message, which is in the **messageIcons** table. > - messageButton: A string specifing the button(s) of the message, which is in the **messageButtons** table. > - messageButtonDefault: An integer specifing the default button (note that it will receive a bolder font) diff --git a/[gameplay]/dialogs/sourceC.lua b/[gameplay]/dialogs/sourceC.lua index d2df21515..476219ea2 100644 --- a/[gameplay]/dialogs/sourceC.lua +++ b/[gameplay]/dialogs/sourceC.lua @@ -35,13 +35,16 @@ function messageClick() local elementBox = messageBoxWindows[elementParent] if elementBox then - local elementCallback = messageBoxCallbacks[elementParent] + local elementCallback = messageBoxCallbacks[elementParent][1] + local elementCallbackSource = messageBoxCallbacks[elementParent][2] local elementCallbackResult = guiGetText(source) elementCallbackResult = utf8.upper(elementCallbackResult) - if elementCallback and type(elementCallback) == "function" then - elementCallback(elementCallbackResult) + if elementCallback and type(elementCallback) == "string" then + if elementCallbackSource and getResourceState(elementCallbackSource) == "running" then + call(elementCallbackSource, elementCallback, elementCallbackResult) + end end destroyElement(elementParent) @@ -72,12 +75,6 @@ function messageBox(messageTitle, messageContent, messageCallback, messageIcon, error("Bad argument @ 'messageBox' [Expected string at argument 2, got " .. type(messageContent) .. "]") end - if not messageCallback or type(messageCallback) ~= "function" then - messageCallback = function() - outputDebugString("Since you did not assign a callback function to this message, we replaced it with an empty function.", 2) - end - end - if not messageIcon or type(messageIcon) ~= "string" then messageIcon = "INFO" end @@ -183,7 +180,7 @@ function messageBox(messageTitle, messageContent, messageCallback, messageIcon, end messageBoxWindows[messageWindowElement] = {messageTitle, messageContent, messageIcon, messageButton} - messageBoxCallbacks[messageWindowElement] = messageCallback + messageBoxCallbacks[messageWindowElement] = {messageCallback, sourceResource} end function messageBoxEx(messageTitle, messageContent, messageIcon, messageButton, messageButtonDefault, messageSound, messageSoundVolume)