Disassembling widely used LUA

From ArcEmu-Wiki
Jump to: navigation, search

Contents



This page will try to cover the wide use of menus to better facilitate the learning of what multitudes of people are tossing into their scripts, thinking that it "works" as is. When really they are missing out on the functionality of their scripts. In this page I will discuss a weather npc menu that was based on templates that, many scriptwriters use without knowledge of all fields.


Comments

Comments in LUA are done with a

 --

or for multi-line comments:

 --[[
Im a comment
]]
You can just prefix every newline that is a comment with a
 --
but its up to you how you want your code to look, use the multi-line comment example above for parsers like Ohloh.


Begin

function W_NPC_main_menu(pUnit, player)
	if (player:IsGm() == true) then
	pUnit:GossipCreateMenu(5000001, player, 0)
	pUnit:GossipMenuAddItem(5, "Rain", 1, 0)
	pUnit:GossipMenuAddItem(5, "Snow", 2, 0)
	pUnit:GossipMenuAddItem(5, "Heavy rain", 4, 0)
	pUnit:GossipMenuAddItem(5, "Sandstorm", 5, 0)
	pUnit:GossipMenuAddItem(5, "Sunny", 6, 0)
	pUnit:GossipMenuAddItem(5, "Kalimdor Heavy Rain", 7, 0)
	pUnit:GossipMenuAddItem(5, "Kalimdor Sunlight", 8, 0)	
	pUnit:GossipMenuAddItem(5, "Eastern Kingdoms Heavy Rain", 9, 0)
	pUnit:GossipMenuAddItem(5, "Eastern Kingdoms Sunlight", 10, 0)	
	pUnit:GossipMenuAddItem(2, "Close", 11, 0)
	pUnit:GossipSendMenu(player)
	else
	PLAYER_NAME = player:GetName()
	pUnit:SendChatMessage(16, 0, ""..PLAYER_NAME.." accessed GM script without proper authorization.")
	player:SoftDisconnect()
	end
end
 
function W_NPC_on_gossip_talk(pUnit, event, player)
   W_NPC_main_menu(pUnit, player)
end
 
function W_NPC_on_gossip_select(pUnit, event, player, id, intid, code)
	if ( intid == 1) then
	player:SetPlayerWeather(2, 2.0)
	player:GossipComplete()
end
 
	if ( intid == 2) then
	player:SetPlayerWeather(8, 2.0)
	player:GossipComplete()
end
 
	if ( intid == 3) then
	player:SetPlayerWeather(1, 2.0)
	player:GossipComplete()
end
 
	if ( intid == 4) then
	player:SetPlayerWeather(4, 2.0)
	player:GossipComplete()
end
 
	if ( intid == 5) then
	player:SetPlayerWeather(16, 2.0)
	player:GossipComplete()
end
 
	if ( intid == 6) then
	player:SetPlayerWeather(0, 0)
	player:GossipComplete()
end
 
	if ( intid == 7) then
	pUnit:SetZoneWeather(1, 4, 2.0)
	player:GossipComplete()
end
 
	if ( intid == 8) then
	pUnit:SetZoneWeather(1, 0, 0)
	player:GossipComplete()
end
 
	if ( intid == 9) then
	pUnit:SetZoneWeather(2, 4, 2.0)
	player:GossipComplete()
end
 
	if ( intid == 10) then
	pUnit:SetZoneWeather(2, 0, 0)
	player:GossipComplete()
end
 
	if ( intid == 11) then
	player:GossipComplete()
end
end
 
RegisterUnitGossipEvent(5000055,1,"W_NPC_on_gossip_talk")
RegisterUnitGossipEvent(5000055,2,"W_NPC_on_gossip_select")
 
--		WEATHER_TYPE_NORMAL            = 0, // NORMAL (SUNNY)
--		WEATHER_TYPE_FOG               = 1, // FOG
--		WEATHER_TYPE_RAIN              = 2, // RAIN
--		WEATHER_TYPE_HEAVY_RAIN        = 4, // HEAVY_RAIN
--		WEATHER_TYPE_SNOW              = 8, // SNOW
--		WEATHER_TYPE_SANDSTORM         = 16 // SANDSTORM
--		Unit:SetZoneWeather(zone_id,type,density)

This is a fully functional script. But ah, how do I make one myself?


Steps to scripting your own NPCs

1.

  • Picking a function name that will not overwrite or interfere with other scripts. In this script that name is W_NPC

2.

  • Picking what you will be using in your NPC's menu. In this script this is (pUnit, player)

3.

This is a GM script, why? Because players accessing it could cause lulz in the server by spamming commands.

  • Pick your security if you need to, this uses soft disconnection + says the players name that accessed it without permissions.
if (player:IsGm() == true) then
 
 
else
PLAYER_NAME = player:GetName()
pUnit:SendChatMessage(16, 0, ""..PLAYER_NAME.." accessed GM script without proper authorization.")
player:SoftDisconnect()
end
 
 
 
''' Read with me ''' if player is a game master give him this if player is '''not''' a game master disconnect him and say his name.
 
The script is then ended, even after the if, as that end inside of the if is for the if only. This is however not the end of file.


4.


We skipped over alot of whats inside that big block with that if now didn't we?

Lets go over that then.

	pUnit:GossipCreateMenu(5000001, player, 0)

But what does that mean?

	pUnit:GossipCreateMenu(GossipID, player, 0)
 
 
--Gossip ID Is the gossip index inside of your database that you want to use for you NPC, even storyteller NPCs are possible.

5.

Underneath that there was more? what about that?

That would be

	pUnit:GossipMenuAddItem(5, "Rain", 1, 0)

which actually is

	pUnit:GossipMenuAddItem(ICONID, "OUTPUT", INTID, 0) </pre
 
*ICONID is an icon that you can select from, a speech bubble, a tabard, a gear, and a few others.
** INTID is the index of what you want to access inside the script.

We then go onto

How to make a proper INTID

An INTID is a way of calling a "what to do" after a player "clicks on this". Widely used for gossip menus and boss scripts alike.

	if ( intid == 1) then
	player:SetPlayerWeather(2, 2.0)
	player:GossipComplete()
end
 
We can see from this, this is INTID 1. Or so it thinks it is. Multiples of INTIDs should be able to be called underneath this, but there is usually no reason for that.


This script then adjusts PlayerWeather to 2, density 2.0 and then GossipCompletes which closes the gossip window.


Almost done?

Yeah. Almost done.


What about the bottom of that script?

The bottom of that script is

RegisterUnitGossipEvent(5000055,1,"W_NPC_on_gossip_talk")
RegisterUnitGossipEvent(5000055,2,"W_NPC_on_gossip_select")

-- which actually is

RegisterUnitGossipEvent(NPCID,What this register does,"functions")
RegisterUnitGossipEvent(NPCID,What this register does,"functions")
 
--[[
NPCID, the id of the creature this script applies to
 
The number in the What this register does field applies to how the function is triggered. ]]
See: Lua_HandleEvent
 --[[

    Functions, these control the names of the gossip talks and selects. they should not conflict with other scripts, all that is needed is a difference in function names.   and before that two ends.   Traditionally in a menu script a EOF is marked as two ends, multiples of ends being needed if a IF is still presently running on that code.

]]


Locals and globals?

 
Personal tools