我为什么选择Defold游戏引擎?

180 阅读5分钟

前面写Lua入门的时候提到,想在工作之外找到事情做,于是选择尝试游戏开发。首先明确自身的定位,我基本上不可能做大型的游戏,只能是一些小游戏,也只能是2D的,不考虑任何3D相关的方案。做出来的游戏需要能支持多个平台,包含Windows版本、App版本、Web H5版本等。开发语言必须要简单,要快捷,要占用很少的资源就能运行。游戏引擎还最好是免费的,不想花钱。

三种游戏引擎对比

我对比了Defold、Godot、Cocos Creator三种游戏引擎,首先这三个都是免费的。简单做了一个对比表如下所示:

对比项DefoldDodotCocos Creator
开发语言LuaC# 或 GDScrioptTypeScript
2D/3D2D2D,正在丰富3D相关功能支持3D,但更侧重于2D
商业模式完全自由免费,无任何限制完全自由免费,无任何限制开源
支持发布平台Linux Windows H5 Facebook iOS Android macOS XBox官网上说支持所有平台,但最新的Godot4对于H5支持的还不是很好iOS, Android, Windows, macOS, Web

从上表来看,我一定要求最丰富的发布平台,而且只需要2D,那么Defold是我最好的选择。不选择Cocos Creator的主要原因还是个人不喜欢TypeScript,每次写前端都会莫名其妙的引入了一大堆的依赖(当然,可能是我水平差)。

Defold初体验

在初步了解Defold之后,更坚定了选择它的理由如下:

  1. 操作简单,资源管理器、脚本编码器、属性设置窗口都设置的很符合使用习惯;
  2. Lua脚本简单,相关的API封装的非常好,文档清晰;
  3. 丰富的学习资源,有示例,有文档;
  4. 有非常丰富的游戏资产,包含广告、数据分析、动画、UI控件、数学、物理引擎、网络等等各类开箱即用的资产;
  5. 非常清晰的结构;
  6. 非常清晰的脚本执行流程。
Defold清晰的结构

Defold游戏引擎中的元素都层层嵌套的,每一个元素就可以认为是一个对象。如下图所示:

building_blocks.png

不得不借用Defold官网这张图,因为它画的非常清楚,根本不需要重新再画。从这个图可做说明如下:

  1. 游戏最上层的元素是Collection,Collection内部可以包含其他的Collection与Game Object,通常用于构建游戏关卡,以及由多个游戏对象构建的敌人或角色。比如:一个敌人可能包含一个身体与一把武器,那么就可以把身体Game Object与游戏Game Object两个组合成一个Collection,然后添加到关卡Collection中。
  2. Game Object 是具有ID,位置,旋转和缩放的容器。用于包含组件。它们通常用于创建玩家角色、敌人、礼物、子弹等等,能在游戏中独立存在的东西。
  3. Component 是放入到Game Object中的实体,比如:图像、声音、脚本等等。
Defold清晰的脚本执行流程

Defold有三种脚本,分别是script gui_script render_script。script是Game Object的脚本,gui_script是Gui界面的脚本,render_script是渲染脚本。最入门的只以script为示例,默认的script脚本如下:

function init(self)
	-- Add initialization code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
end

function final(self)
	-- Add finalization code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
end

function update(self, dt)
	-- Add update code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
end

function fixed_update(self, dt)
	-- This function is called if 'Fixed Update Frequency' is enabled in the Engine section of game.project
	-- Can be coupled with fixed updates of the physics simulation if 'Use Fixed Timestep' is enabled in
	-- Physics section of game.project
	-- Add update code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
end

function on_message(self, message_id, message, sender)
	-- Add message-handling code here
	-- Learn more: https://defold.com/manuals/message-passing/
	-- Remove this function if not needed
end

function on_input(self, action_id, action)
	-- Add input-handling code here. The game object this script is attached to
	-- must have acquired input focus:
	--
	--    msg.post(".", "acquire_input_focus")
	--
	-- All mapped input bindings will be received. Mouse and touch input will
	-- be received regardless of where on the screen it happened.
	-- Learn more: https://defold.com/manuals/input/
	-- Remove this function if not needed
end

function on_reload(self)
	-- Add reload-handling code here
	-- Learn more: https://defold.com/manuals/hot-reload/
	-- Remove this function if not needed
end

从上面注释也能看出来各个方法的用途,如果不想写某一个方法的逻辑,也可以把空的方法都删除。

  1. init(self) 方法是在Game Object初始化的时候运行,只会运行一次,可用于初始化某些属性。
  2. final(self) 方法是在Game Object被销毁时运行,可用于释放资源。
  3. update(self, dt) 方法是Game Object每一帧渲染时执行的,可在此处理每一帧的逻辑,比如移动某个角色的位置等。
  4. fixed_update(self, dt) 方法是按游戏的game.project配置文件中指定的频率进行执行。
  5. on_message(self, message_id, message, sender) 方法用于接收Game Object之间传递的消息,Defold中消息系统非常重要,通过消息解耦,提高灵活性。
  6. on_input(self, action_id, action) 方法用于接收用户的输入,输入配置于game.input_binding。
  7. on_reload(self) 方法是在Game Object重新加载时执行的方法。

好了,有这些理由就够了,接下来就用Defold了,看我做出一个又一个废物游戏吧。