Script

**Scripts** are Developer Tools that are made to execute multiple actions. They are made using a modified version of Lua.

Scripts can be inside Script Sets, and there is a maximum of 100 scripts that can be created in a world.

## Defaults

Creating a script will create the following script text by default:

```
-- description: script code
--{{ Dear Adventurer: Release illegal information, social software and sharing scam information will lead to banning. }}

--{{{ 游戏开始，执行一次 <game start> run once!
--### template : 初始化 init ###
--[==[ 定义lua变量,执行开发者API等
local msg = 'hello, mini world!'
Game:msgBox(msg)
--]==]

--### template : 注册触发器 register tigger ###
--[==[ 使用表情动作，弹消息[msg]
local function func\_event(param)
 local playerid = CurEventParam.TriggerByPlayer
 if playerid then
 local ret, name = Player:getNickname(playerid)
 if ret == ErrorCode.OK then
 Chat:sendSystemMsg('[' .. name .. '] say mini')
 end
 end
end
ScriptSupportEvent:registerEvent('Player.PlayAction', func\_event)
--]==]
--}}}

--{{{ 游戏时钟，每时钟(50ms)执行一次 <game tick> run once per tick!
--[==[
local bloodtick = 0
return function()
 -- 房主每秒回复1点血
 bloodtick = bloodtick + 1
 if bloodtick >= 20 then
 bloodtick = 0

 local playerid = 0 -- 房主
 local ret, hp = Player:getAttr(playerid, PLAYERATTR.CUR\_HP)
 if ret == ErrorCode.OK then
 hp = hp + 1
 Player:setAttr(playerid, PLAYERATTR.CUR\_HP, hp)
 end
 end
end
--]==]
--}}}

```
This code can be deleted and replaced with your own code.

A new script set will be called "Script Set X", while X is the number of script sets currently in the world before, plus one.

## Examples

*Note: We only list three examples in this page. More examples can be found here.*

### Example 1

When the game starts, a monster will be spawned near the player every 10 seconds and will despawn in 10 seconds. Player must kill 10 Boarmen to win.

```
local ActorId = 3102
local obj\_ids = {} -- monster ID list
local def\_count = 0 --Times of defeat
local mob\_count = 5 --number of monsters
local function SpawnMonsters()
	local ret, x, y, z = Player:getPosition()
	for idx = 1, mob\_count do --generated monster ID
		local newX = math.random(-5, 5) + x
		local newZ = math.random(-5, 5) + z
		local ret, objids = World:spawnCreature(newX, y, newZ, ActorId)
		if ret == ErrorCode.OK then
			obj\_ids[#obj\_ids+1] = objids[1]
			obj\_ids[#obj\_ids+1] = objids[2]
		end
	end
end
local function ClearMonsters()
	if #obj\_ids == 0 then return end
	for idx = 1, #obj\_ids do
		World:despawnCreature(obj\_ids[idx])
	end
	obj\_ids = {}--clear out ID list
end

Game\_Start = function()
	local ret, timerId = MiniTimer:createTimer("TimerX")
	if ret == ErrorCode.OK then --Activate Timer
		MiniTimer:startForwardTimer(timerId)
		SpawnMonsters()
	end
end

Timer\_Change = function(event\_args)
	local timerid = event\_args['timerid']
	local ret, secs = MiniTimer:getTimerTime(timerid)
	if ret == ErrorCode.OK then
		if math.fmod(secs, 10) == 0 then
			ClearMonsters() --Refresh the monster every 10 seconds
			SpawnMonsters()
		end
	end
end

Defeat\_Actor = function(event\_args)
	local toobjid = event\_args['toobjid'] --ID of defeated Target
	local playerId = event\_args['eventobjid']--Player ID
	local ret, actorId = Creature:getActorID(toobjid)
	if ret==ErrorCode.OK and actorId==ActorId then
		def\_count = def\_count + 1
		Chat:sendSystemMsg("KillMobNumber:"..def\_count)
		if def\_count == 10 then
			local win = TEAM\_RESULTS.TEAM\_RESULTS\_WIN
			local result = Player:setGameResults(playerId, win)
			if result == ErrorCode.OK then
				Game:doGameEnd() --Game Over
			end
		end
	end
end

ScriptSupportEvent:registerEvent([=[Game.Start]=], Game\_Start)
ScriptSupportEvent:registerEvent([=[minitimer.change]=], Timer\_Change)
ScriptSupportEvent:registerEvent([=[Player.DefeatActor]=], Defeat\_Actor)

```

### Example 2

When the game starts, place 1 Stone on the right side of the player for every movement.

```
Player\_MoveStep = function(event\_args)
	local playerId = event\_args['eventobjid']
	local ret, x, y, z = Player:getPosition()

	local ret, curdir = Player:getCurPlaceDir(playerId)
	if curdir == FACE\_DIRECTION.DIR\_NEG\_X then x = x+2; z = z+2 end
	if curdir == FACE\_DIRECTION.DIR\_POS\_X then x = x-2; z = z-2 end
	if curdir == FACE\_DIRECTION.DIR\_NEG\_Z then z = z+2; x = x+2 end
	if curdir == FACE\_DIRECTION.DIR\_POS\_Z then z = z-2; x = x-2 end
	Block:replaceBlock(104, x, y, z)
end
ScriptSupportEvent:registerEvent([=[Player.MoveOneBlockSize]=], Player\_MoveStep)

```

### Example 3

The game will place a light and switch, and when the player has broken 10 Grass Blocks, the light will turn on.

```
local x, y, z = 5, 7, 5 --Coordinates of light
Game\_Start = function()
	-- Place down light and switch
	local palyer\_1 = Player:getHostUin()
	Player:setPosition(palyer\_1, 0, 7, 0)
	local switchId, lightId = 724, 707
	Block:replaceBlock(lightId, x, y, z)
	Block:replaceBlock(switchId, x, y+1, z, 4)
	for idx= 1, 10 do -- Place down grass block
		Block:replaceBlock(100, x+idx, y, z)
	end
end

local remove\_count = 0
Block\_Removed = function(event\_args)
	local blockid = event\_args['blockid']
	if blockid == 100 then --Grass block is broken
		remove\_count = remove\_count + 1
		if remove\_count == 10 then --Activate switch
			Block:setBlockSwitchStatus({x=x,y=y+1,z=z}, true)
		end
	end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game\_Start)
ScriptSupportEvent:registerEvent([=[Block.Remove]=], Block\_Removed)

```

## Trivia

* Scripts may be used to replace triggers, as multiple triggers can be programmed into one script.