PDA

View Full Version : LUA Tutorial



Zaronz
28-01-09, 04:03 PM
1. The Basic

Let's try script our NPC Where it's marked in red
InsertName We'll put the name of the function, this can be anything.

For example



function InsertName_OnCombat(Unit, Event)
= This will cause it do something when it enters combat.
For example



function InsertName_OnCombat(Unit, Event)
Unit:RegisterEvent("InsertName_Function", 7000, 0)
end
So now we will make an event happen when the NPC enters combat,
Now.. "Function" this is what the NPC will do. (Note: there is a list of functions at the bottom)

Then "7000" This is how many millisecond(s) it will take before the NPC does the Function =7seconds
You can set the timer to anything you want (Note: if the NPC is set to do the event infinite or multiple times it will do the Function each 7000 ms if it's set to.)

And the "0" This is how many times it will do the Function if it's set to 0 it will do it infinite times, if set to 1 it will do it once, 2 twice.. etc..


Then it's time for the function..



function InsertName_Function(pUnit, Event)
pUnit:Command
endWow.. So now i actually added some to it, this will cause the creature to do a unit command. (See the list of Commands)

Function - This is the Function you registered in the previous step

Command - This will cause the unit to do a Command, there's a list of commands at the bottom.

(Note: You can add multiple Commands to the function.))

Example: Let's say we want the creature to cast a Shadow Bolt.
So first, let's replace the InsertName with Warlock I'm just using the name Warlock as an example, you can name it whatever you want. So here's the script

function Warlock_ShadowBolt(pUnit, Event)
pUnit:FullCastSpellOnTarget(9613, pUnit:GetMainTank())
endWarlock - The Name (InsertName)

ShadowBolt - The Function, ( The Function can be named anything )

FullCastSpellOnTarget - The Unit Command - FullCastSpellOnTarget causes it to cast a spell on target with cast time and that stuff. You use the command CastSpellOnTarget - Then it will ignore the cast time. But i recommend using FullCastSpellOnTarget

9613 - The ID of the spell I used this: http://www.wowhead.com/?spell=9613 (http://cloaking.me/browse.php?u=Oi8vd3d3Lm1tb3Byby5uZXQvZm9ydW1zLyZxd W90O2h0dHA6Ly93d3cud293aGVhZC5jb20vP3NwZWxsPTk2MTM mcXVvdDs%3D&b=5)

GetMainTank - The Get Command i used GetMainTank, that will make the NPC cast the spell on the one that got the aggro.
You can use other Get commands aswell, check the list at the bottom.


Now the script should be something like this:

function Warlock_OnCombat(Unit, Event)
Unit:RegisterEvent("Warlock_ShadowBolt", 7000, 0)
end

function Warlock_ShadowBolt(pUnit, Event)
pUnit:FullCastSpellOnTarget(9613, pUnit:GetMainTank())
endThat will make the creature cast a Shadow Bolt on the main tank, every 7 second when it enters combat. But it won't stop doing that.. We gotta remove the event(s) when he Leaves combat or Dies.. Usually, there is exceptions! But for this script we'll remove the event then. So now we gotta add it to the script;


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

function Warlock_OnDied(Unit, Event)
Unit:RemoveEvents()
endRemoveEvents() - This will Remove all the events from the creature, that means the creature will stop doing the function.

OnDied - Triggers when the creature dies.

OnLeaveCombat - Triggers when the creature leaves combat.

We would need the creature to Register these so add these lines to the script,

RegisterUnitEvent(ID, 1, "Warlock_OnCombat")
RegisterUnitEvent(ID, 2, "Warlock_OnLeaveCombat")
RegisterUnitEvent(ID, 4, "Warlock_OnDied")ID - ID of the creature you want to script.


Now if you've followed the Basic tutorial your result would be something like this:

function Warlock_OnCombat(Unit, Event)
Unit:RegisterEvent("Warlock_ShadowBolt", 7000, 0)
end

function Warlock_ShadowBolt(pUnit, Event)
pUnit:FullCastSpellOnTarget(9613, pUnit:GetMainTank())
end

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

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

RegisterUnitEvent(ID, 1, "Warlock_OnCombat")
RegisterUnitEvent(ID, 2, "Warlock_OnLeaveCombat")
RegisterUnitEvent(ID, 4, "Warlock_OnDied")I will explain this further and add lot's of more content when i get time, (I'll try to update it daily)

List of commands


:AdvanceSkill(skillID, value) : Advances the skill line specified with the given value.
:AddSkill(skillID) : Adds the skill with the ID given to the target.
:RemoveSkill(skillID) : Removes the skill with the ID given from the target.
:PlaySpellVisual(guid, spellid) : Plays the specified spell's visual on the target given. See page for more info and usage.
:RemoveThreatByPtr(target) : Removes all threat from the given target.
:EventCastSpell(target, spell, delaytime, repeat) : Casts a spell over and over with the delay and repeat times given. See page for more info and usage.
:AttackReaction(target, damage, spell) : Creates "fake" threat towards the target specified.
:DismissPet() : Removes the pet of the unit.
:HandleEvent(event_ID, target, misc_1) : Allows the unit to handle the event specified. See page for more info and usage.
:SetMoveRunFlag(1 or 0) : Set to 1 to enable the unit to run, else set to 0.
:SendChatMessage(type, language, message) : Sends a message using the type and language given. See page for more info and usage.
:MoveTo(x, y, z, o) : Moves the unit to the coordinates given.
:SetMovementType(#) : Sets the unit's movement type. See page for more info and values.
:CastSpell(spellID) : Makes the unit cast an instant spell on itself using the spell ID given.
:CastSpellOnTarget(spellID, target) : Makes the unit cast an instant spell on it's target using the spell ID given.
:FullCastSpell() : Makes the unit cast a spell with cast time on itself using the spell ID given.
:FullCastSpellOnTarget() : Makes the unit cast a spell with casting time on it's target using the spell ID given.
:SpawnGameObject(entry, x, y, z, duration) : Spawns a game object with the entry given at the coordinates given. See page for more info and usage.
:SpawnCreature(entry, x, y, z, o, faction, duration) : Spawns an NPC at the coordinates given using the entry, faction, and duration given.
:RegisterEvent(Name, time, repeat) : Registers a new function with the name and time given, and repeating with the value given. See page for more info and usage.
:RemoveEvents() : Removes all events associated with the unit, see page for more info.
:SendBroadcastMessage(string) : Send the player targeted the specified message. See page for more info and usage.
:SendAreaTriggerMessage(string) : Sends all players in the area the message specified, will appear like an area trigger message. See page for more info and usage.
:KnockBack() : Check core for usage. Info will be given when tested.
:MarkQuestObjectiveAsComplete(questid, objective) : Updates the quest ID given with the quest objective given as complete. Check core for more info on it's usage.
:LearnSpell(SpellID) : Player only command. Teaches the target the specified spell.
:UnlearnSpell(SpellID) : Opposite from LearnSpell. Player only command.
:HasFinishedQuest(QuestID) : Checks if the target has finished the quest number given. Player only command.
:ClearThreatList() : Does not clear threat table, instead sets the threat of all units or players causing threat to 1.
:ChangeTarget(target) : Changes the target of the unit to the target specified.
:Emote(emoteID, time) : Makes the unit perform or sustain the given emote. Time is optional, performs the emote after the specified time is up.
:Despawn() : When called, makes the unit or targeted unit despawn.
:PlaySoundToSet(SoundID) : Plays the given sound ID to the entire world. See page for more info.
:RemoveAura(SpellID) : If the unit has the aura with the spell given, will remove it.
:StopMovement(delaytime) : Stops the unit from moving using the delay time given, resumes after time is up. Unit only command.
:AddItem(ItemID) : Player only command. Adds the item specified to the targeted player.
:RemoveItem(ItemID) : Player only command. Removes the item specified from the targeted player.
:CreateCustomWaypointMap() : Creates a custom waypoint map to add waypoints to.
:CreateWaypoint(X, Y, Z, WaitTime, MoveFlags, DisplayID) : Creates a waypoint with the values given. Also creates a waypoint map if one is not already created. See page for more info.
:DestroyCustomWaypointMap() : Deletes the waypoint map, if one was created.
:MoveToWaypoint(WaypointID) : Moves the unit to the waypoint ID specified. See page for more info.
:TeleportUnit(mapid, X, Y, Z) : Teleports the unit to the coordinates given, zone is not needed.
:ClearHateList() : Same as ClearThreatList, sets all threat to 1.
:WipeHateList() : Will clear ALL threat from the unit, causing it to leave combat and return to it's spawn.
:WipeTargetList() : Does the same thing as WipeHateList.
:WipeCurrentTarget() : Causes the unit to disengage from the current target, then targeting the next target. Current target leaves combat.
:CastSpellAoF(X, Y, Z, SpellID) : Used for AoE (area of effect) spells being cast by the unit. Casts the spell given at the coordinates given.
:RemoveAllAuras() : Removes all auras, positive or negative, from the unit or target.
:StopChannel() : If the unit is channeling a spell, causes the unit to stop channeling.
:ChannelSpell(guid, spellID) : Used when casting a channeled spell, casts on the target's guid. See page for more info and usage.
:ReturnToSpawnPoint() : Unit returns to it's original spawn point.
:HasAura(spellID) : Checks if the target or unit has the spell aura specified.
:Land() : Causes the unit or target to land, removing the ability to fly.
:CancelSpell() : Cancels the unit's current spell being cast.
:Root() : Stops all movement.
:Unroot() : Resumes movement if waypoints exist.
:CalcDistance() : Check core for usage.
:ModUInt32Value(field, value) : Similar to SetUInt32Value.
:ModFloatValue() : Similar to SetFloatValue.
:SendData() : If data is collected prior to calling this function, will send to unit. See page for more info and usage.
:InitPacket() : Check core for usage.
:AddDataToPacket() : Check core for usage.
:AddGuidDataToPacket() : Check core for usage.
:AdvanceQuestObjective(QuestID, objective) : Similar to CompleteQuestObjective.
:Heal(target, SpellID, amount) : Uses the spell given on the target given, but heals the amount given instead of the amount the spell uses.
:Energize(target, spellid, amount, type) : Similar to Heal, but depends on the power type. See page for more info and usage.
:SendChatMessageAlternateEntry(entry, type, language, message) : Similar to SendChatMessage, but uses the unit given (entry).
:SendChatMessageToPlayer(type, language, message, plr) : Similar to SendChatMessage, sends the messsage if the player exists.
:Strike(target, weapon_damage_type, dbcSpell.LookupEntry(sp), adddmg, pct_dmg_mod,exclusive_damage) : See core for usage.
:Kill(target) : Kills the selected target.
:DealDamage(target, damage, spellID) : Uses the spell visual to deal the amount of damage given to the target selected.
:CreateGuardian(etry, duration, angle) : Check core for usage.
:CalcToDistance(X, Y, Z) : Check core for usage.
:CalcAngle(X, Y, X2, Y2) : Check core for usage.
:CalcRadAngle(X, Y, DX, DY) : Check core for usage.
:IsInvisible() : Returns true if the target is invisible, else returns false.
:IsInvincible() : Returns true if the target is invincible, else returns false.
:ResurrectPlayer() : Check core for usage.
:KickPlayer(Name, delay) : Kicks the player with name given, after the delay time is up.
:CanCallForHelp(1 or 0) : Sets the unit to be able to call for help if set to 1, else when set to 0, can not call for help.
:CallForHelpHp() : Check core for usage.
:RemoveFromWorld() : Removes the target from world if target exists (kick).
:SpellNonMeleeDamageLog() : Places entries into the players combat log using the information specified. Check core for info and usage.
:ModThreat(target, amount) : Changes the amount of threat from the target to the amount specified.
:AddAssistTargets() : If a friendly unit is found, will attack the unit's target. Check core for usage.
:RemoveAurasByMechanic(string, 1 or 0) : Removes auras with the mechanic specified in string form, set to 1 for only the hostile auras, set to 0 to remove all auras with given mechanicGet Commands


:GetPlayerRace() : Returns the player's race to the unit's script.
:GetCurrentSpellId() : If a unit is casting a spell, this will return the spell ID of the spell that the unit is casting.
:GetStanding(FactionID) : Returns the reputation value of the player to the unit.
:GetMainTank() : Returns and selects the player that is currently causing the most threat to the unit.
:GetAddTank() : Returns and selects the player currently causing the second most threat to the unit.
:GetX() : Returns the X coordinate of the unit or player.
:GetY() : Returns the Y coordinate of the unit or player.
:GetZ() : Returns the Z coordinate of the unit or player.
:GetO() : Returns the orientation of the unit or player.
:GetTauntedBy() : Returns the player the unit was taunted by.
:GetSoulLinkedWith() : Returns the unit the scripted unit is soul linked with, if any exist.
:GetItemCount(ItemID) : Returns the amount of items the player has, depending on the ItemID given.
:GetName() : Returns the name of the unit or selected player.
:GetHealthPct() : Returns the percentage of health the unit or selected player currently has.
:GetManaPct() : Returns the percentage of mana the unit or selected player currently has.
:GetInstanceID() : Returns the instance ID the unit or selected player is in, if any.
:GetClosestPlayer() : Returns and selects the closest player.
:GetRandomPlayer(#) : Returns and selects a random player, depending on the number given.
:GetRandomFriend() : Returns and selects a unit friendly to the scripted unit.
:GetUnitBySqlId(SQLID) : Returns a unit using it's SQL ID. Use ".npc info" to get the SQL ID in-game.
:GetPlayerClass() : Returns the selected player's class ID.
:GetHealth() : Returns the unit's or selected player's exact health.
:GetMaxHealth() : Returns the unit's or selected player's maximum amount of health.
:GetMana() : Returns the unit's or selected player's exact amount of mana.
:GetMaxMana() : Returns the unit's or selected player's maximum amount of mana.
:GetCreatureNearestCoords(X, Y, Z, NPCID) : Returns the unit if it is found. Check corresponding page for usage.
:GetGameObjectNearestCoords(X, Y, Z, OBJECTID) : Returns the object as a unit if it is found. Check corresponding page for usage.
:GetDistance(target) : Returns the distance from the unit and selected target.
:GetGUID(guid) : Returns a uint64 guid number that is unique to every player, unit, or game object.
:GetZoneID() : Returns the Zone ID that the unit is currently in.
:GetCurrentSpell() : Similar to GetCurrentSpellID.
:GetSpawnX() : Returns the X coordinate where the unit originally spawned.
:GetSpawny() : Returns the Y coordinate where the unit originally spawned.
:GetSpawnZ() : Returns the Z coordinate where the unit originally spawned.
:GetSpawnO() : Returns the orientation of the unit where it originally spawned.
:GetInRangePlayersCount() : If the unit has any in range players, will return the number of players at that time.
:GetUInt32Value(field) : Check core for info on how it is used. Uncommonly used command in Lua scripts.
:GetUInt64Value(field) : Check core for info on how it is used. Uncommonly used command in Lua scripts.
:GetFloatValue(field) : Check core for info on how it is used. Uncommonly used command in Lua scripts.
:GetAIState() : Returns the numerical value of the AI state the unit is in.
:GetInRangeGameObjects() : If the unit has any in range objects, will return the number of objects at that time.
:GetInRangePlayers() : Similar to GetInRangePlayersCount.
:GetAITargets() : Returns a table that lists all of the targets the unit has. Also known as the threat table.
:GetUnitByGUID(guid) : Returns the unit that has the specified guid.
:GetInRangeObjectsCount() : Similar to GetInRangeGameObjects, but returns the number of objects.
:GetAITargetsCount() : Returns the number of targets the unit has.
:GetUnitToFollow(target) : Similar to SetUnitToFollow. Check core for more info on usage.
:GetNextTarget() : Selects the player causing second most threat.
:GetPetOwner() : Returns the name of the unit or player that "owns" the pet selected or defined.
:GetEntry() : Returns the entry ID for the selected unit or target, else returns the entry ID of the unit itself if no target is selected.
:GetFaction() : Returns the faction for the selected unit or target, else returns the faction of the unit itself if no target is selected.
:GetThreatByPtr(target) : Check core for info on how it is used. Uncommonly used command in Lua scripts.
:GetInRangeFriends() : Selects in range units friendly to the scripted unit.
:GetPowerType() : Returns the power type in string form, "Health", "Mana", "Rage", etc.
:GetMapId() : Returns the map ID the unit or target is currently in.
:GetFactionStanding(faction) : Returns the reputation value of the faction specified of the selected target.
:GetPlayerLevel() : Returns the targeted player's level to the unit.



CREDITS: ArcEmu Wiki for the Commands
MORE TO COME!glglgl

Avidgamer
28-01-09, 04:07 PM
Very nice. +rep

Zaronz
28-01-09, 04:25 PM
Thank you Avidgamer (http://www.mmopro.net/forums/members/avidgamer.html) :)

Zaronz
29-01-09, 11:11 AM
No one else interested? :(
There's no point to continue this thread if none is interested

QQrofl
29-01-09, 11:36 AM
Just have to give people time to make a LUA script and such. But very nice. +rep

Zaronz
29-01-09, 12:41 PM
Thank you =P

I will try to update it so it will be tutorials from beginner to advanced,