# 基础学习案例

小提示:在代码语法里,“ -- ” 表示注释的意思,“--” 所在的一行内,后边编写的内容脚本不会执行

脚本基础:

函数的创建:【更多lua编程知识请点击

  • 函数名称是自由创建的,a(),b(),miniworld()都是可以的
  • 函数必须使用function来创建
  • 函数的创建写法:
    • a = function() end
    • 上面这句的功能,就是创建了一个名为 a 的函数
  • 函数的内容写法:
abc = function() --创建一个名为abc的函数
	--这里编辑函数里面的内容
end
  • 函数怎么执行:
    • 函数里写了很多内容,那怎么执行呢:
abc = function() --创建一个名为abc的函数
	--这里编辑函数里面的内容
end
abc()  --调用函数写在单独一行,就会运行这个函数了

示例1:

在自己面前放下3个土块,如果敲掉土块,会替换成石块

local face = FACE_DIRECTION.DIR_POS_Y
Game_Start = function()
	local ret1, playerId = Player:getMainPlayerUin()
	--游戏开始后放置方块否则取不到玩家位置
	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事件是指游戏开始,所以在游戏开始的时候就执行了Game_Start函数

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 --被破坏的方块是土块
		Block:replaceBlock(104, xPos, yPos, zPos, face)
	end
end
ScriptSupportEvent:registerEvent([=[Block.Remove]=], Block_Removed)

示例2:

在聊天框对全部玩家输出: 你好,迷你世界

Game_Start = function()
	Chat:sendChat("你好,迷你世界") --白色字体带玩家名
	Chat:sendSystemMsg("你好,迷你世界") --橙色字体不带玩家名
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)

示例3:

如何在小地图画个以自身为原点的圆圈

-- MapMark接口目前有点问题
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

示例4:

获得一个区域内有多少个石块/怪物

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 '包含' or '不包含'
		Chat:sendSystemMsg("该区域内有"..#arr.."个生物且"..blkCont.."石方块")
	end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)

示例5:

破坏1个土块,给红队加1分

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']

	 --被破坏的方块是草块或土块则给红队加1分
	if blockid==100 or blockid==101 then
		local teamid, score = 1, 1--红队teamid是1
		local result = Team:addTeamScore(teamid, score)
	end
end
ScriptSupportEvent:registerEvent([=[Block.Remove]=], Block_Removed)

示例6:

检测角色背包里面有多少个土块,并输出在聊天框

Game_Start = function()
	local itemId = 101
	local ret, playerId = Player:getMainPlayerUin()
	local bp1Type = BACKPACK_TYPE.SHORTCUT	--快捷栏
	local bp2Type = BACKPACK_TYPE.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 = "土块数量 快捷栏:"..num1..' 存储栏:'..num2
		Chat:sendSystemMsg(text)
	end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)

示例7:

自己做一个动作表情,就把队友传送到自己面前(地图至少有2个队伍才生效)

-- 需把当前地图的队伍数量改为1个队以上
Player_PlayAct = function(event_args) -- 创建1个名为Player_PlayAct的函数,函数里面编辑的是传送队友的逻辑
	local actId = event_args['act']
	local playerId = event_args['eventobjid']
	
	if actId~=nil and actId==1 then --打招呼表情,“ ~=表示不等于; == 表示 等于”
		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 --将队友传送到自己面前
				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)
-- 需要用到Player事件的Player.PlayAction接口,做动作的时候会触发一次

代码解读

  • 想让代码不同情况下执行,我们就需要用到事件系统
  • 根据脚本运行机制的定义,我们需要在触发表情动作的时候“传送队友”,那么就需要用到Player事件
  • 如上,找到Player事件的Player.PlayAction接口调用传送队友的“函数Player_PlayAct”

示例8:

在储物箱里存入10棵树苗

Game_Start = function()
	local x, y, z = 5, 7, 5
	local result = WorldContainer:addStorageBox(x, y, z)
	if result == ErrorCode.OK then --成功创建储物箱
		local itemid, num = 212, 10
		local result = WorldContainer:addStorageItem(x, y, z, itemid, num)
	end
end
ScriptSupportEvent:registerEvent([=[Game.Start]=], Game_Start)

示例9:

每过10秒,就把自己传送回出生点

local ret, timerId = MiniTimer:createTimer("X计时")
if ret == ErrorCode.OK then --启动正向计时器
	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 --每隔十秒
			Player:setPosition(playerId, x, y+1, z)
		end
	end
end
ScriptSupportEvent:registerEvent([=[minitimer.change]=], Timer_Change)

示例10:

房主按下“快捷栏1”创建1个区域并用方块填充,按下“快捷栏2”再复制到另一个地点


act = function()
	local ret, hostid = Player:getHostUin()	
	local ret,num = Player:getCurShotcut(hostid)
	if num == 0 then 
			--房主按下快捷栏1的时候,创建一个矩形区域,中心点是10.7.10,边长是5
		local pos1 = {x = 3, y = 10 ,z = 8}
		local dim = {x = 2, y = 2,z = 2}--  注意这里的边长算法是x*x+1=实际边长
		ret111, areaid = Area:createAreaRect(pos1, dim)
		Area:fillBlock(areaid, 112)  --用112这个方块填充区域
	end
	if num == 1 then
	-- 房主按下快捷栏2的时候,把上面这个矩形复制到10.12.10位置(以右下角为原点,而不是中心点)
		local pos2 = {x = 3 , y = 15 ,z = 8}
		local ret = Area:cloneArea(areaid, pos2)
	end
end
ScriptSupportEvent:registerEvent([=[Player.SelectShortcut]=], act)

示例11:

点击任意一个方块,向+z轴方向移动3格

function Player_ClickBlock(params)
    --点击任意方块往z轴偏移3格
    local playerId = params['eventobjid']
    
    local x, y, z = 0, 0, 3;
    Actor:appendSpeed(playerId, x, y, z)
end
ScriptSupportEvent:registerEvent([=[Player.ClickBlock]=], Player_ClickBlock)

示例12:

角色每移动一格,就会往面相的方向加速度运动

function Player_MoveStep(params)
    --角色每移动一格,就会往朝向的方向移动加速度
    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)
上次更新: 2020-4-15 6:35:47 ├F10: PM┤