User Tools

Site Tools


developer_center:developer_editor:script:lucky_number_game

Lucky Number

I. Diagram

II. Design

You need to add a input box. Player enter string in the input box

III. Script

local _input_string = nil --- fill your element	
function game(event) 
	if _input_string == event.uielement then
      	local variable_lucky_number = math.random(0, 100)
      	if string.find(event.content, variable_lucky_number) then
          		Chat:sendSystemMsg("Congratulation! Your lucky number is: " .. variable_lucky_number)
      	else
          		Chat:sendSystemMsg("Error Code " .. variable_lucky_number)
      	end
  		end
end
ScriptSupportEvent:registerEvent('UI.LostFocus',game)

IV. Explanation

Line 1:

local _input_string = nil --- fill your element	
  • Local: This keyword is used to declare a variable with local scope, which means it’s only accessible within the block of code where it’s defined.
  • _input_string: This is a variable name. You can change to another name, but if your variable has 2 letters, you need to add _ to replace for space character
  • nil: This is a special value, it doesn't have any specific values. You need to change it to fit your map
  • - - - fill your element: This comment line will help others understand your code

Line 2 - 10:

function game(event) 
	if _input_string == event.uielement then
      	local variable_lucky_number = math.random(0, 100)
      	if string.find(event.content, variable_lucky_number) then
          		Chat:sendSystemMsg("Congratulation! Your lucky number is: " .. variable_lucky_number)
      	else
          		Chat:sendSystemMsg("Error Code " .. variable_lucky_number)
      	end
  		end
end
  • function game(event) … end: for Lua script, you need to call the function. It is the same actions in trigger.
  • Function must have end and a variable name, here “game” is variable name“. You need it to register the event.
  • if _input_string == event.uielement then … end: This condition will compare your string with lucky number, if they're the same - the game will chat ” Congratulation! Your lucky number is: “ .. variable_lucky_number”. However, they aren't the same - the game will chat “ Error Code ” .. variable_lucky_number“.

Line 5:

      if string.find(event.content, variable_lucky_number) then
      
  • if: This starts a conditional statement in Lua.
  • string.find: This is a function in Lua’s string library that searches for a pattern in a given string. It returns the start and end indices of the first occurrence of the pattern if it’s found, or nil if it’s not found.
  • event.content: This likely refers to a string that is part of an event object, which might contain text that you want to search through.
  • variable_lucky_number: This is a variable that holds the pattern you’re searching for within event.content.
  • then: This indicates the beginning of the block of code that will execute if the condition is true.

The last line:

ScriptSupportEvent:registerEvent('UI.LostFocus',game)
  • ScriptSupportEvent:registerEvent(EventName, FunctionToExecute): This method is used to register an event listener.
  • EventName: This is the name of the event you want to listen for.
  • FunctionToExecute: This is the function that will be called when the event occurs.

If you have any questions about Developer Tools, feel free to join our official discord server Mini World Global DEV discord

developer_center/developer_editor/script/lucky_number_game.txt · Last modified: 2024/05/05 15:34 by don