• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Quickstart Guide to Installing Lua Scripts (easy)

Level 31
Joined
Jul 10, 2007
Messages
6,306
The Quick Start Guide To Installing Lua to a map.

First make sure you have NewGen installed (Setting up NewGen WE (with pictures))


Step 1 (installating Lua framework, necessary for all maps)

Copy and paste this into the map (must be done for any map wanting to use Lua).
JASS:
		//! externalblock extension=lua FileExporter $FILENAME$
			//! i local FILENAME = "FILE_NAME"

			//! runtextmacro LUA_FILE_HEADER()
			//! i initmap()
		//! endexternalblock

		//! textmacro LUA_FILE_HEADER
		    //! i do
			//! i local PATH_LUA_p = "grimext\\luadir"
			//! i local PATH_JASS_p = PATH_LUA_p .. "\\" .. FILENAME .. "_dir"
			
			//! i local PATH_LUA = PATH_LUA_p .. "\\"
			//! i local PATH_JASS = PATH_JASS_p .. "\\"
			//! i local JASS_HUB = "jass\\luajass." .. FILENAME .. ".j"
			//! i function initmap()
			    //! i os.execute("if not exist " .. PATH_LUA .. " (mkdir " .. PATH_LUA .. ")")
			    //! i os.execute("if not exist " .. PATH_JASS .. " (mkdir " .. PATH_JASS .. ")")
			    //! i local file = io.open(JASS_HUB, "r")
			    //! i if (file == nil) then
				//! i file = io.open(JASS_HUB, "w")
				//! i file:write("")
				//! i file:close()
			    //! i else
				//! i file:close()
			    //! i end
			    
			    //! i os.execute("if not exist grimext\\luadir\\" .. FILENAME .. "_dir (mkdir grimext\\luadir\\" .. FILENAME .. "_dir)")
			//! i end
		    //! i end
		//! endtextmacro

Change FILE_NAME in "FILE_NAME" (line 2) to the name of your map.

Example

//! i local FILENAME = "FILE_NAME"
to
//! i local FILENAME = "MY_RANDOM_MAP"

The name can't contain spaces or special characters.

Once done, hit save.
Step 2 (finishing installation of Lua framework)

Delete the code and then copy and paste this into the map.
JASS:
		//! import "luajass.FILE_NAME.j"
		//! textmacro LUA_FILE_HEADER
		    //! i do
			//! i local FILENAME = "FILE_NAME"
			//! i function getfilename()
			    //! i return FILENAME
			//! i end
			//! i local PATH_LUA_p = "grimext\\luadir"
			//! i local PATH_JASS_p = PATH_LUA_p .. "\\" .. FILENAME .. "_dir"
			//! i local PATH_LUA = PATH_LUA_p .. "\\"
			//! i local PATH_JASS = PATH_JASS_p .. "\\"
			//! i local JASS_HUB = "jass\\luajass." .. FILENAME .. ".j"
			//! i local olddofile = dofile
			//! i local oldrequire = require
			//! i local oldloadfile = loadfile
			//! i function dofile(name)
			    //! i oldrequire("luadir\\" .. name)
			//! i end
			//! i function require(name)
			    //! i dofile(name)
			//! i end
			//! i function loadfile(name)
			    //! i dofile(name)
			//! i end
			//! i local function getluapath(name)
			    //! i return (PATH_LUA .. name .. ".lua")
			//! i end
			//! i local function getjasspath(name)
			    //! i return (PATH_JASS .. name .. ".luajass.j")
			//! i end
			//! i local function getjassimport(name)
			    //! i return ("\/\/! import \"..\\" .. getjasspath(name) .. "\"")
			//! i end
			//! i local function del(name)
			    //! i os.remove(name)
			//! i end
			//! i local function read(path)
			    //! i local file = io.open(path, "r")
			    //! i code = nil
			    //! i if (file ~= nil) then
				//! i code = file:read("*all")
				//! i file:close()
			    //! i end
			    //! i return code
			//! i end
			//! i local function write(path, code)
			    //! i file = io.open(path, "w")
			    //! i file:write(code)
			    //! i file:close()
			//! i end
			//! i local function import(name)
			    //! i local code = read(JASS_HUB)
			    //! i local line = getjassimport(name) .. "\n"
			    //! i local s,k = code:find(line)
			    //! i if (s == nil) then
				//! i write(JASS_HUB, code .. line)
			    //! i end
			//! i end
			//! i function readlua(name)
			    //! i return read(getluapath(name))
			//! i end
			//! i function writelua(name, code)
			    //! i write(getluapath(name), code)
			//! i end
			//! i function readjass(name)
			    //! i return read(getjasspath(name))
			//! i end
			//! i function writejass(name, code)
			    //! i write(getjasspath(name), code)
			    //! i import(name)
			//! i end
			//! i function deletelua(name)
			    //! i del(getluapath(name))
			//! i end
			//! i function deletejass(name)
			    //! i del(getjasspath(name))
			    //! i local line = getjassimport(name) .. "\n"
			    //! i local code = read(JASS_HUB)
			    //! i local s,k = code:find(line)
			    //! i if (s ~= nil) then
				//! i write(JASS_HUB, code:sub(1,s-1) .. code:sub(k+1))
			    //! i end
			//! i end
		    //! i end
		//! endtextmacro

Find this line
//! import "luajass.FILE_NAME.j"

And change the FILE_NAME portion to the name you decided for your map.

Example

//! import "luajass.FILE_NAME.j"
to
//! import "luajass.MY_RANDOM_MAP.j"

Find this line
//! i local FILENAME = "FILE_NAME"

And change FILE_NAME to what you decided for your map.

Example

//! i local FILENAME = "FILE_NAME"
to
//! i local FILENAME = "MY_RANDOM_MAP"

Then hit save.
Step 3 (installing recommended Lua systems)

Lua systems only need to be installed on a given computer one time. Once it is installed, it will work across all maps.

Copy and paste this script into the map
JASS:
		//! externalblock extension=lua FileExporter $FILENAME$
		    //! runtextmacro LUA_FILE_HEADER()
		    //! i writelua("GetObjectId", [[
		    //! i function getobjectid(obj, objecttype)
			//! i if (currentobjecttype() ~= objecttype) then
			    //! i setobjecttype(objecttype)
			//! i end
			//! i local object = generateid(obj)
			//! i while (
			    //! i objectexists(object) or
			    //! i string.find(object, "'", 1, true) ~= nil or
			    //! i string.find(object, '\\', 1, true) ~= nil or
			    //! i string.find(object, ',', 1, true) ~= nil or
			    //! i string.find(object, '/', 1, true) ~= nil) do
			    //! i object = generateid(obj)
			//! i end
			//! i return object
		    //! i end
		    //! i ]])
		//! endexternalblock

Hit save. Then delete the script. Now the above script will work in any map that has the Lua framework installed in it.

Copy and paste this script into the map
JASS:
		//! externalblock extension=lua FileExporter $FILENAME$
		    //! runtextmacro LUA_FILE_HEADER()
		    //! i writelua("GetVarObject", [[
		    //! i local filename = "JassGlobals"
		    //! i local filename_lua = getfilename() .. "_VAR_OBJECT_JassGlobals1"
		    //! i dofile("GetObjectId")
		    //! i local vars = readlua(filename_lua)
		    //! i local vars2 = readjass(filename)
		    //! i local varsdata
		    //! i local newvars = ""
		    //! i if (vars == nil) then
			//! i vars = {}
			//! i vars2 = ""
			//! i varsdata = ""
		    //! i else
			//! i if (vars ~= "return {}") then
			    //! i varsdata = vars:sub(9,vars:len()-1)
			    //! i vars = loadstring(vars)()
			//! i else
			    //! i varsdata = ""
			    //! i vars = {}
			//! i end
			//! i if (vars2 == nil) then
			    //! i vars2 = ""
			//! i else
			    //! i vars2 = vars2:sub(string.len("globals")+1, vars2:len()-string.len("\nendglobals"))
			//! i end
		    //! i end
		    //! i local imports = {}
		    //! i do
			//! i local s,k = vars2:find("constant integer ")
			//! i local s2,k2
			//! i while (s ~= nil) do
			    //! i s2,k2 = vars2:find("=", k)
			    //! i imports[vars2:sub(k+1, s2-1)] = true
			    //! i s,k = vars2:find("constant integer ", k2)
			//! i end
		    //! i end
		    //! i function getvarobject(base, objtype, varname, import)
			//! i local value = vars[varname]
			//! i local imported
			//! i if (import == nil) then
			    //! i import = false
			//! i end
			//! i if (value == nil) then
			    //! i imported = false
			    //! i value = getobjectid(base, objtype)
			    //! i while (vars["1" .. value] ~= nil) do
				//! i value = getobjectid(base, objtype)
			    //! i end
			    //! i vars[varname] = value
			    //! i vars["1" .. value] = varname
			    //! i if (newvars == "") then
				//! i newvars = "['" .. varname .. "']='" .. vars[varname] .. "',['1" .. value .. "']='" .. varname .. "'"
			    //! i else
				//! i newvars = newvars .. ",['" .. varname .. "']='" .. vars[varname] .. "',['1" .. value .. "']='" .. varname .. "'"
			    //! i end
			//! i else
			    //! i imported = imports[varname] or false
			    //! i if (currentobjecttype() ~= objtype) then
				//! i setobjecttype(objtype)
			    //! i end
			//! i end
			//! i if (import ~= imported) then
			    //! i if (not imported) then
				//! i vars2 = vars2 .. "\nconstant integer " .. varname .. "='" .. value .. "'"
			    //! i elseif (imported) then
				//! i local s,k = string.find(vars2, "\nconstant integer " .. varname .. "='" .. value .. "'")
				//! i vars2 = vars2:sub(1,s-1) .. vars2:sub(k+1, vars2:len())
			    //! i end
			    //! i imports[varname] = import
			//! i end
			//! i return value
		    //! i end
		    //! i function getvarobjectname(value)
			//! i return vars["1" .. value]
		    //! i end
		    //! i function getvarobjectvalue(objectname)
			//! i return vars[objectname]
		    //! i end
		    //! i function updateobjects()
			//! i writejass(filename, "globals" .. vars2 .. "\nendglobals")
			//! i if (varsdata == "") then
			    //! i varsdata = newvars
			//! i elseif (newvars ~= "") then
			    //! i varsdata = varsdata .. "," .. newvars
			//! i end
			//! i newvars = ""
			//! i writelua(filename_lua, "return {" .. varsdata .. "}")
		    //! i end
		    //! i ]])
		//! endexternalblock

Hit save. Then delete the script. Now the above script will work in any map that has the Lua framework installed in it.

Copy and paste this script into the map
JASS:
		//! externalblock extension=lua FileExporter $FILENAME$
			//! runtextmacro LUA_FILE_HEADER()
			//! i writelua("DummyPhysicalAbility", [[
			//! i dofile("GetVarObject")
			//! i function getdummyphysicalability(name, levels, import)
				//! i local buffs = {}
				//! i local buffcount = 0
				//! i do
					//! i local cur = levels
					//! i local curstr
					//! i while (cur > 0) do
						//! i curstr = tostring(cur)
						//! i buffs[cur] = getvarobject("BNva", "buffs", "BUFFS_" .. name .. curstr .. "_DUMMY", false)
						//! i createobject("BNva", buffs[cur])
						//! i makechange(current, "fnam", name .. "_DUMMY" .. curstr)
						//! i cur = cur - 1
					//! i end
				//! i end
				//! i local ability = getvarobject("AIob", "abilities", "ABILITIES_" .. name .. "_DUMMY", import)
				//! i createobject("AIob", ability)
				//! i makechange(current, "anam", name .. "_DUMMY")
				//! i makechange(current, "amat", "")
				//! i makechange(current, "asat", "")
				//! i makechange(current, "aspt", "")
				//! i makechange(current, "atat", "")
				//! i makechange(current, "ata0", "")
				//! i makechange(current, "alev", tostring(levels))
				//! i makechange(current, "Idam", "1", "0")
				//! i makechange(current, "ahdu", "1", "0")
				//! i makechange(current, "adur", "1", "0")
				//! i makechange(current, "atar", "1", "")
				//! i do
					//! i local cur = levels
					//! i local curstr
					//! i while (cur > 0) do
						//! i curstr = tostring(cur)
						//! i makechange(current, "abuf", curstr, buffs[cur])
						//! i makechange(current, "Iob5", curstr, "2")
						//! i cur = cur - 1
					//! i end
				//! i end
				//! i local ability2 = getvarobject("Afrb", "abilities", "ABILITIES_" .. name .. "_DUMMY_2", import)
				//! i createobject("Afrb", ability2)
				//! i makechange(current, "anam", name .. "_DUMMY_2")
				//! i makechange(current, "amat", "")
				//! i makechange(current, "amho", "1")
				//! i makechange(current, "achd", "0")
				//! i makechange(current, "alev", tostring(levels))
				//! i makechange(current, "atar", "1", "")
				//! i makechange(current, "ahdu", "1", "0")
				//! i makechange(current, "adur", "1", "0")
				//! i do
					//! i local cur = levels
					//! i local curstr
					//! i while (cur > 0) do
						//! i curstr = tostring(cur)
						//! i makechange(current, "abuf", curstr, buffs[cur])
						//! i cur = cur - 1
					//! i end
				//! i end
				//! i return {buffs = buffs, ability = ability, ability2 = ability2}
			//! i end
			//! i ]])
		//! endexternalblock

Hit save. Then delete the script. Now the above script will work in any map that has the Lua framework installed in it.
Step 4 (installing popular vjass scripts)

A vjass script that has an installation script like the one for UnitIndexer
JASS:
		//! externalblock extension=lua ObjectMerger $FILENAME$
			//! runtextmacro LUA_FILE_HEADER()
			//! i dofile("GetVarObject")
			//! i local id = getvarobject("Adef", "abilities", "ABILITIES_UNIT_INDEXER", true)
			//! i createobject("Adef", id)
			//! i makechange(current, "anam", "Unit Indexing")
			//! i makechange(current, "ansf", "(Unit Indexing)")
			//! i makechange(current, "aart", "")
			//! i makechange(current, "arac", "0")
			//! i updateobjects()
		//! endexternalblock

Must be installed into each map that wants to use that vjass system.

  • Copy and paste the Lua installation script into the map (like the one shown above)
  • Hit save
  • Close the map
  • Open the map
  • Delete the script
  • Copy and paste the vJASS script into the map (the actual system)
  • Hit save

All vjass installation scripts that generate objects, like the one for UnitIndexer, are installed exactly the same way.

Example (Revive Unit)-

Copy and paste this script into the map (Lua installation script)
JASS:
		//! externalblock extension=lua ObjectMerger $FILENAME$
			//! runtextmacro LUA_FILE_HEADER()
			//! i dofile("GetVarObject")
			//! i local rez = getvarobject("AHre", "abilities", "ABILITIES_REVIVE_UNIT_RESURRECTION", true)
			//! i createobject("AHre",rez)
			//! i makechange(current,"anam","DumResurrection")
			//! i makechange(current,"aher","0")
			//! i makechange(current,"acat","")
			//! i makechange(current,"atat","")
			//! i makechange(current,"Hre1","1","1")
			//! i makechange(current,"aare","1","0")
			//! i makechange(current,"aran","1","0")
			//! i makechange(current,"acdn","1","0")
			//! i makechange(current,"amcs","1","0")
			//! i makechange(current,"atar","1","Air,Dead,Enemy,Friend,Ground,Neutral")
			//! i local dummy = getvarobject("ushd", "units", "UNITS_REVIVE_UNIT_DUMMY", true)
			//! i createobject("ushd",dummy)
			//! i makechange(current,"unam","Dummy")
			//! i makechange(current,"uabi",rez .. ",Aloc,Avul")
			//! i makechange(current,"ucbs","0")
			//! i makechange(current,"ucpt","0")
			//! i makechange(current,"umdl","none.mdl")
			//! i makechange(current,"usca","0.01")
			//! i makechange(current,"ushu","None")
			//! i makechange(current,"umvh","0")
			//! i makechange(current,"umvs","0")
			//! i makechange(current,"ufoo","0")
			//! i makechange(current,"umpi","100000")
			//! i makechange(current,"umpm","100000")
			//! i makechange(current,"umpr","1000")
			//! i updateobjects()
		//! endexternalblock

Hit save. Close the map. Open the map. Delete the script.

Copy and paste this script into the map (the actual vJASS script)
JASS:
		library ReviveUnit
			globals
				private unit reviver 
				private real rx
				private real ry
			endglobals
			private module ReviveInitialization
				private static method onInit takes nothing returns nothing
					set rx = GetRectMaxX(bj_mapInitialPlayableArea)-1
					set ry = GetRectMaxY(bj_mapInitialPlayableArea)-1
					set reviver = CreateUnit(Player(15),UNITS_REVIVE_UNIT_DUMMY,rx,ry,0)
				endmethod
			endmodule
			struct Revive extends array
				static method Unit takes unit whichUnit returns boolean
					local real x = GetUnitX(whichUnit)
					local real y = GetUnitY(whichUnit)
					local boolean bb = true
					if IsUnitType(whichUnit,UNIT_TYPE_HERO) == true then
						call ReviveHero(whichUnit,x,y,false)
					else
						call SetUnitX(whichUnit,rx)
						call SetUnitY(whichUnit,ry)
						set bb = IssueImmediateOrderById(reviver,852094)
						call SetUnitX(whichUnit,x)
						call SetUnitY(whichUnit,y)
					endif
					return bb
				endmethod
				implement ReviveInitialization
			endstruct
		endlibrary

Hit save. ReviveUnit is now ready to be used.

Try this code to see that it can be used (cnp into map, hit save, then hit test map)-
JASS:
		struct TestReviveUnit extends array
			private static real x
			private static real y
			private static rect world
			private static unit u
			private static method r3 takes nothing returns nothing
				call DestroyTimer(GetExpiredTimer())
				call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Reviving Peasant")
				call Revive.Unit(u)
			endmethod
			private static method r2 takes nothing returns nothing
				call TimerStart(GetExpiredTimer(), 2, false, function thistype.r3)
				call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Killing Peasant")
				call KillUnit(u)
			endmethod
			private static method r takes nothing returns nothing
				local fogmodifier fo = CreateFogModifierRect(Player(0), FOG_OF_WAR_VISIBLE, world, true, true)
				call FogModifierStart(fo)
				call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Creating Peasant")
				set u = CreateUnit(Player(0), 'hpea', x, y, 0)
				call PanCameraToTimed(x,y,0)
				call TimerStart(GetExpiredTimer(), 2, false, function thistype.r2)
				set fo = null
			endmethod
			private static method onInit takes nothing returns nothing
				set world = GetWorldBounds()
				set x = (GetRectMaxX(world)+GetRectMinX(world))/2
				set y = (GetRectMaxY(world)+GetRectMinY(world))/2
				call TimerStart(CreateTimer(), 0, false, function thistype.r)
			endmethod
		endstruct
 
Last edited:
Top