ArcEmu: Lua Server Hooks Tutorial - ArcEmu

Jump to content

Toggle shoutbox Lastest Announcements

dfighter  : (07 December 2014 - 12:06 PM) Arcemu is in hibernation mode, please read http://arcemu.org/fo...showtopic=26903
dfighter  : (01 January 2013 - 05:56 PM) Arcemu wishes you all a happy new year!
Hasbro  : (12 September 2012 - 10:01 AM) Please excuse our outage from the web! Our web host had a major malfunction!
dfighter  : (01 September 2012 - 04:05 PM) Since the spam bots just don't want to stop, I've enabled admin verification when registering.
dfighter  : (23 January 2012 - 09:56 PM) Please note that from now on you will need to confirm your email on the wiki in order to edit it!
Hasbro  : (31 December 2011 - 12:50 PM) Happy New Years all!
Navid  : (26 December 2011 - 04:09 AM) Merry Christmas !!!!!! Happy holidays all :)
WAmadeus  : (24 December 2011 - 03:54 PM) Merry Christmas to all!
dfighter  : (24 December 2011 - 11:05 AM) The Arcemu team wishes y'all a Merry Christmukkah!
Hasbro  : (05 October 2011 - 12:53 PM) Looking for web designers for upcoming web related project. If you're interested in designing user interfaces contact me
dfighter  : (02 September 2011 - 03:47 PM) So who here wants vehicles in Arcemu? :P http://arcemu.org/fo...showtopic=25440
Hasbro  : (14 August 2011 - 03:25 PM) Join us on irc, grab an irc client and connect to irc.freenode.net join channel #arcemu /server irc.freenode.net:6667 /join #arcemu
jackpoz  : (03 August 2011 - 05:33 AM) to all Lua Engine (old one) users: please check http://arcemu.org/fo...showtopic=25274
Hasbro  : (20 May 2011 - 05:27 PM) Looking for people experienced with CMake configuration and setup! Contact me asap
Hasbro  : (15 May 2011 - 05:03 PM) ArcEmu is recruiting C++ programmers, contact Hasbro if interested.
paroxysm  : (03 May 2011 - 06:26 PM) Updated luabridge gossip example to describe the whole gossip creation process rather than just how to create menu. Gossip tutorial
paroxysm  : (23 April 2011 - 11:35 AM) Lua writers can refer to the Luabridge Tutorials section in the Wiki to learn how to write gossip code correctly.
Hasbro  : (20 April 2011 - 05:22 PM) Thank you for your continuous contribution of bug reports, we are working on them.
Hasbro  : (17 April 2011 - 03:20 AM) Please consider donating to support our bills. Donations can be sent using PayPal to donations@arcemu.org - Thank you for your support.
paroxysm  : (10 April 2011 - 12:43 AM) Refer to the Luabridge Tutorials section in the Wiki to learn the new syntax of luabridge.
Resize Shouts Area

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Lua Server Hooks Tutorial For LuaHypArc

#1 User is offline   hypersniper 

  • Advanced Member
  • Group: Retired
  • Posts: 227
  • Joined: 21-November 08
  • Gender:Male

Posted 20 September 2009 - 05:13 AM

Lua Server Hooks
This entire tutorial is only for users with LuaHypArc v2004+. See http://luahyparc.zapto.org/

1. What are Server Hooks?
Server hooks are just like Creature events (OnLoad, OnCombat, OnDeath etc), except that instead of creatures, server hooks are for the entire server. A few examples of server hook events include OnCastSpell, OnEnterWorld, OnHonorableKill.

2. Working through an example
Using them is just like your Creature events. You make a function and register it using RegisterServerHook. Let's work through an example.

The goal: To make a welcome message appear to a player when they log in.

Firstly, if you didn't know, to make a message appear on the player's screen you use player:SendAreaTriggerMessage(message). Alright, let's give it a try. I'm going to put "? ? ?" on places where I'm not sure what to do, since this is hard stuff.
function Welcome(???)
???:SendAreaTriggerMessage("Welcome to the game!")
end

RegisterServerHook(???)

OK, let's see here. I don't know what to put in the brackets. And since I don't know what's in the brackets, I don't have pUnit or player to put before the :. I also don't know what to put in RegisterServerHook.

After referring to the LuaHypArc changelog, I realise that RegisterServerHook(event, functionName). That's kinda like RegisterUnitEvent, isn't it? We don't need an entry id for server hooks, though. Let's fill in our "? ? ?" accordingly.
function Welcome(???)
???:SendAreaTriggerMessage("Welcome to the game!")
end

RegisterServerHook(???, "Welcome")

Hmm. Still plenty of "? ? ?". Let's take a look at a list I included in LuaHypArc, "servhookdocu1.txt".
SERVER HOOK NUMERICAL IDS

SERVER_HOOK_NEW_CHARACTER = 1
SERVER_HOOK_KILL_PLAYER = 2
SERVER_HOOK_FIRST_ENTER_WORLD = 3
SERVER_HOOK_ENTER_WORLD = 4
SERVER_HOOK_GUILD_JOIN = 5
SERVER_HOOK_DEATH = 6
SERVER_HOOK_REPOP = 7
SERVER_HOOK_EMOTE = 8
SERVER_HOOK_ENTER_COMBAT = 9
SERVER_HOOK_CAST_SPELL = 10
SERVER_HOOK_TICK = 11
SERVER_HOOK_LOGOUT_REQUEST = 12
SERVER_HOOK_LOGOUT = 13
SERVER_HOOK_QUEST_ACCEPT = 14
SERVER_HOOK_ZONE = 15
SERVER_HOOK_CHAT = 16
SERVER_HOOK_LOOT = 17
SERVER_HOOK_GUILD_CREATE = 18
SERVER_HOOK_ENTER_WORLD_2 = 19
SERVER_HOOK_CHARACTER_CREATE = 20
SERVER_HOOK_QUEST_CANCELLED = 21
SERVER_HOOK_QUEST_FINISHED = 22
SERVER_HOOK_HONORABLE_KILL = 23
SERVER_HOOK_ARENA_FINISH = 24
SERVER_HOOK_OBJECTLOOT = 25
SERVER_HOOK_AREATRIGGER = 26
SERVER_HOOK_POST_LEVELUP = 27
SERVER_HOOK_PRE_DIE = 28
SERVER_HOOK_ADVANCE_SKILLLINE = 29

That definitely will help us. They show the numerical IDs for each server hook event. Which event do we want? Well, we need for when the player logs in. We should use SERVER_HOOK_ENTER_WORLD_2 for this. (I'll tell you a secret - SERVER_HOOK_ENTER_WORLD triggers every map change, not just on log in - that's why we use enter world 2). SERVER_HOOK_ENTER_WORLD_2 = 19, so let's plug it in.
function Welcome(???)
???:SendAreaTriggerMessage("Welcome to the game!")
end

RegisterServerHook(19, "Welcome")

Nice. Let's not waste any time in filling in the other "? ? ?". Let's refer to the second text file included in HypArc, "serverhookdocu2.txt".
LUA ARGUMENTS AVAILABLE TO SERVER HOOKS

OnNewCharacter(event, pPlayer, name, race, class)
OnKillPlayer(event, pPlayer, pVictim)
OnFirstEnterWorld(event, pPlayer)
OnEnterWorld(event, pPlayer)
OnGuildJoin(event, pPlayer, guildName)
OnDeath(event, pPlayer)
OnRepop(event, pPlayer)
OnEmote(event, pPlayer, pUnit, emoteId)
OnEnterCombat(event, pPlayer, pTarget)
OnCastSpell(event, pPlayer, spellId)
OnTick(event)
OnLogoutRequest(event, pPlayer)
OnLogout(event, pPlayer)
OnQuestAccept(event, pPlayer, pQuestGiver, questId)
OnZone(event, pPlayer, zoneId)
OnChat(event, pPlayer, message, type, language)
OnLoot(event, pPlayer, money, itemId)
OnGuildCreate(event, pLeader, guildName)
OnEnterWorld2(event, pPlayer)
OnCharacterCreate(event, pPlayer)
OnQuestCancelled(event, pPlayer, questId)
OnQuestFinished(event, pPlayer, pQuestGiver, questId)
OnHonorableKill(event, pPlayer, pKilled)
OnArenaFinish(event, pPlayer, victory, rated)
OnObjectLoot(event, pPlayer, pTarget, money, itemId)
OnAreaTrigger(event, pPlayer, areaTriggerId)
OnPostLevelUp(event, pPlayer)
OnPreUnitDie(event, killer, victim)
OnAdvanceSkillLine(event, pPlayer, skillId, current)

(Note: In the text file the first function has a typo, it should read OnNewCharacter(event, pPlayer, name, race, class) )
Interesting. This is a list of the arguments (aka 'brackets-after-the-function') we can use with our server hooks. Great! I search for OnEnterWorld2, grab it's bracketty stuff and whack it in.
function Welcome(event, pPlayer)
???:SendAreaTriggerMessage("Welcome to the game!")
end

RegisterServerHook(19, "Welcome")

And, now that I've got stuff in the brackets, I know that pPlayer should go in the final "? ? ?", because pPlayer points to the player that is logging in (just like how pUnit points to the creature in creature events)
function Welcome(event, pPlayer)
pPlayer:SendAreaTriggerMessage("Welcome to the game!")
end

RegisterServerHook(19, "Welcome")


3. Don't be overwhelmed
If you're going "GASP INFORMATION OVERLOAD" I understand. The best advice I can give you is to play around with it, experiment, and do NOT hesitate to ask for personal help in this thread. I'm happy to personally answer anything.

4. Further possibilities?
There are many doors now unlocked by this new feature. More advanced possibilities include sending players to a personal training ground when they create their character, giving players a token when they kill another player, making events happening based on player chat, making Lua events occur when a player equips an item, and much more. Be creative, show the community what you can do!
Please don't PM me asking to fix, correct or look-over your scripts. Please post a new thread in the Lua Scripting section so others can learn and help.
0

#2 User is offline   prod 

  • Member
  • Pip
  • Group: Members
  • Posts: 25
  • Joined: 05-December 08

Posted 23 September 2009 - 08:13 PM

Hey dude i got Question.


ReloadLuaEngine() +- Reloads the Lua engine & scripts.


How do i use this?
0

#3 User is offline   hypersniper 

  • Advanced Member
  • Group: Retired
  • Posts: 227
  • Joined: 21-November 08
  • Gender:Male

Posted 23 September 2009 - 09:14 PM

Unfortunately that command doesn't have much real use, only on an admin item or similar. What it does is resets the Lua engine, and reloads all scripts, so if you make a modification of a script you only have to do ReloadLuaEngine() instead of restarting the whole server. That's the idea of it anyway. :P
Please don't PM me asking to fix, correct or look-over your scripts. Please post a new thread in the Lua Scripting section so others can learn and help.
0

#4 User is offline   prod 

  • Member
  • Pip
  • Group: Members
  • Posts: 25
  • Joined: 05-December 08

Posted 23 September 2009 - 10:44 PM

View Posthypersniper, on 23 September 2009 - 09:14 PM, said:

Unfortunately that command doesn't have much real use, only on an admin item or similar. What it does is resets the Lua engine, and reloads all scripts, so if you make a modification of a script you only have to do ReloadLuaEngine() instead of restarting the whole server. That's the idea of it anyway. :)


thanks that answered my question :P!
0

#5 User is offline   Muffler 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 366
  • Joined: 17-June 08
  • Gender:Male
  • Location:CCCP

Posted 24 September 2009 - 01:56 PM

function VidereElixir(event, pPlayer, spellId)
	  if spellId == 14050 then
	   pPlayer:Kill()
	end
end
RegisterServerHook (10,"VidereElixir")


http://www.wowhead.com/?item=11243

0

#6 User is offline   Shauren 

  • Helper
  • Group: Contributor
  • Posts: 808
  • Joined: 04-November 08
  • Gender:Male
  • Location:Poland
  • Interests:hmm, wow?

Posted 25 September 2009 - 09:56 AM

what is that for Muffler? this spell effect should already kill you
0

#7 User is offline   Muffler 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 366
  • Joined: 17-June 08
  • Gender:Male
  • Location:CCCP

Posted 01 October 2009 - 09:27 AM

can some help me ?
quest system broke (ReqCastSpellId not working) ad i try make quest

http://www.wowhead.com/?quest=8150
function q_test(event, pPlayer, spellId)
	  if spellId == 24195 then
	   pPlayer:MarkQuestObjectiveAsComplete(8150)
	   --pPlayer:FinishQuest(8150)
	end
end
RegisterServerHook (10,"q_test")


after start cast server crash ^_^

What wrong ?

0

#8 User is offline   Shauren 

  • Helper
  • Group: Contributor
  • Posts: 808
  • Joined: 04-November 08
  • Gender:Male
  • Location:Poland
  • Interests:hmm, wow?

Posted 01 October 2009 - 01:54 PM

pPlayer:MarkQuestObjectiveAsComplete(8150, 0)


This should look like this (questid, objective index)
objective index is 0 - 3
0

#9 User is offline   Muffler 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 366
  • Joined: 17-June 08
  • Gender:Male
  • Location:CCCP

Posted 01 October 2009 - 03:32 PM

crash to ^_^

0

#10 User is offline   Muffler 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 366
  • Joined: 17-June 08
  • Gender:Male
  • Location:CCCP

Posted 02 October 2009 - 04:48 AM

hypersniper can you help me ?

0

#11 User is offline   Ikillonyxia 

  • Member
  • Pip
  • Group: Members
  • Posts: 59
  • Joined: 07-June 08

Posted 03 October 2009 - 07:24 PM

Very nice work. I'm really glad to see that the LUA engine has expanded so much since my departure, it's really cool to come back to scripting with a plentiful engine.
0

#12 User is offline   hypersniper 

  • Advanced Member
  • Group: Retired
  • Posts: 227
  • Joined: 21-November 08
  • Gender:Male

Posted 03 October 2009 - 07:35 PM

View PostIkillonyxia, on 03 October 2009 - 07:24 PM, said:

Very nice work. I'm really glad to see that the LUA engine has expanded so much since my departure, it's really cool to come back to scripting with a plentiful engine.


:) Thank you so much, means a lot to me.

@muffler - MarkQuestObjectiveAsComplete does not cause crashes, it must be something else, either way nothing to do with HypArc/Server hooks sorry :)
Please don't PM me asking to fix, correct or look-over your scripts. Please post a new thread in the Lua Scripting section so others can learn and help.
0

#13 User is offline   Muffler 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 366
  • Joined: 17-June 08
  • Gender:Male
  • Location:CCCP

Posted 05 October 2009 - 04:03 PM

i test in default lua engine and crash to ;)

0

#14 User is offline   Muffler 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 366
  • Joined: 17-June 08
  • Gender:Male
  • Location:CCCP

Posted 15 October 2009 - 04:33 AM

if i make two file

file1.lua
function file1(event, pPlayer, spellId)
	if spellId == 53 then
	   print (" cast Backstab")
	end
end
RegisterServerHook (10,"file1")


file2.lua
function file2(event, pPlayer, spellId)
	 print (spellId)
end
RegisterServerHook (10,"file2")


works only one script
Why ?

0

#15 User is offline   Muffler 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 366
  • Joined: 17-June 08
  • Gender:Male
  • Location:CCCP

Posted 21 October 2009 - 06:02 AM

pls any example for SERVER_HOOK_ZONE = 15
Me interest how get user count in any zone

0

#16 User is offline   Muffler 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 366
  • Joined: 17-June 08
  • Gender:Male
  • Location:CCCP

Posted 23 October 2009 - 06:53 AM

View PostMuffler, on 21 October 2009 - 06:02 AM, said:

pls any example for SERVER_HOOK_ZONE = 15
Me interest how get user count in any zone

i find :blink:

function OnArea (event, player, zone)
  print("Area '"..zone.."' have "..table.getn(GetPlayersInZone(zone)).." char")
end
RegisterServerHook(15, "OnArea")


0

#17 User is offline   Phail 

  • Member
  • Pip
  • Group: Members
  • Posts: 22
  • Joined: 25-April 09

Posted 12 November 2009 - 09:38 AM

Did you really need to quad-post that?
0

#18 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 14 November 2009 - 11:38 AM

I'm trying to learn more scripting languages, and get out of my PHP domain and test out LUA. This might sound really, really stupid, but how do I get a player's faction?

What I'm trying to do is OnChat, if a player says '#Home' the script checks for the faction, and based off that, sends the player to Orgrimmar/Stormwind. Thanks for the help.
0

#19 User is offline   Night 

  • Member
  • Pip
  • Group: Members
  • Posts: 53
  • Joined: 22-July 08

Posted 14 November 2009 - 11:40 AM

plr:GetFaction() ?
Posted Image
Posted Image
0

#20 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 14 November 2009 - 11:57 AM

Does that exist?

Would I do:

	if(message == HOME) then
		if(plr:GetFaction()) == 1) then
			--Alliance--
		end
		elseif(plr:GetFaction() == 2) then
			--Horde--
		end
	end


edit

I looked at a teleport script... and would this work? (I don't have access to my server atm)

local race=player:GetPlayerRace()

	if(message == HOME) then
		if(race==1 or race==3 or race==4 or race==7 or race==11) then
			--Horde--
		end
		elseif(race==2 or race==5 or race==6 or race==8 or race==1) then
			--Alliance--
		end
	end

0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users