
Results 1 to 5 of 5
Threaded View
-
17-03-10, 09:37 PM #2
- Rep Power
- 0
- Reputation
- 87
Variables.
A variable is a byte of data, represented by a name, that stores a defined piece of data. Unlike C++, there are no variable types in Lua. Also, there is no need to worry about unsigned/signed variables.
A variable is created by typing a string into the script. This string can be almost anything; however, it cannot be keywords or function names (You'll know if it is a keyword if you're using Notepad++, as it'll be highlighted a bold blue).
Furthermore, variables can only be alphanumeric and have underscores and/or hyphens in them. You can use these characters:
Code:a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0 _
Variables can be declared with no identifier (Global) or with local infront of them. A global variable can pass from script to script, so it can cause problems. It's best to use local variables, which stay inside the current script, to reduce interference.
Good Variable Names:
Code:My_Var = 1 myVar = 2
Code:%MyVar = 1 -- PHP Users may be used to this. My.Var = 2 My&Var = 3
Code:local Var983
Code:local Npc_Id
Back to your First Gossip NPC.
Code:--[[ My First Gossip Script! Tutorial by Neglected ]] -- Variables local NPC_ID = 133713 -- On Triggers -- RegisterUnitEvents
First off, let's create a new function called 'exampleGossipOnTalk(Unit, Event, player)'. These arguments are the standard ones for the OnTalk gossip event. The OnTalk gossip event is the first menu/dialog you see when you talk to the NPC.
Code:--[[ My First Gossip Script! Tutorial by Neglected ]] -- Variables local NPC_ID = YourEntryID -- On Triggers function exampleGossipOnTalk(Unit, Event, player) end -- RegisterUnitEvents
Note that I added an end below the function. We need to end every function that is created, or the Lua Engine will chuck up errors. Now that we have created our function, let's add a RegisterUnitEvent. Usually, if you are created a boss, you will use RegisterUnitEvent(), but since we are making a Gossip NPC, we use RegisterUnitGossipEvent. Note that this is case-sensitive. Add this underneath the appropriate section;
Code:RegisterUnitGossipEvent(NPC_ID, 1, "exampleGossipOnTalk")
Code:RegisterUnitGossipEvent(ID, EVENT_ID, FUNCTION_NAME)
Code:function exampleGossipOnTalk(Unit, Event, player) end
Code:function exampleGossipOnTalk(Unit, Event, player) -- Statements go here end
Code::GossipCreateMenu(TEXT_ID, player, INTID)
Code:function exampleGossipOnTalk(Unit, Event, player) Unit:GossipCreateMenu(100, player, 0) end
So now we've created a menu that displays 'Hi, <name>. How can I help you?'. It's still pretty impractical and won't help anyone in any shape, or form (Unless they want a pick-me-up). To make this NPC useful, let's add a few options to our menu by using the :GossipMenuAddItem() statement.
Code::GossipMenuAddItem(ICON_ID, MENU_CONTENT, INTID[, CODE])
Code:0 = Chat bubble 1 = Bag 2 = Wings 3 = Book 4 = Cog/Gear 5 = Cog/Gear 6 = Bag with coin 7 = Chat bubble with "..."
Code::GossipMenuAddItem(0, "|cfFFFFFF White! |r", 1, 0)
INTID? Well, when you use the OnSelect function later, the INTID is used to determine which menu was opened. Therefore, you have to use unique IntIDs for each Menu selection, or the script will not work. Also, the IntID is in numerical form and not string.
CODE is an optional... option.. that can be omitted. It is only used when you want to load up a codebox. This will not be explained in this tutorial, and you will have to experiment to get it completely correct. Needless to say, leave it at 0 (False) unless you want it to load up, then set it to 1 (True).
I'll add a few options to our Gossip Menu..
Code:function exampleGossipOnTalk(Unit, Event, player) Unit:GossipCreateMenu(100, player, 0) Unit:GossipMenuAddItem(0, "Teleport me to the mall.", 1, 0) Unit:GossipMenuAddItem(0, "Remove Resurrection Sickness.", 2, 0) Unit:GossipMenuAddItem(0, "Never mind.", 3, 0) end
Code:Unit:GossipSendMenu(player)
Code:function exampleGossipOnTalk(Unit, Event, player) Unit:GossipCreateMenu(100, player, 0) Unit:GossipMenuAddItem(0, "Teleport me to the mall.", 1, 0) Unit:GossipMenuAddItem(0, "Remove Resurrection Sickness.", 2, 0) Unit:GossipMenuAddItem(0, "Never mind.", 3, 0) Unit:GossipSendMenu(player) end
Code:--[[ My First Gossip Script! Tutorial by Neglected ]] -- Variables local NPC_ID = 133713 -- On Triggers function exampleGossipOnTalk(Unit, Event, player) Unit:GossipCreateMenu(100, player, 0) Unit:GossipMenuAddItem(0, "Teleport me to the mall.", 1, 0) Unit:GossipMenuAddItem(0, "Remove Resurrection Sickness.", 2, 0) Unit:GossipMenuAddItem(0, "Never mind.", 3, 0) Unit:GossipSendMenu(player) end -- RegisterUnitEvents RegisterUnitGossipEvent(NPC_ID, 1, "exampleGossipOnTalk")
Code:exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc)
Code:--[[ My First Gossip Script! Tutorial by Neglected ]] -- Variables local NPC_ID = 133713 -- On Triggers function exampleGossipOnTalk(Unit, Event, player) Unit:GossipCreateMenu(100, player, 0) Unit:GossipMenuAddItem(0, "Teleport me to the mall.", 1, 0) Unit:GossipMenuAddItem(0, "Remove Resurrection Sickness.", 2, 0) Unit:GossipMenuAddItem(0, "Never mind.", 3, 0) Unit:GossipSendMenu(player) end function exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc) end -- RegisterUnitEvents RegisterUnitGossipEvent(NPC_ID, 1, "exampleGossipOnTalk")
Code:--[[ My First Gossip Script! Tutorial by Neglected ]] -- Variables local NPC_ID = 133713 -- On Triggers function exampleGossipOnTalk(Unit, Event, player) Unit:GossipCreateMenu(100, player, 0) Unit:GossipMenuAddItem(0, "Teleport me to the mall.", 1, 0) Unit:GossipMenuAddItem(0, "Remove Resurrection Sickness.", 2, 0) Unit:GossipMenuAddItem(0, "Never mind.", 3, 0) Unit:GossipSendMenu(player) end function exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc) end -- RegisterUnitEvents RegisterUnitGossipEvent(NPC_ID, 1, "exampleGossipOnTalk") RegisterUnitGossipEvent(NPC_ID, 2, "exampleGossipOnSelect
ID; Not a clue. :3
code; This is used if you enabled code in the Previous menu.
pMisc; this is the name of the person who started the Gossip (?).
None of these are much use to you, because you probably won't use them. At least, not in this tutorial. So, let's add the first option (of the 3) to our new function.
Code:function exampleGossipOnSelect(Unit, Event, player, id, intid, code, pMisc) if (intid == 1) then player:Teleport(MapID, x, y, z) player:GossipComplete() end end
BRAIN FREEEZE!
Err.. OK then, panic moment over. Let's work through this.. deep breaths.. in.. out..