Macros

Description

Macros are persistent Lua functions being called as long as the player is logged in. The persistence of the Lua function is determined by the timeout specified by the user. All Parameters are not required in order to create a macro.

Prototypes
macro(timeout, callback)
macro(timeout, name, callback)
macro(timeout, name, callback, parent)
macro(timeout, name, hotkey, callback)
macro(timeout, name, hotkey, callback, parent)
Parameters
    • timeout: the time in milliseconds after which the function is called.
    • name: macro name on the bot interface. Macro with name will keep it's status (on/off) after login/logout/restart. If the macro has no name it won't show up on the bot interface
    • hotkey: keyboard hotkey to disable/enable the macro. can be nil or empty string
    • parent: the tab to show the on/off macro button on. Default tab is main
    • callback: is the Lua function being called called.
      • the function can have macro object parameter function(macro) to toggle macro state.
Return

Macro return table with following functions:

    • isOn() - return true if macro is enabled
    • isOff() - return true if macro is disabled
    • setOn() - enables macro
    • setOff() - disables macro
    • toggle() - enables or disables macro
Examples
macro(timeout, callback)

Macro with no button on the bot interface.

macro(10000, function() -- run every 10 seconds
    turn(0) -- turn the character direction to north.
  end)

macro(timeout, name, callback)

Macro with a on/off button on bot main Tab.

onoff-macro.png

macro(1000, "exchange money", function()
    for i, container in pairs(getContainers()) do
      for j, item in ipairs(container:getItems()) do
        if item:isStackable() and (item:getId() == 3035 or item:getId() == 3031) and item:getCount() == 100 then
          return use(item)
        end
      end
    end
  end)

macro(timeout, name, callback, parent)

Macro with a on/off button on a tab

macro-on-tab.png

local warTab = addTab("War") -- Add new tab called War

local oldTarget
macro(500, "Hold Target", function()

    if g_game.isAttacking() then
      oldTarget = g_game.getAttackingCreature() -- save the target
    end
    if oldTarget and not g_game.isAttacking() and getDistanceBetween(pos(), oldTarget:getPosition()) <= 8 then -- if the player has no target and player is on distance
      g_game.attack(oldTarget)
    end

  end, warTab)
 
macro(timeout, name, hotkey, callback)

Macro with on/off keyboard hotkey on Main tab

hotkeymacro.png

macro(100, "hide useless tiles", "Ctrl+H", function()
    for i, tile in ipairs(g_map.getTiles(posz())) do
      if not tile:isWalkable(true) then
        tile:setFill('black')
      end
    end
  end)


macro(timeout, name, hotkey, callback, parent)

Macro with a keyboard hotkey on a tab

hotkeymacroontab.png

local warTab = addTab("War") -- Add new tab called War

macro(250, "Dance", "Ctrl+D", function()

    turn(math.random(0, 3)) -- turn to a random direction.

 end, warTab)
Toggling macro state

This feature is available from version 1.8, it adds ability to disable/enable the macro from the script.

macro(1000, "refill arrows", function(macro)
	if getAmmo() == nil then
		local arrows = findItem(3447) -- find arrows
		if arrows then
			moveToSlot(arrows, SlotAmmo, arrows:getCount()) -- the arrows was found move them to ammo slot
			return -- arrows was found and moved end the cycle.
		end
		-- if the script reach this point the arrows wasn't found on any of player backpacks.
		macro.setOff() -- disable the macro since we didn't find any arrows
	end
end)

available toggling functions is setOff()setOn()toggle()

you also assign the macro to a variable and change it's state

local refilArrows = macro(1000, "refill arrows", function(macro)
	if getAmmo() == nil then
---.......
      
-- using a hotkey for example to toggle between refilling arrow state
hotkey("f5", "toggle refilling", function()
	refilArrows.toggle() -- you can also use setOn(), setOff()
	info("refilling was disabled")
end)