# Cases and examples

1:

Game start, spawn monsters near character every 10 seconds. Monster despawn in 10 seconds. Player defeat 10 Boarman 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)

2:

After game start, generate 1 stone block on the right side of player With each step

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)


3:

Place down light and switch in scene, when player got 10 dirt block, switch on the light



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)

Last Update: 4/15/2020, 6:29:50 PM