Lua Scripting Tutorials

Source: Internet
Author: User
Tags true true

--[[Tool Preparation
1. A tool that supports UTF8 without BOM encoding, for example: notepad++
2. A multi-file search tool for keywords, for example: file Seeker
3.Eluna the source of the corresponding end
]]--

--[[website Related
Eluna Source
Https://github.com/eluna-dev-mangos/ElunaCoreWotlk
Https://github.com/ElunaLuaEngine/Eluna-TC-Wotlk

Sample source Code
Https://github.com/ElunaLuaEngine/Scripts
]]--

--[[lua Basic Syntax-Comments
Single-line Comment:--
Multiline Comment:--[[note content]]----[=[note content can have [] such a symbol]=]--
]]--

--[==[lua Basic Syntax-variables
Keywords (cannot do variables):
And break do else ElseIf
End False for function if
In local nil not OR
Repeat return then true until while
Variable type:
Nil null value
Boolean 2, True True/False false
Number value, which can be a decimal, hexadecimal 0x10
String string that, if you wish, can contain the ' \ S ' character.
Table table, like an array, usually with the {} symbol, the table can contain multiple tables, LUA so subscript is starting from 1
function functions
UserData player,object,item,map,quest ...

Local a=10 (loacal) variable, valid only in the current file
A=10 (default) global variable, valid for all LUA, overrides

Numerical:
Local a=123456
Local b=0x10
Local c=3.14159265358
String:
Local str1= "123"
Local str2= "Hello"
Local str3=[[This is a multi-line string
This is a multi-line string
]]
Local str4=[=[This is multi-line [string]
This is multi-line [string]
]=]
Form:
Local t={
"A",
123,
{"A", "B"},
{123, "C"},
b=456,
["10"]=123,
["ASD"]= "HI",
}
Get the contents of the table:
T[1]= "a"
T[2]=123
t.b=456
t["10"]=123
t.asd= "HI"
T[3][1]= "a"
T[4][2]= "C"
Get a table with a for loop

--Applies only to tables that are not indicated by []
For k,v in pairs (T) do
K=1,V=T[1]
K=2,V=T[2]
End

Function:
In Lua, functions are best used with loacal to reduce the probability of server-side-to-machine
Local function Fun1 ()

End

Local t={}

function t.fun2 ()-because T is a local variable, so the Fun2 function of T is also a local variable

End

]==]--

--[[lua basic syntax-base Function library
String:
Local str= "123456"
String connection: Local str2=str ... " 789 "=" 123456789 "
Mode 1:
String.find (str, "1") =1 find where the specified character appears
String.sub (str,2,4) = "234" clipping string
String.sub (str,2) = "23456" clipping string
String.len (str) =6 string length
String.Format ("%s is%d children", "xiaoming", 1) = "Xiaoming is a 1 child" string formatting
Mode 2:
Str:find ("1") =1
Str:sub (2,4) = "234"
Str:sub (2) = "23456"
Str:len () =6
"%s is%d children": Format ("Xiaoming", 1) = "Xiaoming is 1 children"

Numerical:
Local A,B=MATH.MODF (1/3) a=0, b=0.3333333 A is quotient, B is the remainder

Time:
Local secs=os.time () seconds, details can be directly Baidu
Date:
Local Ts=os.date ("*t", Time)
Local T=string.format (%d%d months%d days%d%d seconds, ts.year,ts.mon,ts.day,ts.hour,ts.min,ts.sec)
]]--

--[[lua Basic Syntax-operations
+-*/%^ not
Not generally used to reverse the true and false

]]--

--[[lua Basic Grammar-judging, looping
Judging if
Local A=nil
Local b=0
Local C=false

if (expression) then--the expression equals nil or false to false and the other is true. If true, the then content is executed, otherwise the else content

Else

End

Loop for
For i=1,10 do--from 1 to 10, per default +1

End

For i=1,10,2 do--from 1 to 10, +2 per time

End

For I=10,1,-1 do--from 10 to 1, 1 per time

End

]]--

--[[eluna start
LuaFunctions.cpp all functions
HookMgr.h All Event Events
The register inside the LuaFunctions.cpp ... function is to add a monitor to the specified game object
(Event, function)
When the specified game object takes on the event you want (found in the HookMgr.h file), the function is called
Part of the register requires a specified item or other entry

]]--

--eluna Example-Add a menu or function to an item
--Note: Create LUA, remember in the notepad++ Format menu, select UTF-8 No BOM encoding
--1. First in LuaFunctions.cpp, find the Register function of the item
Registeritemevent (entry, event, function)
Registeritemgossipevent (entry, event, function)
--2. Then find the menu-related event in HookMgr.h
--[[only in Registeritemevent (entry, event, function) event
Enum Itemevents
{
Item_event_on_dummy_effect = 1,//(EVENT, Caster, Spellid, Effindex, item)
Item_event_on_use = 2,//(EVENT, Player, item, target) items used
Item_event_on_quest_accept = 3,//(EVENT, Player, item, QUEST) Item acceptance task
Item_event_on_expire = 4,//(EVENT, player, Itemid)
Item_event_count
};]] --
--only in the event of Registeritemgossipevent (entry, event, function)
--[[enum gossipevents
{
Show Menu
Gossip_event_on_hello = 1,//(EVENT, Player, object)-object is the Creature/gameobject/ite M
Select Menu
Gossip_event_on_select = 2,//(EVENT, Player, object, sender, IntID, Code, menu_id)-Object Is the Creature/gameobject/item/player, menu_id are only for Player gossip
Gossip_event_count
};]] --
--3. Found 3 related
--item_event_on_use and Gossip_event_on_hello are the same, in order to facilitate the general use of Gossip_event_on_hello
--gossip_event_on_select
--4. It is now necessary to specify the entry of the item and the item must be available.
Local itementry=6948--Furnace Stone
--5. Creating a function
--gossip_event_on_hello corresponding function parameters (EVENT, Player, object)
--gossip_event_on_select corresponding function parameters (EVENT, Player, object, sender, IntID, code, MENU_ID)
--so we created 2 local functions
Local function book (event, player, item)

End
Local function Select (event, player, item, sender, IntID, code, MENU_ID)
---processing according to the Sender,intid of the clicked menu and the input code (is a string)
End
--6. Finding related functions
--We need to add a menu, and a function to display the menu, using the "GOSSIP" search function
Player:gossipmenuadditem (icon, MSG, sender, intid[, code, Popup, Money])--Add menu
Player:gossipsendmenu (Npc_text, unit[, menu_id])--Send menu (show)

--7. Function analysis
-NOTE: [parameter] is a description [] The parameters inside can not be
Player:gossipmenuadditem (icon, MSG, sender, IntID)
--(menu icon number, item text, Sender,intid,code need input (true-need/false-not required), confirm prompt text, hint to spend copper
Player:gossipmenuadditem (icon, MSG, sender, IntID, code, popup, Money)
--(Menu page text number, menu owner, here is Item)
Player:gossipsendmenu (Npc_text, unit[, menu_id])--unit= only need to give a value when the player is menu_id
Player:gossipcomplete ()--Close the menu, generally put to the Select function, after the menu is clicked, you can click again will not trigger the SELECT function, so you need to close
Player:gossipclearmenu ()--clear menu, usually used before adding a menu
--8. Finding constants built into functions
--[[we don't know what icon,npc_text is.
This is the time to use file seeker or the same tool,
A. Select the source of the folder server (because Eluna may be the constant of the caller)
B. Select file Type H CPP
C. Enter keywords, search
]]--
--gossip_icon menu icon
Local Gossip_icon_chat = 0--Conversation
Local Gossip_icon_vendor = 1--Cargo
Local Gossip_icon_taxi = 2--Transfer
Local Gossip_icon_trainer = 3--Training (book)
Local gossip_icon_interact_1 = 4--Resurrection
Local gossip_icon_interact_2 = 5--Set as my home
Local Gossip_icon_money_bag = 6--Money bag
Local Gossip_icon_talk = 7-application speech + black dot
Local Gossip_icon_tabard = 8--Union (shirt)
Local Gossip_icon_battle = 9--Join the battlefield double sword crossover
Local Gossip_icon_dot = 10--Join the battlefield
--gossip_option
Local Gossip_option_none = 0--unit_npc_flag_none
Local GOSSIP_OPTION_GOSSIP = 1--Unit_npc_flag_gossip

--Using
Player:gossipsendmenu (0, unit[, menu_id])
--If a variable is declared
Player:gossipsendmenu (Gossip_option_none, unit)
--9. Registering monitoring functions
Registeritemgossipevent (Itementry, 1, book)
Registeritemgossipevent (Itementry, 2, Select)
--Note:
The--lua is from top to bottom, and if the following 2 register functions are placed above the book,select, they will fail.

Lua Scripting Tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.