Reloading a specified script in-game using a small Lua script
Content
- Introduction
- Which one to use?
- How to set it up
- How to use
- The script - utilizing a chat hook
- The script - utilizing a gossip script
- Extra information
Introduction
Are you one of those guys who uses a newer ArcEmu rev than 3089? Or are you one of those guys running a non-ArcEmu server with a Lua Engine? And are you wanting to reload a script in-game? Well, then this is for you.
Which one to use?
If you run a Lua engine which has a chat hook then I recommend the one with the chat hook as it's faster to type it immediately instead of having to open up a gossip and a codebox.
If you do not have a Lua engine with a chat hook then the gossip script is for you. Using the codebox you will be able to specify which script you want to load.
How to set it up?
If you're using the chat hook version then it's a simple matter of drag and drop in your scripts folder just as you would with any Lua script.
If you're using the gossip version you'll have to add a gossip item to your database first, change the ITEM_ID_GOES_HERE to the newly added item and add the Lua script to your scripts folder. Don't know how to make a gossip item? Then I point you in the direction of this guide: Sign In
How to use?
Using the chat hook version you can simply type: #ReloadScript <name of the script> . Thus if I have a script called "MyLua" in my scripts folder I type "#ReloadEngine MyLua".
The gossip script uses the codebox to ask for the script name. Thus if you have the codebox open and you have a script called "MyLua" you simply have to type "MyLua" in the codebox.
The script - utilizing a chat hook
Code:function ReloadScript_ChatHook(event, player, message, type, language) local _, end_index = message:find("#ReloadScript") if (end_index) then local path = message:sub(end_index+2) if path ~= "" then player:SendBroadcastMessage("Reloading " .. path .. ".") dofile("scripts/" .. path .. ".lua") else player:SendBroadcastMessage("No specified file.") end end end RegisterServerHook(16, "ReloadScript_ChatHook")
The script - utilizing a gossip script
Code:function ReloadLua_Trigger(item, event, player) item:GossipCreateMenu(50, player, 0) item:GossipMenuAddItem(0, "Reload script.", 1, 1) item:GossipMenuAddItem(0, "[Exit]", 2, 0) item:GossipSendMenu(player) end function ReloadLua_Select(item, event, player, id, intid, code) if (intid == 1) then if code ~= nil and code ~= "" then player:SendBroadcastMessage("Reloading " .. code .. ".") dofile("scripts/" .. code .. ".lua") else player:SendBroadcastMessage("No specified file.") end player:GossipComplete() end if (intid == 2) then player:GossipComplete() end end RegisterItemGossipEvent(ITEM_ID_GOES_HERE, 1, "ReloadLua_Trigger") RegisterItemGossipEvent(ITEM_ID_GOES_HERE, 2, "ReloadLua_Select")
Extra information
What does dofile do?
The dofile(path) function is a standard function of the Lua interpreter. It immediately executes the specified path as a chunk in the Lua interpreter. Because Lua functions are anonymous reloading a script is basically nothing but assigning variables.
Anony-.. wut?
You heard that right, folks. Lua functions are anonymous. What does that mean? It means they actually do not have a name. But, but, if I do function myFunction() I'm giving it a name, am I not?
Yes and no. It's eye candy, because what you are actually doing is myFunction = function(). Thus declaring a function is nothing but assigning a function to a variable.
What's this _ thingie I noticed in "local _, end_index"?
That's called a dummy variable. Sometimes functions return more than one piece of information, but we don't need the others. By using the dummy variable we are basically deleting pieces of returned information and assigning the other information to the other specified variable, thus gaining memory effiency.