ArcEmu: [lua] How To Script As Modules - 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] How To Script As Modules Halls of Stone

#21 User is offline   paroxysm 

  • Chatty Cathy
  • Group: Retired
  • Posts: 320
  • Joined: 25-June 08
  • Gender:Male
  • Server OS:Windows

Posted 25 March 2010 - 09:49 PM

The extension doesn't have to be script, I chose script because it seemed to be an appropriately identify the file type.
Since we used package.seeall in our module function, the script files have global variable visibility so we can use all the functions.
0

#22 User is offline   HalestormXV 

  • The Ultimatum
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,031
  • Joined: 07-June 08
  • Gender:Male

Posted 25 March 2010 - 09:57 PM

View Postparoxysm, on 25 March 2010 - 09:49 PM, said:

The extension doesn't have to be script, I chose script because it seemed to be an appropriately identify the file type.
Since we used package.seeall in our module function, the script files have global variable visibility so we can use all the functions.


Thanks parox, I know i have probably been a pain asking all these questions (but that is what happens when i see new stuff) lol but I think you answered everything I had to ask lol so I appreciate it. So I really thing I am all set with this now.
**Owner of the Serenade of Sorrow Funserver**
My Most Recent Video: The Book of Kidou
Posted Image
0

#23 User is offline   paroxysm 

  • Chatty Cathy
  • Group: Retired
  • Posts: 320
  • Joined: 25-June 08
  • Gender:Male
  • Server OS:Windows

Posted 26 March 2010 - 06:40 PM

I want other scripters to change how they write their scripts to this form since it's more encapsulating, which is why I altered the RegisterEvent functions to take a direct Lua function. So they can pass in functions declared in modules even if the modules aren't global.
0

#24 User is offline   Neglected 

  • Member
  • Pip
  • Group: Members
  • Posts: 25
  • Joined: 27-May 09
  • Gender:Male

Posted 27 March 2010 - 11:19 AM

I looked through your script (after getting your reply again) and decided to do what I usually do when I'm stuck with this sort of thing; go through it, line by line, and comment it so I can get a clearer picture.

As such, would the following be basically the gist of this?
Regards.


module("TEST", package.seeall); 
-- We first create the module (_NAME = "TEST") and we can access all global functions.

local mod = require("TEST"); 
-- Now we make mod assume the value of TEST (the table we created).

assert(type(mod) == "table", "[Star++]: Could not find module, loading terminated."); 
-- We force Lua to check if mod is a table, if it isn't then we stop loading the script.

module(mod._NAME..".SUBTABLE", package.seeall);
-- We now create a new submodule within mod._NAME
-- This creates a new entry in mod._NAME.
-- mod._NAME usually holds the name of the module, in this case, 'TEST'.
-- Therefore, when we use module(mod._NAME..".TEST", package.seeall);, we're essentially doing this:
-- TEST.SUBTABLE
-- TEST.SUBTABLE is a new KEY, which is assigned the value of 
-- TEST.SUBTABLE = {_NAME = "SUBTABLE"};
-- The difference between doing this and just defining a table though, is that this sets the environment to TEST.SUBTABLE too.

mod = getfenv(1);
-- We define mod as the global environment. 

local info = debug.getinfo(1);
-- Info is defined as the stack level #1. (I clearly have no idea what stacks are)

local src = string.reverse(info.short_src);
-- Turns info.short_src the other way around. info.short_src is a short version of where the function was defined.

local file_sep;
if GetPlatform() == "Win32" then
	file_sep = "\\";
else
	file_sep = "/";
end
-- Here we determine the file seperator (ie, script\\test or script/test)

-- Now we resolve the file path of src..
local startpos, endpos = string.find(src, file_sep), string.len(src);
-- Startpos = the first / we see in src, whereas endpos is the length of src.
-- Henceforth, if src = tset/detcelgen/tpircs/, then we'd get 5 and 22.

local directory = string.reverse(string.sub(src, startpos, endpos));
-- Now get the string of src, from position 5 and 22; detcelgen/tpircs/.
-- We then flip this back around, /script/neglected

-- We now make our modules based on the resolved directory (/script/neglected
local modules = {directory.."test.script", directory.."test2.script"};
-- This makes two strings in the table modules; /script/neglected/test.script and /script/neglected/test2.script
for _, v in pairs(modules) do
	local loaderfunc, errormsg = loadfile(v);
	-- Here we load the file, and loadfile will return an error CODE (not a message) should one be needed
	if loaderfunc == nil then
		print(errormsg);
		-- If we couldn't load the file, then we print the error code (automatically turned into a message thanks to Lua)
	else
		setfenv(loaderfunc, mod);
		-- We now change loaderfunc (That's the file we just loaded)'s environment to mod (getfenv(1)).
		local ret, errormsg = pcall(loaderfunc);
		-- pcall is a Protected call. It returns 'true' if there are no errors, but returns an error code (like loadfile) if there is an error.
		-- It uses loadfile to load the file.
		if ret == false then
			print(errormsg);
			-- Ret returned false (error) and so it prints the error message.
		else
			print(string.format("Successfully loaded script file: \"%s\"", v));
			-- Prints 'Successfully loaded script file: filename, where filename is the value of modules.
		end
	end
end

-- actual scripts
local mod = getfenv(1);
-- again, we assign mod the value of our global environment.
assert(type(mod) == "table", "[Star++]: Could not find module, loading terminated."); 
-- We force Lua to check if mod is a table, if it isn't then we stop loading the script.
module(mod._NAME..".TEST", package.seeall)
-- We create another module inside our global environment (STAR.TEST) that can use global functions and is named TEST (another subtable, STAR.TEST.TEST)
local self = getfenv(1)
-- We then assign self the value of STAR.TEST.TEST, enabling us to use self.

Lua Wiki Editor.
Ahn'Qiraj PvP EU - Hitswitch [Rogue]; Aeire [Priest]; Neglected [Warlock]
0

#25 User is offline   hypersniper 

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

Posted 01 April 2010 - 08:56 PM

I started using this method for a dungeon, I have to say I very much like its format, I like the idea of plugging in features as you make them. It's a shame you can't get the contents of a directory using Lua (without extensions), because otherwise you could make a sub-directory for say Halls of Stone and the main script just loadfile's based on that sub-directory.

Speaking of the directory, Windows can use '/' as can Linux and I believe Mac, and there's no real need to resolve the directory, the Lua scripts are loaded relative to arcemu-world.exe's location, and so that's where directory commands are relative to within the scripts.

Hence, I was able to replace:
local info = debug.getinfo(1)
local src = string.reverse(info.short_src)
local file_sep;
if(GetPlatform() == "Win32") then
        file_sep = "\\"
else
        file_sep = '/'
end
-- Directory resolving algo
local startpos,endpos = string.find(src,file_sep),string.len(src)
local directory = string.reverse(string.sub(src,startpos,endpos))
print("Resolved directory is :"..directory)

With:
local directory = "scripts/"

Which is a bit clearer. I see that the original way is how it's done in the core, but Lua's loadfile needs not as much complexity :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

#26 User is offline   paroxysm 

  • Chatty Cathy
  • Group: Retired
  • Posts: 320
  • Joined: 25-June 08
  • Gender:Male
  • Server OS:Windows

Posted 01 April 2010 - 09:04 PM

In this case, the script files were meant to be located in properly named folder such as scripts/ULDUAR/HALLS_OF_STONE/
The reason for directory resolving is to allow the script files root directory to be relocated/renamed and still have them load correctly. Hard coded file path is always a bad idea.
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