PDA

View Full Version : [EPIC] detailed LUA guide



SerialKiller
20-02-10, 06:35 AM
I DID NOT DO THIS ALL CREDITS GO TO VISION1000

If anything is wrong with this guide, or information is incorrect, please post back and I'll correct it, Be aware I'm not an Lua expert, i learned by looking at many Lua guides, and by messing around and testing out stuff in-game. These are the concepts i feel i have a pretty good understanding of so far. If there is anything i'm missing in this guide, also post and i'll add it in. (Also say if any of the examples / sections of this guide are unclear, i'll try my best to fix them Edit: I've been learning C++, and well... if you have the time, its definitely worth learning over lua.)

After reading through all the other guides that are vague and leave out very basic information that you are expected to know,
but do not.
i decided to make a very very thorough one.


[Table of Contents]

[1.0]: General
[1.1]: Functions & Registering Events
[1.2]: Variables
[1.3]: The If Statement
[1.4]: Command List & Usage

[2.0] Creature Events
[2.1]: Most Common Creature Commands & Usage
[2.5]: Combat and General Events
[2.9]: Creature Summary and Tips

[3.0] Gameobject Events.
[3.1] Most Common Gameobject Commands & Usage
[3.5] Gameobject Events
[3.9] Gameobject Summary and Tips

[5.0] Generalized Gossip Events.

[$.#]3x7r4 1Nf0rNm471on


.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.: .:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.: .:.:.:.:.:.:.:.:.:.:.:.:.

[1.0]General


[1.1]Functions & Registering Events


Functions are blocks of code created to be called upon and do a specific task when your script is executed. The only way to initially call on a function is by having a certain Event happen to the scripted Unit(Whether it be a Creature, Gameobject, or Item)
These triggering events are;

-Accepting a Quest
-Completing a Quest
-Canceling a Quest
-Activating a Quest Gameobject
-Killing a Creature Required for a Quest
-Exploring an Area for a quest
-Picking up an Item for a Quest
-Creature Entering Combat
-Creature Leaving Combat
-Creature Killing an Enemy
-Creature Dieing
-Creature being Parried
-Creature being Dodged
-Creature being Blocked
-Creature being Critically Hit
-Creature Parrying
-Creature Dodging
-Creature Blocking??
-Creature Critically Hitting
-Creature Hitting
-Creature Feared
-Creature Flee
-Creature Loading
-Creature Reaching a Waypoint
-Creature having loot taken from it
-Creature Emoting
-Gameobject being Created
-Gameobject Spawning
-Gameobject having loot taken from it
-Gameobject being clicked/used
-Gameobject Despawning
-Talking to a Gossip Unit
-Selecting an option from a Gossip Unit
-Closing a Gossip Window / Ending it
The Skeleton if you will, of a function must always look like this;

function FunctionName(Unit, Event)

endWhen this function is called it will execute what is between the Skeleton. As stated above, it can only be initially called by a trigger event. To have a function called when a specific event happens
you must register that function to do so. Below is the syntax used to register different types of Events.


RegisterUnitEvent(NPCID, FLAG, "FunctionName")
RegisterUnitGossipEvent(NPCID, FLAG, "FunctionName")
RegisterItemGossipEvent(ItemID, FLAG, "Item_Trigger")
RegisterGameObjectEvent(GoID, FLAG, "FunctionName")
Replacing the NPC/item/Go Id's with the ones you will spawn in the world and use. the 'FLAG' is a number that indicates the event that the registered function will trigger on. Here is a breakdown of how to register an event.

[NOTE]When Registering Events, you type it at the bottom or top of the script.
Please remember that no function has to have any particular name. Not having "OnCombat" in the function name, won't affect if it gets triggered on combat, as long as its function name matches the name your registering, and you have the correct flag.



function Creature_OnCombat(Unit, Event)

end

RegisterUnitEvent(6, 1,"Creature_OnCombat")

6: Is the NPCID of a Kobold Vermin, Change it to the NPCID you want to script.
1: Is the Flag of the type of event you want this registered function to get triggered on.
"Creature_OnCombat": Is the name of the function your registering to be executed when the Kobold enters combat.
RegisterUnitEvent: Is used to Register a Unit/Creature Event.

NPCID was 6 (For Kobold Vermin)
EventFlag was 1 (To Register the function Creature_OnCombat when the kobold vermin enters combat
FunctionName "Creature_OnCombat"
Below is a Complete List of EventFlags you can use when registering an event.



[QUEST EVENTS]
QUEST_EVENT_ON_ACCEPT = 1,
QUEST_EVENT_ON_COMPLETE = 2,
QUEST_EVENT_ON_CANCEL = 3,
QUEST_EVENT_GAMEOBJECT_ACTIVATE = 4,
QUEST_EVENT_ON_CREATURE_KILL = 5,
QUEST_EVENT_ON_EXPLORE_AREA = 6,
QUEST_EVENT_ON_PLAYER_ITEMPICKUP = 7,
QUEST_EVENT_COUNT,
[CREATURE EVENTS]
CREATURE_EVENT_ON_ENTER_COMBAT = 1,
CREATURE_EVENT_ON_LEAVE_COMBAT = 2,
CREATURE_EVENT_ON_TARGET_DIED = 3,
CREATURE_EVENT_ON_DIED = 4,
CREATURE_EVENT_ON_TARGET_PARRIED = 5,
CREATURE_EVENT_ON_TARGET_DODGED = 6,
CREATURE_EVENT_ON_TARGET_BLOCKED = 7,
CREATURE_EVENT_ON_TARGET_CRIT_HIT = 8,
CREATURE_EVENT_ON_PARRY = 9,
CREATURE_EVENT_ON_DODGED = 10,
CREATURE_EVENT_ON_BLOCKED = 11,
CREATURE_EVENT_ON_CRIT_HIT = 12,
CREATURE_EVENT_ON_HIT = 13,
CREATURE_EVENT_ON_ASSIST_TARGET_DIED = 14,
CREATURE_EVENT_ON_FEAR = 15,
CREATURE_EVENT_ON_FLEE = 16,
CREATURE_EVENT_ON_CALL_FOR_HELP = 17,
CREATURE_EVENT_ON_LOAD = 18,
CREATURE_EVENT_ON_REACH_WP = 19,
CREATURE_EVENT_ON_LOOT_TAKEN = 20,
CREATURE_EVENT_ON_AIUPDATE = 21,
CREATURE_EVENT_ON_EMOTE = 22,
CREATURE_EVENT_COUNT,
[GAMEOBJECT EVENTS]
GAMEOBJECT_EVENT_ON_CREATE = 1,
GAMEOBJECT_EVENT_ON_SPAWN = 2,
GAMEOBJECT_EVENT_ON_LOOT_TAKEN = 3,
GAMEOBJECT_EVENT_ON_USE = 4,
GAMEOBJECT_EVENT_AIUPDATE = 5,
GAMEOBJECT_EVENT_ON_DESPAWN = 6,
GAMEOBJECT_EVENT_COUNT,
[GOSSIP EVENTS]
GOSSIP_EVENT_ON_TALK = 1,
GOSSIP_EVENT_ON_SELECT_OPTION = 2,
GOSSIP_EVENT_ON_END = 3,
GOSSIP_EVENT_COUNT,
[RANDOM FLAGS]
RANDOM_ANY = 0,
RANDOM_IN_SHORTRANGE = 1,
RANDOM_IN_MIDRANGE = 2,
RANDOM_IN_LONGRANGE = 3,
RANDOM_WITH_MANA = 4,
RANDOM_WITH_RAGE = 5,
RANDOM_WITH_ENERGY = 6,
RANDOM_NOT_MAINTANK = 7,
RANDOM_COUNT,
[NOTE]: 'RandomFlags' are generally used for creature targeting. hers an example.


Unit:GetRandomPlayer(7)That will get a random player that is not the main tank. (Replacing the the 7 with a different flag when changing it of course).


When creating the meat of a function, every Lua command must have a prefix before the command that tells what will be executing the command(Gameobject/Creature/Item)

For example, when you want a Creature to cast a spell on a random target. it would need a prefix telling who is casting the spell;


function Creature_OnCombat(Unit, Event)
Unit:CastSpellOnTarget(11, Unit:GetRandomPlayer(7))
end

RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")
Above, tells the Unit, To cast spell 11 (Frost bolt of the Ages) on a random player that isn't the main tank. Since it was registed "OnCombat", it will only cast this spell once as the unit enters combat, To make it cast periodically, you Register an event from inside a function.



function Creature_OnCombat(Unit, Event)
Unit:RegisterEvent("FunctionName", Interval, IntervalCount)
end

function Frostbolt(Unit, Event)
Unit:CastSpellOnTarget(11, Unit:GetRandomPlayer(7)
end
FunctionName is the name of the function your calling/registering (the name must match the name of the function you are calling), the interval is the amount of time it will take for the event to get registered, and the IntervalCount is the amount of times the function will be registered (Leave it at 0 for infinite)
Now if you make the interval 1000, and the invervalcount 0. It will cast Frostbolt at a random target that isn't the main tank every second, But say your group wipes. He will still register the event when not in combat, and when he enters combat again he will cast frostbolt twice a second. Which is why you need to use the command
Unit:RemoveEvents() which clears all events from the unit. You do this by creating a new function and registering it with the flag "2" for 'CREATURE_EVENT_ON_LEAVE_COMBAT' and "4" for "CREATURE_EVENT_ON_DIED", Then inserting Unit:RemoveEvents(), clearing all events when the NPC Leaves combat / your group wipes.



function Creature_OnCombat(Unit, Event)
Unit:RegisterEvent("FrostboltSpam", 1000, 0)
end

function FrostboltSpam(Unit, Event)
Unit:FullCastSpellOnTarget(11, Unit:GetClosestPlayer())
end

function Creature_OnLeaveCombat(Unit, Event)
Unit:RemoveEvents()
end

function Creature_OnDied(Unit, Event)
Unit:RemoveEvents()
end

RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")
RegisterUnitEvent(NPCID, 2, "Creature_OnLeaveCombat")
RegisterUnitEvent(NPCID, 4, "Creature_OnDied")

You can only Register events with Creatures/NPC's. From inside Combat Events / Gossip Events, gameobjects and items cannot register events..

[1.2]Variables


Variables are a value stored in memory that can be called upon or modified at a later point in your script. Just like in grade 1 where you have to find out what goes in the box with a equation like so 1 + 2 = [ ]. The Box being the variable, would end up being 3. However we don't use boxes, It is easiest to name your variable after what the variable will be used for, especially when making large scripts. Now there are 2 types of Variables. Global variables, and local variables. Global variables are variables used throughout your entire script. Local variables are variables used within a function. When creating local variables, they need to be declared (Making the Lua engine realize they are local variables). they are declared by using the term 'local' behind the variable your declaring.

function Creature_OnCombat(Unit, Event)
local intVariable = 0
endGlobal variables do not need declaration and can be created the same way as local variables, except without the usage of the term 'local'

function Creature_OnCombat(Unit, Event)
intVariable = 0
end[NOTE]You can have the same local and global variable names as long as you do not use both of them inside the same function.
[NOTE]I'm Not sure about this haven't ever tried it, But i believe Global variables, can be used throughout all your scripts in the scripts folder. So if you want a variable to be relative to just one of your scripts, declare the variable as a local variable at the top of the script:



local intEnteredCombat = 0

function Creature_OnCombat(Unit, Event)
intEnteredCombat = intEnteredCombat + 1
end

RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")

[1.3]The If Statement


When scripting with Lua, One of the most handy things to understand is an If statement. How it works is, IF <this happens / is true> then <this will happen>. Example below:



local intVariable = 0
function Creature_OnCombat(Unit, Event)
intVariable = 0
if (intVariable == 0) then -- If intVariable is equal to something other than 0, nothing will happen when the creature enters combat.
Unit:CastSpellOnTarget(11, Unit:GetRandomPlayer(7)) -- Otherwise it will cast spell 11 (frostbolt of the ages, on a random target that isn't the tank)
end

end
An if statement must always start with If, and end with end. when checking the variables value in a statement, you must use two equals signs, as opposed to one. This indicates you are checking the variables value, rather than modifying/setting it. Within an If statement you can have a few basic substatements (which are not limited to); Elseif, and Else. These statements do what they say.

function Creature_OnCombat(Unit, Event)

if (intVariable == 0) then
Unit:CastSpellOnTarget(11, Unit:GetRandomPlayer(0))
intVariable = 1
elseif (intVariable == 1) then
Unit:CastSpellOnTarget(5, Unit:GetRandomplayer(0))
else
Unit:SendChatMessage(14, 0, "intVariable has no value!)
intVariable = 0
end
end

Whats happening in the function above, is when the NPC enters combat for the first time, since intVariable was not given a value at any other point in the script, it is 'null' (nothing, no value). Which is not 0 or 1, thus executing what is in the 'else' statement, setting intVariable to 0, and sending a chat message. The 2nd time the creautre enters combat, they will cast frostbolt, and the third time and all times after that, they will cast death touch.
A good trick you can use with If statements, when timing scripts. is the use of a variable that equals itself + 1, take a look;


local intVariable = 0
function Creature_OnCombat(Unit, Event)
intVariable = 0 -- Setting a value so it isn't null.
Unit:RegisterEvent("Timer", 1000, 1)
end

function Timer(Unit, Event)
intVariable = intVariable + 1

if (intVariable == 1) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 1")
elseif (intVariable == 2) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 2")
elseif (intVariable == 3) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 3")
elseif (intVariable == 4) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 4")
elseif (intVariable == 5) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 5")
else
Unit:SendChatMessage(14, 0, "intVariable is another value aside from 1, 2, 3, 4, 5")
end
end

Now with the script above, since it's only getting registered once, (IntervalCount is 1) the function Timer, will only be executed once. we need to either set the intervalcount to 0, or create a Stop Variabe while looping the function.


local intVariable = 0
function Creature_OnCombat(Unit, Event)
intVariable = 0
intStop = 0 -- New Stop Variable
Unit:RegisterEvent("Timer", 1000, 1)
end

function Timer(Unit, Event)
intVariable = intVariable + 1

if (intVariable == 1) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 1")
elseif (intVariable == 2) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 2")
elseif (intVariable == 3) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 3")
elseif (intVariable == 4) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 4")
elseif (intVariable == 5) then
Unit:SendChatMessage(14, 0, "intVariable is equal to 5")
else
Unit:SendChatMessage(14, 0, "intVariable is another value aside from 1, 2, 3, 4, 5")
end

if (intStop == 1) then
-- nothing
elseif (intStop == 0) then
Unit:RegisterEvent("Timer", 1000, 1) -- Loops the Timer function unless intStop == 1, in which case it stops.
end
end

creating a stop variable can be useful for when you want to have a certain NPC's death trigger an event to temporarily stop, or prevent an NPC from spawning if one already spawned. And it is a lot safer as when you have intervalcount set to 0, if you spawn more than 1 NPC, you can have an event registered more often than you want.


[1.4]Command List & Usage


[NOTE]:Some of these Commands may not work with some Lua Engines



math.random(min, max) -- returns a random number between min and max. Best used as a variable
Ex.______________________
Choice=math.random(1,5)
if (choice == 5) then
player:SendAreaTriggerMessage("You just won the lottery")
end
_________________________

Gossip Commands
Item:GossipCreateMenu(textid, player, 0)
Item:GossipMenuAddItem(iconid, "name", intid, type);
Item:GossipSendMenu(player);
Item:GossipComplete(player);
Item:GossipSendPOI(player, Xcoord, Ycoord, icon, flags, data, nameofPOI);
Unit:GossipCreateMenu(textid, player, 0);
Unit:GossipMenuAddItem(iconid, "name" intid, type);
Unit:GossipSendMenu(player);
Unit:GossipComplete(player);
Unit:GossipSendPOI(player, Xcoord, Ycoord, icon, flags, data, nameofPOI);
GameObject:GossipCreateMenu(textid, player, 0);
GameObject:GossipMenuAddItem(iconid, "name", intid, type);
GameObject:GossipSendMenu(player);
GameObject:GossipComplete(player);
GameObject:GossipSendPOI(player, Xcoord, Ycoord, icon, flags, data, nameofPOI);

[NOTE] for GossipCreateMenu: textid, is the same id used in npc_textid, however you cannot have the same textid for 2 different menus.
[NOTE] iconid = Icon next to the text in the menu list ingame. intid is a variable used in GossipSubMenu, type - 0 is regular, 1 = code(A prompt box pops up and you have to enter a new value for the variable 'code', doesn't work that good.)

GET COMMANDS

Unit:GetPlayerRace(); -- returns number based on race. [1=Human][2=Orc][3=Dwarf][4=NightElf][5=Undead][6=Tauren][7=Gnome][8=Troll][10=BloodElf][11=Draenei]
Unit:GetCurrentSpellId(); -- Returns spell ID the target is currently casting
Unit:GetStanding(lua_State * L, Unit * ptr);
Unit:GetMainTank(lua_State * L, Unit * ptr);
Unit:GetAddTank(lua_State * L, Unit * ptr);
Unit:GetX(); -- Returns X Coordinate
Unit:GetY(); -- Returns Y Coordinate
Unit:GetZ(); -- Returns Z Coordinate
Unit:GetO(); -- Returns Orientation
Unit:GetTauntedBy(); -- returns player who taunted
Unit:GetSoulLinkedWith(lua_State * L, Unit * ptr);
Unit:GetItemCount(itemid); -- returns amount
Unit:GetName(); -- returns Unit Name
Unit:GetHealthPct(); -- Returns Units health betweeen 1 and 100
Unit:GetManaPct(); -- Returns Units Mana between 1 and 100
Unit:GetInstanceID(); -- returns instance id
Unit:GetClosestPlayer(RandomFlag); -- Gets closest player; use Random Flags.
Unit:GetRandomPlayer(RandomFlag); -- Gets random player; use Random Flags.
Unit:GetRandomFriend(); -- Gets Random friend / player; use Random flags?.
Unit:GetUnitBySqlId(); -- Gets another NPC by their SQLID
Unit:GetPlayerClass(); -- Returns number based on class. [Warrior=1][Paladin=2][Hunter=3][Rogue=4][Priest=5][Deathknight=6][Shaman=7][Mage=8][Warlock=9][Druid=11]
Unit:GetHealth(); -- Returns Units Current health
Unit:GetMaxHealth(); -- Returns units Max health
Unit:GetCreatureNearestCoords(x, y, z, NPCID);
Unit:GetGameObjectNearestCoords(x, y, z, GOID);
Unit:GetDistance(); -- Returns Distance. .debug rangecheck (Uses distance2dsq, 2 distance only.)
Unit:GetGUID(); -- Returns GUID
Unit:GetZoneId(); -- Returns GUID
Unit:GetMaxMana(Value); -- Sets Max Mana
Unit:GetMana(); -- Returns Current Mana
Unit:GetCurrentSpell(); -- returns current spell id?
Unit:GetSpawnO(); -- Returns Orientation of original Spawn
Unit:GetSpawnZ(); -- Returns Z Coordinate of original Spawn
Unit:GetSpawnY(); -- Returns Y Coordinate of original Spawn
Unit:GetSpawnX(); -- Returns X coordinate of Original Spanw
Unit:GetInRangePlayersCount(); -- Returns number based on amount of in range players.
Unit:GetUInt32Value(lua_State * L, Unit * ptr);
Unit:GetUInt64Value(lua_State * L, Unit * ptr);
Unit:GetFloatValue(lua_State * L, Unit * ptr);
Unit:GetAIState(lua_State * L, Unit * ptr);
Unit:GetCurrentSpell(); -- same as others?
Unit:GetInRangeGameObjects(); -- Returns Gameobjects in range in a Table (Cannot be used as a target)--\_____ Will get error "Unit Expected, got Nil"
Unit:GetInRangePlayers(); -- Returns In range Players in a Table (Cannot be used as a target)----------/
Unit:GetAITargets(lua_State * L, Unit * ptr);
Unit:GetUnitByGUID(); -- Returns Unit by their Guid
Unit:GetInRangeObjectsCount(); -- Returns a number based on amount of in range gameobjects
Unit:GetAITargetsCount(lua_State * L, Unit * ptr);
Unit:GetUnitToFollow(lua_State * L, Unit * ptr);
Unit:GetNextTarget(); -- Gets next highest threat target.
Unit:GetPetOwner(lua_State * L, Unit * ptr);
Unit:GetEntry(); -- Returns npc entryid?
Unit:GetFaction(); -- Returns NPC's faction
Unit:GetThreatByPtr(lua_State * L, Unit * ptr);
Unit:GetInRangeFriends(lua_State * L, Unit * ptr);
Unit:GetPowerType(lua_State * L, Unit * ptr);
Unit:GetMapId(); -- Returns Mapid
Unit:GetFactionStanding(lua_State * L, Unit * ptr);
Unit:GetPlayerLevel(); -- Returns playerlevel

IS COMMANDS -- Will returns '1' or 'true' if true. Not sure what one.

Unit:IsPlayerAttacking();
Unit:IsPlayerMoving();
Unit:IsPlayerAtWar(factionID);
Unit:IsPlayer();
Unit:IsCreature();
Unit:IsInCombat();
Unit:IsAlive();
Unit:IsDead(l);
Unit:IsInWorld();
Unit:IsCreatureMoving();
Unit:IsFlying();
Unit:IsInFront();
Unit:IsInBack();
Unit:IsPacified();
Unit:IsFeared();
Unit:IsStunned();
Unit:HasInRangeObjects();
Unit:IsInWater();
Unit:IsInArc();
Unit:IsPet();
Unit:MoveFly();
Unit:NoRespawn();
Unit:HasItem();
Unit:FlyCheat();

OTHER COMMANDS

Unit:AdvanceSkill(skillid, amount);
Unit:AddSkill(skillid);
Unit:RemoveSkill(skillid);
Unit:PlaySpellVisual(lua_State * L, Unit * ptr);
Unit:RemoveThreatByPtr(lua_State * L, Unit * ptr);
Unit:EventCastSpell(lua_State * L, Unit * ptr);
Unit:AttackReaction(lua_State * L, Unit * ptr);
Unit:DismissPet(lua_State * L, Unit * ptr);
Unit:HandleEvent(lua_State * L, Unit * ptr);
Unit:SetMoveRunFlag(lua_State * L, Unit * ptr);
Unit:SendChatMessage(Language, Type, "Message"); -- Langage; what language message is in, type = what form message is in (say/yell/whisper), "message" = duh
Unit:MoveTo(x, y, z, o); -- Once NPC gets to position i reccomend using Unit:SetFacing(Orientation) if O coordinate doesn't work.
Unit:SetMovementType(lua_State * L, Unit * ptr);
Unit:CastSpell(SpellID); -- Casts spell on Itself
Unit:CastSpellOnTarget(spellID, target); -- Casts spell ID on a target with no casttime, mights till be bugged and cast on itself
Unit:FullCastSpell(spellid); -- Fully casts a spell on itself / aoe spell
Unit:FullCastSpellOnTarget(spellid, target); -- Full casts a spell on a target.
Unit:SpawnGameObject(GOID, x, y, z, o, duration); -- self explanitory (set duration to 0 to keep gameobject spawned until server restart/shutdown
Unit:SpawnCreature(NPCID, x, y, z, o, faction, duration); -- self explanitory (set duration to 0 to keep creatued spawned forever until server restart/shutdown)
Unit:RegisterEvent("Event", Interval, IntervalCount); -- "Event"; event name, Interval; amount of time between registering the event again, IntervalCount; Amount of times to register the event.
Unit:RemoveEvents(); -- Removes all events from the Unit
Unit:SendBroadcastMessage("Text"); -- player only command, sends to chat box (player:SendBroadcastMessage("BroadcastMessageAppearsInTextBox"))
Unit:SendAreaTriggerMessage(lua_State * L, Unit * ptr); -- player only command, sends across screen (player:SendAreaTriggerMessage("AreaTriggerMessageAppearsAcrossScreen"))
Unit:KnockBack(dx, dy, affect1, affect2); -- Not sure, distancex, distancey, affects might be spell ID's?
Unit:MarkQuestObjectiveAsComplete(lua_State * L, Unit * ptr); -- no clue
Unit:LearnSpell(SpelLID); -- learns spellid, may be player only command
Unit:UnlearnSpell(SpellID); -- Unlearns spellid, may be player only command
Unit:HasFinishedQuest(QuestID); -- Returns true / 1 if true
Unit:ClearThreatList(); -- Drops all agro, may even leave combat
Unit:ChangeTarget(lua_State * L, Unit * ptr);
Unit:Emote(emoteid, time);
Unit:Despawn(despawntime, respawntime); -- despawntime = despawns in x miliseconds, respawntime = respawns in x miliseconds. (To Permanantly remove a creature spawned by another creature with no SQLID, use :RemoveFromWorld()
Unit:PlaySoundToSet(SoundID);
Unit:RemoveAura(SpellID);
Unit:StopMovement(Time); -- time = Time in miliseconds
Unit:AddItem(itemid, itemcount); -- playeronly command
Unit:RemoveItem(itemid, itemcount); -- playeronly command
Unit:CreateCustomWaypointMap(lua_State * L, Unit * ptr);
Unit:CreateWaypoint(lua_State * L, Unit * ptr);
Unit:DestroyCustomWaypointMap(lua_State * L, Unit * ptr);
Unit:MoveToWaypoint(lua_State * L, Unit * ptr);
Unit:TeleportUnit(map, x, y, z); -- Teleports player that clicked to location, not sure~ might teleport the Unit.
Unit:ClearHateList(); -- Resets threat list, gets random target
Unit:WipeHateList(); -- clears hate list, might leave combat
Unit:WipeTargetList(lua_State * L, Unit * ptr);
Unit:WipeCurrentTarget(lua_State * L, Unit * ptr);
Unit:CastSpellAoF x, y, z, spellid); -- self explanitory
Unit:RemoveAllAuras(); -- removes all auras, hostile and friendly
Unit:StopChannel(); -- stops channeling
Unit:ChannelSpell(spellid, target); -- channels spell on target?
Unit:ReturnToSpawnPoint(); -- returns to spawn point
Unit:HasAura(spellid); -- returns true or 1 if true.
Unit:Land(); -- Unit removes 1024 flag of flying.
Unit:CancelSpell(spellid); -- stops casting spell?
Unit:Root(target); -- self
Unit:Unroot(target); -- explanitory?
Unit:CalcDistance(target); -- returns value based in yards?
Unit:ModUInt32Value(lua_State * L, Unit * ptr);
Unit:ModFloatValue(lua_State * L, Unit * ptr);
Unit:SendData(lua_State * L, Unit * ptr);
Unit:InitPacket(lua_State * L, Unit * ptr);
Unit:AddDataToPacket(lua_State * L, Unit * ptr);
Unit:AddGuidDataToPacket(lua_State * L, Unit * ptr);
Unit:AdvanceQuestObjective(lua_State * L, Unit * ptr);
Unit:Heal(lua_State * L, Unit * ptr);
Unit:Energize(lua_State * L, Unit * ptr);
Unit:SendChatMessageAlternateEntry(lua_State * L, Unit * ptr);
Unit:SendChatMessageToPlayer(lua_State * L, Unit * ptr);
Unit:Strike(lua_State * L, Unit * ptr);
Unit:Kill(target);
Unit:DealDamage(lua_State * L, Unit * ptr);
Unit:CreateGuardian(lua_State * L, Unit * ptr);
Unit:CalcToDistance(lua_State * L, Unit * ptr);
Unit:CalcAngle(lua_State * L, Unit * ptr);
Unit:CalcRadAngle(lua_State * L, Unit * ptr);
Unit:IsInvisible(lua_State * L, Unit * ptr);
Unit:IsInvincible(lua_State * L, Unit * ptr);
Unit:ResurrectPlayer(player); -- player only command
Unit:KickPlayer(lua_State * L, Unit * ptr);
Unit:CanCallForHelp(lua_State * L, Unit * ptr);
Unit:CallForHelpHp(lua_State * L, Unit * ptr);
Unit:RemoveFromWorld(); -- Removes Unit From World (useful for npcs not saved to the Db {NPC spawned by other NPCS with no GUID})
Unit:SpellNonMeleeDamageLog(lua_State * L, Unit * ptr);
Unit:ModThreat(lua_State * L, Unit * ptr);
Unit:AddAssistTargets(lua_State * L, Unit * ptr);
Unit:RemoveAurasByMechanic(lua_State * L, Unit * ptr);
Unit:RemoveAurasType(lua_State * L, Unit * ptr);
Unit:AddAuraVisual(lua_State * L, Unit * ptr);

SET COMMANDS

Unit:SetPlayerStanding(lua_State * L, Unit * ptr);
Unit:SetPlayerLevel(level); -- might not work
Unit:SetPlayerAtWar(lua_State * L, Unit * ptr);
Unit:SetCreatureName(lua_State * L, Unit * ptr);
Unit:SetDeathState(lua_State * L, Unit * ptr);
Unit:SetPowerType(lua_State * L, Unit * ptr);
Unit:SetAttackTimer(time, duration);
Unit:SetMana(Value); -- sets currnet mana
Unit:SetMaxMana(Value);
Unit:SetHealth(Value); -- sets current health
Unit:SetMaxHealth(Value);
Unit:SetFlying(); -- Sets NPC to fly.
Unit:SetCombatCapable(1); --------\
Unit:SetCombatMeleeCapable(1); ----\
Unit:SetCombatRangedCapable(1); ----\_________ Set to 1 for Disableing Capableness, set to 0 to enable it again.
Unit:SetCombatSpellCapable(1); -----/
Unit:SetCombatTargetingCapable(1);-/
Unit:SetNPCFlags(NPCFLAGS); -- sets npc flags
Unit:SetModel(displayid); -- displayid
Unit:SetScale(Scale); -- size
Unit:SetFaction(faction); -- faction id
Unit:SetStandState(lua_State * L, Unit * ptr);
Unit:SetTauntedBy(lua_State * L, Unit * ptr);
Unit:SetSoulLinkedWith(lua_State * L, Unit * ptr);
Unit:SetInFront(lua_State * L, Unit * ptr);
Unit:SetHealthPct(%); -- sets health Percent
Unit:SetOutOfCombatRange(lua_State * L, Unit * ptr);
Unit:ModifyRunSpeed(lua_State * L, Unit * ptr);
Unit:ModifyWalkSpeed(lua_State * L, Unit * ptr);
Unit:ModifyFlySpeed(lua_State * L, Unit * ptr);
Unit:SetRotation(Orientation);
Unit:SetOrientation(Orientation);
Unit:SetUInt32Value(lua_State * L, Unit * ptr);
Unit:SetUInt64Value(lua_State * L, Unit * ptr);
Unit:SetFloatValue(lua_State * L, Unit * ptr);
Unit:SetUnitToFollow(lua_State * L, Unit * ptr);
Unit:SetNextTarget(lua_State * L, Unit * ptr);
Unit:SetPetOwner(lua_State * L, Unit * ptr);
Unit:SetFacing(lua_State * L, Unit * ptr);

GAMEOBJECT LIST

GET COMMANDS
GameObject:GetName(lua_State * L, GameObject * ptr);
GameObject:GetMapId(lua_State * L, GameObject * ptr);
GameObject:GetCreatureNearestCoords(lua_State * L, GameObject * ptr);
GameObject:GetGameObjectNearestCoords(lua_State *L, GameObject * ptr);
GameObject:GetAreaID(lua_State * L, GameObject * ptr);
GameObject:GetClosestPlayer(lua_State * L, GameObject * ptr);
GameObject:GetZoneId(lua_State *L, GameObject * ptr);
GameObject:GetItemCount(lua_State * L, GameObject * ptr);
GameObject:GetSpawnX(lua_State * L, GameObject * ptr);
GameObject:GetSpawnY(lua_State * L, GameObject * ptr);
GameObject:GetSpawnZ(lua_State * L, GameObject * ptr);
GameObject:GetSpawnO(lua_State * L, GameObject * ptr);
GameObject:GetInRangePlayersCount(lua_State * L, GameObject * ptr);
GameObject:GetEntry(lua_State * L, GameObject * ptr);
GameObject:GetX(lua_State * L, GameObject * ptr);
GameObject:GetY(lua_State * L, GameObject * ptr);
GameObject:GetZ(lua_State * L, GameObject * ptr);
GameObject:GetO(lua_State * L, GameObject * ptr);
GameObject:GetInRangePlayers(lua_State * L, GameObject * ptr);
GameObject:GetInRangeGameObjects(lua_State * L, GameObject * ptr);
GameObject:GetInstanceID(lua_State * L, GameObject * ptr);
GameObject:GetUInt64Value(lua_State * L, GameObject * ptr);
GameObject:GetUInt32Value(lua_State * L, GameObject * ptr);
GameObject:GetFloatValue(lua_State * L, GameObject * ptr);
GameObject:GetGUID(lua_State * L, GameObject* ptr);

OTHER COMMANDS
GameObject:Teleport(lua_State * L, GameObject * ptr); -- player command, player:Teleport(map, x, y, z)
GameObject:AddItem(lua_State * L, GameObject * ptr); -- player command, player:AddItem(itemid, itemcount)
GameObject:Despawn(lua_State * L, GameObject * ptr);
GameObject:IsInWorld(lua_State * L, GameObject * ptr);
GameObject:IsInBack(lua_State * L, GameObject * ptr);
GameObject:IsInFront(lua_State * L, GameObject * ptr);
GameObject:PlaySoundToSet(lua_State * L, GameObject * ptr);
GameObject:SpawnCreature(lua_State * L, GameObject * ptr);
GameObject:SpawnGameObject(lua_State * L, GameObject * ptr);
GameObject:CalcDistance(lua_State * L, GameObject * ptr);
GameObject:SetOrientation(lua_State * L, GameObject * ptr);
GameObject:RemoveFromWorld(lua_State * L, GameObject * ptr);
GameObject:CalcRadAngle(lua_State * L, GameObject * ptr);
GameObject:SetUInt32Value(lua_State * L, GameObject * ptr);
GameObject:SetUInt64Value(lua_State * L, GameObject * ptr);
GameObject:SetFloatValue(lua_State * L, GameObject * ptr);
GameObject:ModUInt32Value(lua_State * L, GameObject * ptr);
GameObject:CastSpell(lua_State * L, GameObject * ptr);
GameObject:FullCastSpell(lua_State * L, GameObject * ptr);
GameObject:CastSpellOnTarget(lua_State * L, GameObject * ptr);
GameObject:FullCastSpellOnTarget(lua_State * L, GameObject * ptr);
GameObjectvEventCastSpell(lua_State * L, GameObject * ptr);
GameObject:GossipObjectCreateMenu(lua_State * L, GameObject * ptr);
GameObject:GossipObjectMenuAddItem(lua_State * L, GameObject * ptr);
GameObject:GossipObjectSendMenu(lua_State * L, GameObject * ptr);
GameObject:GossipObjectComplete(lua_State * L, GameObject * ptr);
GameObject:GossipObjectSendPOI(lua_State * L, GameObject * ptr);
GameObject:ModUInt32Value(lua_State * L, GameObject * ptr);
GameObject:ModFloatValue(lua_State * L, GameObject * ptr);
GameObject:GetFloatValue(lua_State * L, GameObject * ptr);
GameObject:InitPacket(lua_State * L, GameObject * ptr);
GameObject:AddDataToPacket(lua_State * L, GameObject * ptr);
GameObject:AddGuidDataToPacket(lua_State * L, GameObject * ptr);
GameObject:SendData(lua_State * L, GameObject * ptr);
[1.5]Commenting


Thought i'd add this in here real quick. Commenting code is done by using two '-' followed by a space and text of your choice for reminders or tags for certain lines of code. heres an example;

function Creature_OnLoad(Unit, Event)
Unit:RegisterEvent("Timer", 1000, 1)
end

function Timer(Unit, Event)
Unit:FullCastSpellOnTarget(35853, Unit:GetRandomPlayer(7)) -- [SPELLID:35853 = Chain Fireball]
end

another example is encasing a large paragraph of text using --[[ & -- ]. Heres an example



-- [[
################################################## #############
# #
# #
# #
# Dumb Header For scripting Teams? #
# #
# #
# #
################################################## ############# -- ]]

function Creature_OnLoad(Unit, Event)
Unit:RegisterEvent("SendChatMessage", 1000, 0)
end

function SendChatMessage(Unit, Event)
Unit:SendChatMessage(12, 0, "Im an annoying spammer NPC...")
Unit:SetFaction(14)
Unit:CastSpell(5) -- Deathtouches himself
end
[2.0] Creature Events


Most Common Creature Commands & Usage[2.1]


The most common commands when scripting a boss fight of creature are;



Unit:RegisterEvent("EventName", Interval, IntervalCount)
Unit:CastSpell(Spellid)
Unit:CastSpellOnTarget(SpellId, target)
Unit:FullCastSpellOnTarget(SpellId, target)
Unit:FullCastSpell(SpellID)
Unit:GetHealthPct()
Unit:SetModel(DisplayID)
Unit:GetRandomPlayer(FLAG)
[RANDOM FLAGS]
RANDOM_ANY = 0,
RANDOM_IN_SHORTRANGE = 1,
RANDOM_IN_MIDRANGE = 2,
RANDOM_IN_LONGRANGE = 3,
RANDOM_WITH_MANA = 4,
RANDOM_WITH_RAGE = 5,
RANDOM_WITH_ENERGY = 6,
RANDOM_NOT_MAINTANK = 7,
Unit:GetClosestPlayer()
Unit:SpawnCreature(creatureID, x, y, z, o, faction, duration) -- duration = 0 for infinite.
Unit:SendChatMessage(language, channeltype, "Message")
Unit:RemoveEvents()


function Creature_OnCombat(Unit, Event)
Unit:RegisterEvent("WatchHealth", 1000, 1)
end

function WatchHealth(Unit, Event)
if Unit:GetHealthPct() <= 80 then
Unit:RemoveEvents() -- Removes constant Registering event from OnCombat
Unit:RegisterEvent("Phase2", 1000, 1) -- Registers Phase2.
Unit:FullCastSpellOnTarget(11, Unit:GetRandomPlayer(7))
Unit:SendChatMessage(41, 0, "Creature begins to transform!, and his minion appears!")
Unit:SetModel(28090)
Unit:SpawnCreature(6, 1234, 432, 42, 0, 14, 10000) -- Spawns creature 6(Kobold vermin), at x1234, y 432, z 42, and its orientation facing north. its faction is 14, and it will despawn 10 seconds after it is spawned.
else
Unit:RegisterEvent("WatchHealth", 5000, 1)
end
end

Languages and Channeltypes for SendChatMessage



[Language ID
0 = Universal
1 = Orcish
2 = Darnassian
3 = Tauron
6 = Dwarfs
7 = Common
8 = Demonic
9 = Titans
13 = Gnomish
14 = Troll language
33 = Gutterspeak
35 = Draenei

[ChannelTypes
12 = Say
14 = Yell
13 = Whisper
16 = Emote
41 = Area Broadcast with sound \__________ ex: "The lava around sarthiron begins to churn" then the bell noise.
42 = Area Broadcast with sound /

[2.3]Combat and General Events


Bosses are where you can really let your imagination run wild. A Boss must be fun. But no matter how simple it is, you must make sure it is a unique and entertaining fight, Otherwiset, The fight will get boring, no one will attempt the boss as much, everyone will have the same loot. So i'm here to hopefully show you how to make a good fun boss fight. However, your scripts are only as good as your imagination.

But before i start, any new Commands i use in the examples below will be explained and they will also have an example for them. Also, i will be using Kobold Vermins entry ID for the boss, rawr.

To start, I will show you how to create phases, There is a few different ways;


1). This takes a bit more work, but is generally easier to handle.


function Creature_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "rawr its my first phase")
Unit:RegisterEvent("Creature_FirstPhase", 1000, 1)
end

function Creature_FirstPhase(Unit, Event)
if Unit:GetHealthPct() <= 80 then
Unit:RegisterEvent("Creature_SecondPhase", 1000, 1) -- The boss is below 80% hp, registering 2nd phase.
Unit:SendChatMessage(14, 0, "Rawr entering second phase") -- This is where you will input any spells or chat messages you only want to be said ONCE when your npc enters their next phase.
else
Unit:RegisterEvent("Creature_FirstPhase", 1000, 1) -- If the boss isn't below 80% hp, it will register the event and check again every 1 second.
end
end

function Creature_SecondPhase(Unit, Event)
-- Second phase code.
end

RegisterUnitEvent(6, 1, "Creature_OnCombat")

2). This requires the least work, but sometimes can cause problems



function Creature_OnCombat(Unit, Event)
Unit:SendChatMessage(14, 0, "rawr its my first phase")
Unit:RegisterEvent("Creature_FirstPhase", 1000, 0)
end

function Creature_FirstPhase(Unit, Event)
if Unit:GetHealthPct() <= 80 then
Unit:RemoveEvents() -- Removing this event , so it stops registering since its interval count is set to infinite.
Unit:RegisterEvent("Creature_SecondPhase", 1000, 0)
end
end

function Creature_SecondPhase(Unit, Event)
-- Second phase code.
end
[NOTE]: Unit:RemoveEvents() must be before Unit:RegisterEvent("Creature_SecondPhase", 1000,0) otherwise, Creature_SecondPhase event, will be removed.

3). This way, is similar to the first one, except your phases aren't based on the bosses health, there based on if, for example - There add dies, and we have a stoptrigger get set to 1. I added in another variable to count how many seconds goesby so you can time spells or events you want to happen at certain points in phase one.



local intCreatureAddDeadTrigger = 0
local intPhaseOneCount = 0

function Creature_OnCombat(Unit, Event)
Unit:SendChatMessage(41, 0, "The creature awakens.")
Unit:RegisterEvent("Creature_PhaseOne", 1000, 1)
Unit:SpawnCreature(NPCID, x, y, z, o, 14, 0)
end

function Creature_PhaseOne(Unit, Event)
intPhaseOneCount = intPhaseOneCount + 1

if (intPhaseOneCount == 15) then
-- After 15 seconds he'll do this and so on...
elseif (intPhaseOneCount == 30) then
-- and so on...
elseif (intPhaseOneCount == 35) then
-- ... and so on...
elseif (intPhaseOneCount == 50) then
-- On your last event you want to happen, set intPhaseOneCount back to 0 so it will continue to loop the events above.
end


if (intCreatureAddDeadTrigger == 1) then
Unit:RegisterEvent("Creature_PhaseTwo", 1000, 1)
else
Unit:RegisterEvent("Creature_PhaseOne", 1000, 1)
end
end

function Creature_PhaseTwo(Unit, Event)
-- awmahgawd phase 2.
end

function CreatureAdd_OnDied(Unit, Event)
intCreatureAddDeadTrigger = 1
end

RegisterUnitEvent(NPCID, 4, "CreatureAdd_OnDied")
RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")

At first, i was surprised about how short this section was, but then i realized, After you know about what you can do with variables and if statements, and then how to make phases, you can do whatever you want.

[2.9]Creature Summary and Tips


There is a few things i know to make your life much much easier. The first thing is making an NPC as a whole, a variable, Meaning NPCs can cast spells on each other, Say things, all in the same function.. (Thanks Stoneharry)



function Creature_OnLoad(Unit, Event)
-- Say the bosses name is Fresca (Courtesy of the leaning tower of pop cans on my desk)
Fresca = Unit -- What this does, is makes the "Unit" from this function into the variable of "Fresca"
end

RegisterUnitEvent(NPCID, 18, "Creature_OnLoad")

The reason this is insanely useful is due to being able to have two different creatures being able to do something in the same function. And when your timing an even where two different npcs talk to eachother, ect ect. You can do that without making a parralell function line and trying to get the timing right. heres an example.




local TalkStartCount = 0

function CreatureOne_OnLoad(Unit, Event)
COne = Unit
end

function CreatureTwo_OnLoad(Unit, Event)
CTwo = Unit
end

function CreatureOne_OnCombat(Unit, Event)
COne:RegisterEvent("Talk_Start", 1000, 15)
COne:SetCombatMeleeCapable(1) -- All this just makes them friendly, and stand still / not attackable
CTwo:SetCombatMeleeCapable(1) -- Whlie there talking to each other before
COne:SetFaction(35) -- the actual event happens and
CTwo:SetFaction(35) -- you have to fight them
end

function Talk_Start(Unit, Event)
TalkStartCount = TalkStartCount + 1

if (TalkStartCount == 1) then
COne:SendChatMessage(14, 0, "Hey, how are you doing")
elseif (TalkStartCount == 5) then
CTwo:SendChatMessage(14, 0, "Good... and yourself")
elseif (TalkStartCount == 10) then
COne:SendChatMessage(14, 0, "I'm going to kill you")
elseif (TalkStartCount == 15) then
CTwo:SendChatMessage(14, 0, "gasp.")
CTwo:SetFaction(84)
COne:SetFaction(106)
CTwo:SetCombatMeleeCapable(0)
COne:SetCombatMeleeCapable(0)
end
end

RegisterUnitEvent(NPCID, 18, "CreatureTwo_OnLoad")
RegisterUnitEvent(NPCID, 18, "CreatureOne_OnLoad")
RegisterUnitEvent(NPCID, 1, "CreatureOne_OnCombat")

This instead of...



local TalkStartCountTwo = 0
local TalkStartCountOne = 0

function CreatureTwo_OnCombat(Unit, Event)
Unit:RegisterEvent("TalkTwo_Start", 1000, 15)
Unit:SetCombatMeleeCapable(1)
Unit:SetFaction(35)
end

function TalkTwo_Start(Unit, Event)
TalkStartCountTwo = TalkStartCountTwo + 1

if (TalkStartCountTwo == 1) then
COne:SendChatMessage(14, 0, "Hey, how are you doing")
elseif (TalkStartCountTwo == 10) then
COne:SendChatMessage(14, 0, "I'm going to kill you")
Unit:SetFaction(84)
Unit:SetCombatMeleeCapable(0)
end
end

function CreatureOne_OnCombat(Unit, Event)
Unit:RegisterEvent("TalkOne_Start", 1000, 15)
Unit:SetCombatMeleeCapable(1)
Unit:SetFaction(35)
end

function TalkOne_Start(Unit, Event)
TalkStartCountOne = TalkStartCountOne + 1

if (TalkStartCountOne == 5) then
CTwo:SendChatMessage(14, 0, "Good... and yourself")
elseif (TalkStartCountOne == 15) then
Unit:SendChatMessage(14, 0, "Gasp")
Unit:SetFaction(106)
Unit:SetCombatMeleeCapable(0)
end
end

RegisterUnitEvent(NPCID, 18, "CreatureTwo_OnLoad")
RegisterUnitEvent(NPCID, 18, "CreatureOne_OnLoad")
RegisterUnitEvent(NPCID, 1, "CreatureOne_OnCombat")
RegisterUnitEvent(NPCID, 1, "CraetureTwo_OnCombat")
Red highlights indicate what doesn't need to be there if we used the first method.

You can probably imagine how much more usefull this will become once your scripts get larger, and you have more than 2 NPC's.

Another tip is keeping you code clean, there is proper coding edicate i believe, but if its your own script. using Tabs instead of 5 spaces isn't going to hurt anything. Space your codes lines out appropriately so its easy to read through the code, or if you realase you scripts its easy for people to re-read them and modify them to their liking.
Sometimes you'll see code with no spaces, and some with plenty of spaces, heres an example.

No Spaces


local intCount = 0

function Creature_OnCombat(Unit, Event)
Unit:RegisterEvent("OmgWhatAMess", 1000, 1)
Unit:SendChatMessage(14, 0, "Hello everyone")
end

function OmgWhatAMess(Unit, Event)
intCount = intCount + 1
if (intCount == 1) then
Unit:SendChatMessage(12, 0, "intcount is equal to 1)
elseif (intCount == 2) then
UnitSendChatMessage(12, 0, "intCount is equal to 2
Unit:FullCastSpellOnTarget(11, Unit:GetClosestPlayer())
else
Unit:SendChatMessage(14, 0, "whats going on")
intCount = 0
end
if Unit:GetHealthPct() <= 80 then
Unit:RegisterEvent("phasetwo", 1000, 1)
else
Unit:RegisterEvent(OmgWhatAMess", 1000, 1)
end
end
RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")

With Spaces



local intCount = 0

function Creature_OnCombat(Unit, Event)
Unit:RegisterEvent("OmgWhatAMess", 1000, 1)
Unit:SendChatMessage(14, 0, "Hello everyone")
end


function OmgWhatAMess(Unit, Event)
intCount = intCount + 1

if (intCount == 1) then
Unit:SendChatMessage(12, 0, "intcount is equal to 1)
elseif (intCount == 2) then
UnitSendChatMessage(12, 0, "intCount is equal to 2
Unit:FullCastSpellOnTarget(11, Unit:GetClosestPlayer())
else
Unit:SendChatMessage(14, 0, "whats going on")
intCount = 0
end

if Unit:GetHealthPct() <= 80 then
Unit:RegisterEvent("phasetwo", 1000, 1)
else
Unit:RegisterEvent(OmgWhatAMess", 1000, 1)
end

end

RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")

With spaces, it becomes much clearer where If statements start and end, and in general whats actually happening in the script.


==============================================\/ Continued \/=============================================

SerialKiller
20-02-10, 06:46 AM
[3.0]Gameobject Events

[3.1] Most Common Gameobject Commands Usage & Events.
You can't do too much with Gameobjects, some people may argue with me but at least on my lua engine, Gameobjects are relatively limited to teleporting.

To make a simple teleport portal. All do you is use the player:teleport command in the registered event.

[NOTE]: Most of these examples are going to be copied and pasted from my server, sorry if some of the coords are messed up



function Gameobject_OnUse(Unit, Event, player)
player:Teleport(mapid, x, y, z)
end

RegisterGameObjectEvent(GOID, 4, "Gameobject_OnUse")
Now, thats not that interesting. However you can add in a few more things, If the player is in combat, you can make the portal un-usable to them;



function Gameobject_OnUse(Unit, Event, player)
if (player:IsInCombat() == true) then
player:SendAreaTriggerMessage("You are in combat!")
else
player:Teleport(mapid, x, y , z)
end
end

RegisterGameObjectEvent(GOID, 4, "Gameobject_OnUse")
Some other things you can do, Is for instance, to avoid people camping where people get ported in in gurubashi, you can write the script with math.random command (I'll add that command in the command list above :X) Ex.



function GurubashiPortal_OnUse(Unit, Event, player)
local Choice=math.random(1, 5) -- Chooses a random number between 1 and 5, That value is stored as the local variable 'choice'
if (player:IsInCombat() == true) then
player:SendBroadcastMessage("You are in combat!")
else
if (Choice == 1) then -- Depending on what number came out of the math.random command,Choice's value can be equal to any of the below values in the if statements. Teleporting a player randomly to 1 of 5 spots in gurubashi arena.
player:Teleport(0, -13217, 184, 100)
elseif (Choice == 2) then
player:Teleport(0, -13269, 212, 100)
elseif (Choice == 3) then
player:Teleport(0, -13294.5, 279, 100)
elseif (Choice == 4) then
player:Teleport(0, -13171, 337, 100)
elseif (Choice == 5) then
player:Teleport(0, -13148, 226, 100)
end
end
end

RegisterGameObjectEvent(98815, 4, "GurubashiPortal_OnUse")
[Note]: SQL Below if you're interested. Arcemu structure


INSERT INTO `gameobject_names` VALUES ('98815', '22', '7849', 'Armory Portal to Gurubashi', '', '', '', '', '', '', '9438', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0');
Something i've done is made certain portals only interactable with GM's / donars. And no i didn't find out a way to check if a player is a GM.

Assuming you don't have over nine thousand Game masters (maybe donars), this shouldn't be too much of a hassle.




function PortalToThinkBox_OnUse(Unit, Event, player)
local Name = player:GetName()

if (Name == nil) then
player:SendAreaTriggerMessage("You cannot enter through here!")
elseif (Name == "GM name one") then
player:Teleport(1, 16227.4, 16403.8, -64)
elseif (Name == "GM name two") then
player:Teleport(1, 16227.4, 16403.8, -64)
elseif (Name == "GM name three") then
player:Teleport(1, 16227.4, 16403.8, -64)
elseif (Name == "GM name four") then
player:Teleport(1, 16227.4, 16403.8, -64)
elseif (Name == "GM name five") then
player:Teleport(1, 16227.4, 16403.8, -64)
else
player:SendBroadcastMessage("You are not a Game Master.")
player:Teleport(mapid, x, y, z) -- Mall Location is what i use.
end
end

-- Or of course, you can set GM/Donar names as variables..

RegisterGameObjectEvent(98814, 4, "PortalToThinkBox_OnUse")
Remember in the example above, The names must be in string form... (Wrapped in quotations) otherwise it will think GMnamefour is a variable with a nil value!

[3.3] Gameobject Summary and Tips

Gameobjects can not register events. But they can still play a good role in a bossfight, and a big part in your server if you aren't a fan of pocket teleporters (People teleport in combat / bleh) Traditional ports are the way to go IMO =).

/facepalm at this pathetic section =)


[5.0] Generalized Gossip Events


Gossip commands and usage are generally all the same for Gameobjects, NPC's and items.

A gossip script has 2 events that need to be registered. (OnGossip, and OnSelectOption / OnSubMenu)



RegisterUnitGossipEvent(NPCID, 1, "OnGossip")
RegisterUnitGossipEvent(NPCID, 2, "OnSubMenu")
RegisterGOGossipEvent(GOID, 2, "OnSubMenu")
RegisterGoGossipEvent(GOID, 1, "OnGossip")
RegisterItemGossipEvent(ITEMID, 1, "Item_OnClick")
RegisterItemGossipEvent(ITEMID, 2, "OnSubMenu")

The basics of a Lua Gossip script are as follows:



function Npc_OnGossip(Unit, Event, player)

end

function Npc_OnSubMenu(Unit, Event, player, id, intid, code)

end

[Note]intid, code, will all be explained later
"player" is the player who clicked the npc.
Not sure whatid is for

The Gossip commands and usage all go inside, they are as follows;



function Npc_OnGossip(Unit, Event, player)
Unit:GossipCreateMenu(100, player, 0)
Unit:GossipMenuAddItem(0, "Name", 1, 50)
Unit:GossipSendMenu(player)
end
Those are the three basic commands to create the actual gossip interface. What they do is;



function Npc_OnGossip(Unit, Event, player)
Unit:GossipCreateMenu(textid, player, 0)
end

textid: Insert the entryid of the text you want to be displayed before your options: Ex. "Hey there, $N. How can I help you?". You can find the correct entryid in `npc_text` table.

player: Who you are creating this menu for.

0: No clue. i just know when its set to anything other than 0, the npc generally doesn't respond with any action.



function Npc_OnGossip(Unit, Event, player)
Unit:GossipMenuAddItem(symbolid, "Name of Option", intid, codeflag)
end

symbolid: Sets the ID of the symbol beside the Option Name.
Name of Option: Sets the name of the Option
intid: This is what intid will be set to if this option is selected.
codeflag: Flags triggering this option to open a input box. (1 and 3 are the flags that work.)
[Note]: please remember intid is a variable.


function Npc_OnGossip(Unit, Event, player)
Unit:GossipSendMenu(player)
end

player: who you are sending the menu too.

Ok, now that you understand what the commands are, lets get started creating an actual gossip script. I will make 3 examples. One of them will be a teleporter, Another one a guessing game.

Teleporter


function Creature_OnGossip(Unit, Event, player)
Unit:GossipCreateMenu(100, player, 0) -- Sends a menu to the player with the npc_text entryid of 100.
Unit:GossipMenuAddItem(0, "Outlands Locations", 1, 0) -- SETS intid to 1!!! And registers Creature_OnSubMenu(If its easier to remember, "OnSelectOption", same thing)
Unit:GossipSendMenu(player) -- Sends menu to the player.
end

function Creature_OnSubMenu(Unit, Event, player, id, intid, code)

if (intid == 1) then -- This is what will happen if you click outlands locations
Unit:GossipCreateMenu(100, player, 0)
Unit:GossipMenuAddItem(0, "Hellfire Penninsula", 2, 0)
Unit:GossipSendMenu(player)
end

if (intid == 2) then -- When you click on Hellfire Penninsula you get teleported since intID gets set to 2.
player:Teleport(530, x, y, z) -- usage = (mapid, x, y, z) full list of player commands with usage will be posted later.
end
end

RegisterUnitGossipEvent(NPCID, 1, "Creature_OnGossip")
RegisterUnitGossipEvent(NPCID, 2, "Creature_OnSubMenu")
NOTE: "Unit:GossipMenuAddItem(0, player, 0, 0)", Just becuase it is in an OnGossip function. (id, intid, code) still apply to their fields. For this reason, 0 sets intid to 0.


Whats happening in the script above, is when you click Outlands Locations, intid gets set to 1, At the point in time you've selected an option, which registers ("Creature_OnSubMenu"), If it doesn't make sense, think of Creature_OnSubMenu as Creature_OnSelectOption. So then that function gets registered. and your send a new menu with outland locations. Upon clicking hellfire penninsula, you clicked an option, which registers Creature_OnSubMenu yet again, and changing intid to 2, Executing the code withing the corresponding If statement.

Now below, i will make a Number guessing gossip script hopefully explaining what almost no Lua Tutorials contain... (Unit, Event, id, intid, code)




function Creature_OnGossip(Unit, Event, player)
Unit:GossipCreateMenu(100, player, 0)
Unit:GossipMenuAddItem(0, "Guess a number between 1 and 10!", 0, 3) -- 3 Flags this menu item to open an input box where a player can enter data (Like you do when redeeming a wow trading card game code for a rocket mount on retail ect ect)
Unit:GossipSendMenu(player)
end

function Creature_OnSubMenu(Unit, Event, player, id, intid, code)
if (intid == 0) then
if (code == "5") then -- code is a variable returned as whatever the player entered in the input box. Remember it is returned as a string (in word form) so whatever is it equal to must be in quotations. Having [if (code == 1) then] it will always return false and execute whats inside the 'else', this being due to anything the player types in the box will return as "<what player typed>".
player:AddItem(29434, 1) -- Adds an item to the player who clicked. player:AddItem(itemid, quantity)
else
player:SendAreaTriggerMessage("You Guessed wrong.") -- inserts a message into the players chat box in yellow.
end
end
end

RegisterUnitGossipEvent(NPCID, 1, "Creature_OnGossip")
RegisterUnitGossipEvent(NPCID, 2, "Creature_OnSubMenu")

"5" Is that value that IF what the player entered in the box is equal to, the statement will add an item to the player.
There wasn't much to explain, so i just threw in what there was to know in comments.

Its been a while since I've updated this, but i thought I'd add this small tip in for gossip scripts;

When creating a gossip scripts, It gets annoying always having to close and re-open the gossip after you've selected a certain option. For example, Say one of your gossip scripts is to give players food, The player does not want to have to re-open the gossip menu everytime he wants to get a new stack of food! So to keep the same menu active after an option has been selected, check out the example below.



function Unit_OnGossip(Unit, Event, player)
Unit:GossipCreateMenu(100, player, 0)
Unit:GossipMenuAddItem(0, "Item Selection", 1, 0)
Unit:GossipSendMenu(player)
end

function Unit_OnSelect(Unit, Event, player, id, intid, code)
if (intid == 1) then
Unit:GossipCreateMenu(101, player, 0)
Unit:GossipMenuAddItem(0, "Add Food!", 2, 0)
Unit:GossipMenuAddItem(0, "Add Water!", 3, 0)
Unit:GossipSendMenu(player)
end

if (intid == 2) then
player:AddItem(FoodItemID, amount)
intid = 1
Unit:GossipSendMenu(player)
end

if (intid == 3) then
player:AddItem(WaterItemID, amount)
intid = 1
Unit:GossipSendMenu(player)
end


end
What this does is adds an item to the players inventory, then sets intid to 1, and sends the menu associated to 1 to the player again. Keep in mind the menu being sent must be the one the player was currently on, otherwise it won't work(There is logic behind this but its late and i forget :S). To have properly working back buttons, i believe you still have to make a replica of the menu you want it to go back to.

Item Gossip

Theres not much difference between regular gossip events, and ones you'd use with an item.



function Item_OnClick(Item, Event, player)
Item:GossipCreateMenu(100, player, 0)
Item:GossipMenuAddItem(0, "hello lets telepert", 0, 0)
Item:GossipMenuAddItem(0, "hello want an item?", 2, 0)
Item:GossipSendMenu(player)
end

function OnSubMenu(Item, Event, player, id, intid, code)
if (intid == 0) then
player:Teleports(mapid, x, y, z)
end

if (intid == 1) then
player:AddItem(itemid, 1)
end
end

RegisterItemGossipEvent(ITEMID, 1, "Item_OnClick")
RegisterItemGossipEvent(ITEMID, 2, "OnSelect")

Above, is an example of an item gossip script. You script the exact same as you would with a Npcs gossip. There is almost no difference.


Below is a list of player commands usefull when creating gossip scripts.


player:AddItem(itemid, quantity) -- Adds item to the players inventory
player:Teleport(mapid, xcoord, ycoord, zcoord) -- Teleports the player to the selected coordinates.
player:SendAreaTriggerMessage("Message") -- Enters a message in the players chat box.
player:SendBroadcastMessage("Message") -- Enters a message across the players screen, where error messsages are shown
player:GossipSendPOI(player, Xcoord, Ycoord, icon, flags, data, nameofPOI); -- Flags a coordinate on your main map, and mini map, as a Point of Interest, with the assigned name, icon and coordinates. Leave data as 0. (I almost never use this command, so im not the best person to be explaining it)
Aside from those, i don't believe you use many More.

[$.#]3x7r4 1Nf0rNm471on

String Concatenation

Had to add this in here right now. Concatenation is just joining two strings together. Say if you wanted your boss to cast a spell on a certain person, and call out their name. You'd use string Concatenation.

Heres an example of having a boss call out a players name.



function Creature_OnCombat(Unit, Event)
Unit:RegisterEvent("Creature_CastSpell", 30000, 0)
end

function Creature_CastSpell(Unit, Event)
local name = Unit:GetRandomPlayer(7)
Unit:FullCastSpellOnTarget(
Unit:SendChatMessage(14, 0, "Enjoy sum frostbewltz"..name) -- "Enjoy sum frostbewltz" is the first part of the string, and 'name' is the variable your joining together.
end -- The two dots are what is joining them together.

RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")

Creature yells: Enjoy sum frostbewltz VisionOneThousand
two periods and two alone, are what you use to join two strings together when you want to do something like above. Or when your debugging a stupid script that won't work, and you can't figure out why a certain value even though set, will never be equal to another value.



local number = 5

function Creature_OnCombat(Unit, Event)
Unit:RegisterEvent("Creature_OnCastSpell", 1000, 1)
end

function Creature_OnCastSpell(Unit, Event)
local plr = Unit:GetRandomPlayer(0) -- Gets a random player
local name = plr:GetName()
Unit:CastSpellOnTarget(11, name) -- Using CastSpellOnTarget, it was bugged at one point and would ALWAYS cast the spell on itself.
Unit:SendChatMessage(14, 0, "name is equal to"..name.."Is that who the spell was cast on?") -- If the spell casts on himself, the command is bugged, if it casts on 'name' (Whoever was said ingame) then it works.
end

RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")
Debugging.

When making a Lua script for your first time. You are going to get a LOT of errors. Most errors are caught by the lua Engine on startup. But some get printed on the console while your script is running ingame (If your trying to add 1 to a variable with a nil value).
Heres a screenshot of what the errors can look like on startup.

http://img36.imageshack.us/img36/3947/debugscreen1.png
[NOTE]: No i don't use a repack, i just still use the Ac-web folder from when i used to =P
It will tell you what line the error is on, which is usually enough to go find whats wrong, but it also tells you whats wrong. even though sometimes it feels like its always saying the same thing.

Heres a list(that i can remember) of debugging errors i've ran into;



Forgetting a Left Parethisis to close a Function/Command.
Unit:SendChatMessage(41, 0, "The Lava Around Sarthiron Begins to Churn"

Forgetting a comma inside the Parameters of a Command.
Unit:SendChatMessage(41, 0 "The Lava Around Sarthiron Begins to Churn")

Concatenating a string with a nil value.
function Creature_OnCombat(Unit, Event)
local name -- Nil value, forgot to put player:GetName()
Unit:SendChatMessage(14, 0, "how come you be agroing me "..name)
end

Adding an integer with a nil value.
function Creature_OnCombat(Unit, Event)
count = count + 1 -- When the creature enters combat, count was never previously given a value, so it is nil, and it is being added to 1. To fix this you'd write 'local count = 0' at the top of the script.
end

Adding an integer to a string.
function Npc_OnGossip(Unit, Event, player)
Unit:GossipCreateMenu(100, player, 0)
Unit:GossipMenuAddItem(1, "Guessing Game", 0, 3)
Unit:GossipSendMenu(player)
end

function Npc_OnSubMenu(Unit, Event, player, id, intid, code)
codeplusone = code + 1 -- Code (Whatever the player wrote in the text box) will always return as a string, trying to add it to 1 will give you an error.
if (code == codeplusone) then
player:AddItem(29434, 20)
end
end

Forgetting or adding an extra 'end' to a function / if statement.
function Creature_OnCombat(Unit, Event)
if (Unit = nil) then
print"awmah gawd this creature doesn't exist and its attacking me!"
elseif
if (Unit ~= nil) then
print"phew this creature exists"
-- Should be an End here.
end
end

Using lowercase letters.
function Creature_OnCOmbat(Unit, Event)
unit:SendChatMessage(14, 0, "Why is this not working?") -- Lowercase unit at the start of the command.
end

As you can see most of these errors are from spelling mistakes (Where the Lua Engine also tells you what line the error is on), However when a certain phase isn't registering and you can't figure out why or where to look. Use of the print command can come in handy, To give you an idea of where to look.

The usage of the print command is as follors



print"insert debug message here"

print("Insert Message here")
Below is how it could be used when you're having a problem with your script



local count = 0

function Creature_OnCombat(Unit, Event)
Unit:RegisterEvent("Creature_PhaseOne", 1000, 1)
end

function Creature_PhaseOne(Unit, Event)
count = count + 1
if (count == 1) then
Unit:SendChatMessage(14, 0, "Im gonna cast a spell on you.")
elseif (count == 4) then
Unit:SendChatMessage(14, 0, "Im gonna send a chat message!")
elseif (count == 5) then
Unit:SendChatMessage(14, 0, "Watchout this script is about to break")
end

if Unit:GetHealthPct <= 80 then
-- register a new event.
else
Unit:RegisterEvent("Creature_PhaseOne", 1000, 1)
print("count is equal to"..count) -- This will print in the console every second what count is equal to. And then you can figure out why it isn't looping like you want it to.
-- The reason is, count was never reset back to 0 when it was equal to 5. so you would've found count would'v continuously went up.
end
end


If you find you're events are accelerating faster and faster casting a spell inside a function until the server crashes check the interval count on the end of when you register an event; I've done once.



function Creature_OnCombat(Unit, Event)
local name = player:GetName()
Unit:SendChatMessage(14, 0, "Why are you agroing me"..name)
Unit:RegisterEvent("Creature_CastSpell", 1000, 0) -- Put it as infinite, which registered the event when the event was registered.
end

function Creature_CastSpell(Unit, Event)
Unit:CastSpell(insertIDhere)

if Unit:GetHealthPct() <= 80 then
-- register new event
else
Unit:RegisterEvent("Creature_CastSpell", 1000, 1) -- You can guess where this led too
end
end
If you get an error about Unknown symbol near 'insert line of code here' Just rewrite that line, not really sure what causes it.

================================================== ==================

That brings us to the end of this tutorial, Hopefully you've learned something or had any questions you had answered. And remember, Sometimes just doing the boring work of 15 extra If statements / functions is worth having a move fun boss fight for.

Hopefully you can use what you've learned to be innovative and creative in your scripts.

This tutorial is finished, but i will still be adding things to it. PM me if you are curious how to do certain things or use certain commands. I will PM you back and then add an example and Explanation to this guide! Its win win.

Apple
20-02-10, 07:01 AM
Nice one +rep ,thanks for sharing

Fatbeard
20-02-10, 07:09 AM
Wow..... This is so long so it almost makes me depressed :( :P
Anyway, nice share! I guess if someone really wants to learn LUA, this is a good way to go :) +Rep

mag1212
20-02-10, 07:26 AM
this is very nice but shouldnt it be in the guide section

Snowcrown
20-02-10, 09:40 AM
Epic list ;) thanks and +rep

SerialKiller
20-02-10, 04:14 PM
tyvm for the +reps :p

Snowcrown
20-02-10, 04:24 PM
function PortalToThinkBox_OnUse(Unit, Event, player)
local Name = player:GetName()

if (Name == nil) then
player:SendAreaTriggerMessage("You cannot enter through here!")
elseif (Name == "GM name one") then
player:Teleport(1, 16227.4, 16403.8, -64)
elseif (Name == "GM name two") then
player:Teleport(1, 16227.4, 16403.8, -64)
elseif (Name == "GM name three") then
player:Teleport(1, 16227.4, 16403.8, -64)
elseif (Name == "GM name four") then
player:Teleport(1, 16227.4, 16403.8, -64)
elseif (Name == "GM name five") then
player:Teleport(1, 16227.4, 16403.8, -64)
else
player:SendBroadcastMessage("You are not a Game Master.")
player:Teleport(mapid, x, y, z) -- Mall Location is what i use.
end
end

-- Or of course, you can set GM/Donar names as variables..

RegisterGameObjectEvent(98814, 4, "PortalToThinkBox_OnUse")
-----------------------------------------------------------------------------------------------------------

FIX:
This can Easy be made with:

local GMrank = player:GetGmRank()
if (GMrank == 'az') then

All acc's with AZ can use it ;)

SerialKiller
24-02-10, 07:35 AM
ye ill add it soon

Alexeng
24-06-10, 03:07 PM
Really really really really REALLY sorry to bump this, but some of the scripting methods in this guide (i know you didnt make it, but im gonna point out to everyone that reads it) are considered unsafe and old, im going to give you an example.


local intCreatureAddDeadTrigger = 0

local intPhaseOneCount = 0


function Creature_OnCombat(Unit, Event)


Unit:SendChatMessage(41, 0, "The creature awakens.")

Unit:RegisterEvent("Creature_PhaseOne", 1000, 1)

Unit:SpawnCreature(NPCID, x, y, z, o, 14, 0)
end
function Creature_PhaseOne(Unit, Event)
intPhaseOneCount = intPhaseOneCount + 1
if (intPhaseOneCount == 15) then
-- After 15 seconds he'll do this and so on...
elseif (intPhaseOneCount == 30) then
-- and so on...
elseif (intPhaseOneCount == 35) then
-- ... and so on...
elseif (intPhaseOneCount == 50) then
-- On your last event you want to happen, set intPhaseOneCount back to 0 so it will continue to loop the events above.
end









if (intCreatureAddDeadTrigger == 1) then


Unit:RegisterEvent("Creature_PhaseTwo", 1000, 1)

else

Unit:RegisterEvent("Creature_PhaseOne", 1000, 1)
end
end
function Creature_PhaseTwo(Unit, Event)
-- awmahgawd phase 2.
end
function CreatureAdd_OnDied(Unit, Event)
intCreatureAddDeadTrigger = 1
end
RegisterUnitEvent(NPCID, 4, "CreatureAdd_OnDied")
RegisterUnitEvent(NPCID, 1, "Creature_OnCombat")










Now, as these



local intCreatureAddDeadTrigger = 0





local intPhaseOneCount = 0




are not in a function, and are subjected to change, they would cause a huge collide.
going to copy another persons guide about collision here, so you noobs that dont know what it is should understand. (This is not from mmopro)


So every single new script I have seen released always has the same few people commenting with the same defense to say the script is broken.






Lets make it clear that you fail to understand collision. I am going to make this as clear as possible for you.






local IWillCollide





function Collide_Event(pUnit, event)

IWillCollide = pUnit



end






That will collide.






function Collide_Event(pUnit, event)





local id = pUnit:GetInstanceID()

end






That will not collide.




Now enough of this OMG YOUR SCRIPT IS BROKZED BECUZ COLLIZE when you fail to even understand how collision works.


If a variable is declared on actual loading of the script like the first box it will collide if a unit in another instance is using it, NOT if a local one is declared within a function. If a Global one is declared within a function then it will collide also.

So take off your KnowItAll Caps and reread the tutorials before you "criticize" another person when you yourself don't know thou ass from thine elbow when it comes to collision.








So that is why some things this tutorial is teaching out is wrong, this is the old way of scripting and i suggest everyone go learn the new way.

PS, this post is broken because when i click preview it seems to automaticly add alot of things like CODE /CODE