# Basic Case Studies

Tip: in lua syntax, the "--" indicates annotations, all content in a line of "--" will not execute.

lua syntax :

To create functions:【Click to know more basic lua syntax

  • Function names are created freely, for example:a(),b(),miniworld()
  • Must use function to create.
  • How to create function:
    • a = function() end
    • meaning a function named a is created
  • How to write the content of a function:
abc = function() --create a function named abc
	--write the function content here
end
  • How to execute functions:
    • There are many content inside functions, how to execute them?:
abc = function() --create a function named abc
	--write the function content here
end
abc()  --To execute, write call function in separate line

Example 1:

Place 3 dirt in front of you, replace with stone block if dirt block is broken

local face = FACE_DIRECTION.DIR_POS_Y
Game_Start = function()
	local ret1, playerId = Player:getMainPlayerUin()
	--Place down blocks after game start to obtain player coordinates
	local ret2, x, y, z = Player:getPosition(playerId)
	Block:replaceBlock(101, x+1, y, z, face)
	Block:replaceBlock(101, x, y, z+1, face)
	Block:replaceBlock(101, x-1, y, z-1, face)
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)
--Game.Start event means the game is started, therefore execute Game_Start in the beginning

Block_Removed = function(event_args)
	local xPos = event_args['x']
	local yPos = event_args['y']
	local zPos = event_args['z']
	local blockid = event_args['blockid']
	if blockid == 101 then --the broken blocks are dirt blocks
		Block:replaceBlock(104, xPos, yPos, zPos, face)
	end
end
ScriptSupportEvent:registerEvent([=[Block.Remove]=], Block_Removed)

Example 2:

Output to all players: hello, mini world

Game_Start = function()
	Chat:sendChat("hello, mini world") --White font with player name
	Chat:sendSystemMsg("hello, mini world") --Orange font without player name
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)

Example 3:

How to make a mark in mini map based on current location

-- MapMark have bugs currently
local markType = MAPMARK_TYPE.MMARK_CIRCLE
local ret, markId = MapMark:newShape(markType,false,255,255,255)
if ret == ErrorCode.OK then
	local radius = 5.0
	local ret, x,y,z = Player:getPosition()
	MapMark:setShapeColor(markId, 255, 0, 0)
	MapMark:updateCircle(markId, x, z, radius)
	MapMark:showShape(markId, true)
end

Example 4:

Obtain the number of stone block/monster within area

Game_Start = function()
	local ret1, playerId = Player:getMainPlayerUin()
	local ret2, px, py, pz = Player:getPosition(playerId)
	if ret1~=ErrorCode.OK or ret2~=ErrorCode.OK then return end

	local pos = {x=px, y=py, z=pz}
	local dim = {x=10, y=3, z=10}
	local ret, areaId = Area:createAreaRect(pos, dim)
	if ret == ErrorCode.OK then
		local ret, arr = Area:getAreaCreatures(areaId)
		local result = Area:blockInArea(areaId, 104)
		local blkCont = result==ErrorCode.OK and 'contain' or 'donotcontain'
		Chat:sendSystemMsg("this area contains"..#arr.."creatures and"..blkCont.."stone blocks")
	end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)

Example 5:

Add 1 score to red team by breaking 1 dirt block

Block_Removed = function(event_args)
	local xPos = event_args['x']
	local yPos = event_args['y']
	local zPos = event_args['z']
	local blockid = event_args['blockid']

	 --Add 1 score to red team if the broken block is grass or dirt block
	if blockid==100 or blockid==101 then
		local teamid, score = 1, 1--ID for red team is 1
		local result = Team:addTeamScore(teamid, score)
	end
end
ScriptSupportEvent:registerEvent([=[Block.Remove]=], Block_Removed)

Example 6:

Detects how many dirt blocks in the character's backpack and outputs number in the chat box

Game_Start = function()
	local itemId = 101
	local ret, playerId = Player:getMainPlayerUin()
	local bp1Type = BACKPACK_TYPE.SHORTCUT	--shortcut bar
	local bp2Type = BACKPACK_TYPE.INVENTORY --inventory
	local ret1, num1, arr1 = Backpack:getItemNumByBackpackBar(playerId, bp1Type, itemId)
	local ret2, num2, arr2 = Backpack:getItemNumByBackpackBar(playerId, bp2Type, itemId)

	if ret1==ErrorCode.OK and ret2==ErrorCode.OK then
		local text = "In shortcut bar:"..num1..' In inventory'..num2
		Chat:sendSystemMsg(text)
	end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)

Example 7:

Teleport teammate in front of you by making an emoticon(requires at least 2 teams)

-- Change the number of teams on the current map to more than 1 team
Player_PlayAct = function(event_args) -- Create a function named Player_PlayAct, includes the logic for teleport teammates
	local actId = event_args['act']
	local playerId = event_args['eventobjid']
	
	if actId~=nil and actId==1 then --emoticon,“ ~=Means not equal to; == Means equal to”
		local ret, teamId = Player:getTeam(playerId)
		if ret==ErrorCode.OK and teamId>0 then
			local ret, x, y, z = Player:getPosition(playerId)
			local ret, num, arr = Team:getTeamPlayers(teamId)
			for idx = 1, num do --Teleport teammate in front of you
				if arr[idx]~=playerId then
					Player:setPosition(arr[idx], x+1, y, z+1)
				end
			end
		end
	end
end
ScriptSupportEvent:registerEvent([=[Player.PlayAction]=], Player_PlayAct)
-- Need to use Player.PlayAction from Player APIs, execute once when make emoticon

Code Dissected

  • If we want our code to execute in different situation, we need to use Event System
  • According to definations from Script mechanism, we need to "teleport a teammate" when triggering an emoticon, so we need to usePlayer Event
  • Same as above, Player.PlayAction from Player Event calls teleport function Player_PlayAct”

Example 8:

Put 10 sapling into inventory box

Game_Start = function()
	local x, y, z = 5, 7, 5
	local result = WorldContainer:addStorageBox(x, y, z)
	if result == ErrorCode.OK then --confirm inventory box is created
		local itemid, num = 212, 10
		local result = WorldContainer:addStorageItem(x, y, z, itemid, num)
	end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)

Example 9:

Every 10 seconds, teleport yourself back to spawn point

local ret, timerId = MiniTimer:createTimer("Xtimer")
if ret == ErrorCode.OK then --Start forward timer
	MiniTimer:startForwardTimer(timerId)
	local ret, num, objIds = World:getAllPlayers()
	MiniTimer:showTimerTips(objIds, timerId, "", true)
end
local ret, playerId = Player:getMainPlayerUin()
local ret, x, y, z = Spawnport:getSpawnPoint()

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 --Every 10 seconds
			Player:setPosition(playerId, x, y+1, z)
		end
	end
end
ScriptSupportEvent:registerEvent([=[minitimer.change]=], Timer_Change)

Example 10:

The game host presses "shortcut bar 1" to created 1 area and fill with blocks, press "shortcut bar 2" to copy the area to other place


act = function()
	local ret, hostid = Player:getHostUin()	
	local ret,num = Player:getCurShotcut(hostid)
	if num == 0 then 
			--When the game host presses shortcut bar 1, create a rectangular area with a center point of 10.7.10 and sides of 5
		local pos1 = {x = 3, y = 10 ,z = 8}
		local dim = {x = 2, y = 2,z = 2}--  Notice that the side length algorithm here is x*x+1=actual side length
		ret111, areaid = Area:createAreaRect(pos1, dim)
		Area:fillBlock(areaid, 112)  --fill area with block id 112
	end
	if num == 1 then
	-- When the game host presses shortcut bar 1, copy the  rectangular area to coordinate 10.12.10 (the bottom right corner is the origin, not the center)
		local pos2 = {x = 3 , y = 15 ,z = 8}
		local ret = Area:cloneArea(areaid, pos2)
	end
end
ScriptSupportEvent:registerEvent([=[Player.SelectShortcut]=], act)

Example 11:

Click on any block and move 3 blocks towards the +z direction

function Player_ClickBlock(params)
    --Click on any block and move 3 blocks towards the +z direction
    local playerId = params['eventobjid']
    
    local x, y, z = 0, 0, 3;
    Actor:appendSpeed(playerId, x, y, z)
end
ScriptSupportEvent:registerEvent([=[Player.ClickBlock]=], Player_ClickBlock)

Example 12:

Every time the character moves one block, it accelerates in the direction of the face direction

function Player_MoveStep(params)
    --Every time the character moves one block, it accelerates in the direction of the face direction
    local playerId = params['eventobjid']
    
    local ret, dirx, diry, dirz = Actor:getFaceDirection(playerId)
    print(dirx..' '..diry..' '..dirz)
    if ret == ErrorCode.OK then 
        Actor:appendSpeed(playerId, dirx, diry, dirz)
    end
end
ScriptSupportEvent:registerEvent([=[Player.MoveOneBlockSize]=], Player_MoveStep)
Last Update: 6/11/2020, 9:30:46 PM