To begin the first 2 things you will need to know is how to define a function and how to register that function:
Code:
function Item_OnUse(Item,Event,player)
This creates a function called "Item_OnUse", the three names within the brackets "Item", "Event" and "player" are all variables which the data from which ever unit uses that function. "Item" stores the unit data, "Event" stores the event data and player stores the account data as to which player used the item.
All functions must end in
Now this function does nothing in this state because nothing is calling it. So here here is how to register that function so that your core knows what to do:
Code:
RegisterItemGossipEvent(125667, 1, "Item_OnUse")
Now in this line of code the core is told that when the item 125667 is clicked the function "Item_OnUse" is called.
So to brake down each peice:
RegisterItemGossipEvent = A command that tells the core that an item is controlled in the function specified, and it is being used for making a menu
125667 = The itemid of the item being used.
1 = The way in which the object is being interacted with, 1 states the item has been clicked
"Item_OnUse" = The function being called.
Now we know how to begin a function and register it lets try adding a menu to the item.
The first line we will be using looks like this
Code:
item:GossipCreateMenu(100, player, 0)
This line of code creates a menu it does not dislpay anything but stores data when items are added to the menu.
To brake down the data:
item = the variable which was defined before
GossipCreateMenu = A command defining what the item should do, in the case it add a new menu into memory
100 = the menus id
player = the variable which we defined before
0 = a value that should always be 0
Now that we can create menus why not add lines to them:
Code:
item:GossipMenuAddItem(0, "My First Menu Item", 1, 0)
This peice of code adds a line to the menu.
To brake down each peice:
item = the variable which was defined before
GossipMenuAddItem = A command which adds a line to the menu which we have already created
0 = the picture used before the text of the menu item, 0 is a speech bubble
"My First Menu Item" = The words that will be displayed for the menu
1 = The menu item id (if the item is clicked the value 1 is stored in a variable called "intid")
0 = a value that should stay at 0
Now that we have a menu with an item in it we can display the contents of that item to the player:
Code:
item:GossipSendMenu(player)
This sends what we have now made to the player..
To brake down each peice
item = the variable which was defined before
GossipSendMenu = This peice of code tells the core to send the menu and its items to the player defined
player = the variable which we defined before
So now you should have a peice of code that looks like this
Code:
function Item_OnUse(Item,Event,player)
item:GossipCreateMenu(100, player, 0)
item:GossipMenuAddItem(0, "My First Menu Item", 1, 0)
item:GossipSendMenu(player)
end
RegisterItemGossipEvent(125667, 1, "Item_OnUse")
This is the structure that all your lua should take now i will point out a few things on the structure.
A function always needs to be registered to have any effect on the item it is being scripted for. You always write what you want to happen inside a function. You must always end a function with end. Inside a function commands are executed from top to bottom so you cannot have a item:GossipSendMenu(player) before a item:GossipCreateMenu(100, player, 0) because you will get no result.
Now we have made an item in a menu and displayed to the player we must make something happen when a user clicks an item.
Will will need to register a new event for this:
Code:
RegisterItemGossipEvent(125667, 2, "Item_OnUse")
As you can see it is very similar to the first register but as you can see there is a 2 where the 1 used to be.
The 2 is in place of the one because this is now a new state, the user has clicked an item in the menu and we want something to happen. So 2 just tells the core where to go once an item has been clicked.
Now where to go:
Code:
function Item_OnSelect(item,Event,Player)
By now you should know what each of these does so i wont bother explaining but just as a reference the line "Item_OnSelect" could be anything like "Myitem_WhenYouClick" the name isnt important. you must make sure the name is that same in the register aswell.
First we must define which item has been clicked
this is made simple because like i told you earlier the number 1 was stored in a variable called intid when clicked so here we are using that functionality to figure out which item has been selected. All ifs must also end in an end
Now we need it to do something:
Code:
player:SendAreaTriggerMessage("You did it!")
This tells the player to send themselves a message up the top of their screen saying "You did it!" in red.
To brake this down:
player = a variable defined in the function.
SendAreaTriggerMessage = a peice of code that tells the player to display a message saying what ever has been defined.
"You did it!" = this is what is supposed to be displayed at the top of the players screen.
Now that we have defined that your script should look like:
Code:
function Item_OnUse(Item,Event,player)
item:GossipCreateMenu(100, player, 0)
item:GossipMenuAddItem(0, "My First Menu Item", 1, 0)
item:GossipSendMenu(player)
end
function Item_OnSelect(item,Event,Player)
if intid == 1 then
player:SendAreaTriggerMessage("You did it!")
end
end
RegisterItemGossipEvent(125667, 1, "Item_OnUse")
RegisterItemGossipEvent(125667, 2, "Item_OnUse")
Now you have a fully functioning menu with an output. But because there is only one item on the menu it will skip the menu and jump into the next function.
If you would like to see the menu try adding a few items to it and giving a few more results i will add one for you.
Code:
function Item_OnUse(Item,Event,player)
item:GossipCreateMenu(100, player, 0)
item:GossipMenuAddItem(0, "Make my model change please", 1, 0)
item:GossipMenuAddItem(0, "Give me an item please", 2, 0)
item:GossipSendMenu(player)
end
function Item_OnSelect(item,Event,Player)
if intid == 1 then
player:SetModel(367)
end
if intid == 2 then
player:AddItem(27978)
end
end
RegisterItemGossipEvent(125667, 1, "Item_OnUse")
RegisterItemGossipEvent(125667, 2, "Item_OnUse")
I hope this guide is helpful as it is my first ever ^^ oh and ive been doing lua for 6 days now <3. I would also like to know how i can imporve the way i structured the tutorial so if u guys could help me out that would be great.
Any questions please post em as i would love to help you guys out