今天在研究猪人革命这个mod的卡顿问题时,发现几点做mod里的应该注意的地方,贴出来和大家分享一下。

第一点,在写代码时应有良好的缩进习惯与注释。

第二点,要做饥荒插件,应先熟练掌握lua这门脚本语言。

第三点,数据结构,代码结构要简析明块,去掉多余的冗余,提高代码可读性与运行速度。

第四点,要了解饥荒里每个函数的作用,不理解的不要乱用。

第一点纯属个人喜好问题,自己写的代码,经常过几个月之后,就不知道这些代码是什么了。不过有良好的注释的话,可以给你一些提示,还有给阅读你代码的读者的提示。

缩进习惯是很重要的,不然你的代码看起来就很乱,这里我要吐槽一下503写的代码,真是“千头万绪是多少啊”!!基本上没有缩进,有时一行代码居然有650 个字符之多。

我在lua中用的缩进与注释习惯是:

1.遇到 函数定义,if,for,while 等,深度加一,退出时深度减一。

2.end独占一行,与对应的关键字对齐。

3。if 条件里有多个and or 的话,每个另起一行。

4.定义函数时,在下面另起一行注释,说明函数作用与参数类型。

第二点的话,那是必需的,万丈高楼平地起,如果你的地基打不好的话,就算你有能力盖起一座房子,平房小楼还好说,问题不大,假如是大厦的话,一点点错误,也能让整座高楼须臾间土崩瓦解。

书籍的话,我推荐《lua程序设计第三版》《lua性能优化技巧》(Roberto Ierusalimschy著)。你在写mod前,一定要阅读这两部书,每键入一行代码,应自我检查是否出错,假如再来复查一遍的话,查错效果没有第一遍来得好。

上面两点废话说完之后,来说第三点重要的,这里我举一些例子来。

--pigking里的任务提示那一部分,大体上像
if v:HasTag("mission01") then
some code...
end
if v:HasTag("mission02") then
some code...
end
if v:HasTag("mission03") then
some code...
end
... ...
if v:HasTag("mission21") then
some code...
end
--[[
试想一下,假如现在不管是第几个任务,要调用v:HasTag() 15次
,但是我们这样写,就可以省去很多次v:HasTag(),提高运行速度]]
if v:HasTag("mission01") then
some code...
elseif v:HasTag("mission02") then
some code...
elseif v:HasTag("mission03") then
some code...
... ...
elseif v:HasTag("mission21") then
some code...
end
--假如现在是第一个任务,第一种要调用15次v:HasTag()
--第二种只要一次
--假如现在是第十个任务,第一种又要15次
--第二种只要10次
--除了mission21 两种一样多外,其他的第二种都比第一种调用次数少,所以运行速度也快。
--pigking 第20个任务更新
--给予玩家10个金子为例
--【源码】
for k = 1, 10 do
GetPlayer().components.inventory:GiveItem(SpawnPrefab("goldnugget"))
end
--这样写要调用GetPlayer() 10次
tmp_player=GetPlayer()
for i=1,10 do
tmp_player.components.inventory:GiveItem(SpawnPrefab("goldnugget"))
end
--这样只要调用一次,但这样写也不是最简便的
--在inventory.lua里,查找GiveItem的函数原型
function Inventory:GiveItem( inst, slot, screen_src_pos )
--它接受四个参数,第一个是self,就是Inventory这个类生成的对象本身
--第二个是物品的类型,可以由SpawnPrefab函数生成,这个函数定义在mainfunction里
--第三个是物品的数量,类型为number
--第四个是物品生成的位置,类型为Vector3
--所以给予玩家10个金子可以这样写
GetPlayer().components.inventory:GiveItem(SpawnPrefab("goldnugget"),10,Vector3(0,0,0))
--比如猪人革命里的modmain 中关于Recipe 的一部分
--【源码】
local bigsword = Recipe("bigsword", {Ingredient("rocks",10),Ingredient("goldnugget",5),Ingredient("livinglog",2),},RECIPETABS.WAR, {SCIENCE=0})
bigsword.atlas = "images/inventoryimages/bigsword.xml"
local death = Recipe("death", {Ingredient("nightmarefuel",10),Ingredient("petals_evil",20),Ingredient("ash",40),},RECIPETABS.MAGIC, {SCIENCE=0})
death.atlas = "images/inventoryimages/death.xml"
local mines = Recipe("mines", {Ingredient("gunpowder",3),Ingredient("flint",5),},RECIPETABS.SCIENCE, {SCIENCE=0})
mines.atlas = "images/inventoryimages/mines.xml"
local gemdarts = Recipe("gemdarts", {Ingredient("redgem",1),Ingredient("bluegem",1),Ingredient("rope",1),},RECIPETABS.WAR, {SCIENCE=0})
gemdarts.atlas = "images/inventoryimages/gemdarts.xml"
local poisondarts = Recipe("poisondarts", {Ingredient("monstermeat",3),Ingredient("flint",3),Ingredient("rope",1),},RECIPETABS.WAR, {SCIENCE=0})
poisondarts.atlas = "images/inventoryimages/poisondarts.xml"
local blastdarts = Recipe("blastdarts", {darts,Ingredient("gunpowder",1),},RECIPETABS.WAR, {SCIENCE=0})
blastdarts.atlas = "images/inventoryimages/blastdarts.xml"
local shadowdarts = Recipe("shadowdarts", {Ingredient("nightmarefuel",5),Ingredient("petals_evil",3),Ingredient("rope",1),},RECIPETABS.WAR, {SCIENCE=0})
shadowdarts.atlas = "images/inventoryimages/shadowdarts.xml"
local healthbox = Recipe("healthbox", {Ingredient("spidergland",3),Ingredient("mosquitosack",3),Ingredient("cutstone",2),},RECIPETABS.SURVIVAL, {SCIENCE=0})
healthbox.atlas = "images/inventoryimages/healthbox.xml"
local hat_sparta = Recipe("hat_sparta", {Ingredient("purplegem",1),Ingredient("log",10),Ingredient("goldnugget",5),},RECIPETABS.WAR, {SCIENCE=0})
hat_sparta.atlas = "images/inventoryimages/hat_sparta.xml"
local armor_heavy = Recipe("armor_heavy", {Ingredient("cutstone",10),Ingredient("marble",5),Ingredient("goldnugget",5),},RECIPETABS.WAR, {SCIENCE=0})
armor_heavy.atlas = "images/inventoryimages/armor_heavy.xml"
local fort_c = Recipe("fort_c", {Ingredient("cutgrass",1),},RECIPETABS.WAR, {SCIENCE=0}, "homesign_placer")
fort_c.atlas = "images/inventoryimages/fort_c.xml"
local fort_b = Recipe("fort_b", {Ingredient("cutgrass",1),},RECIPETABS.WAR, {SCIENCE=0}, "homesign_placer")
fort_b.atlas = "images/inventoryimages/fort_b.xml"
local fort_a = Recipe("fort_a", {Ingredient("cutgrass",1),},RECIPETABS.WAR, {SCIENCE=0}, "homesign_placer")
fort_a.atlas = "images/inventoryimages/fort_a.xml"
local fort_s = Recipe("fort_s", {Ingredient("cutgrass",1),},RECIPETABS.WAR, {SCIENCE=0}, "homesign_placer")
fort_s.atlas = "images/inventoryimages/fort_s.xml"
local fort_fire = Recipe("fort_fire", {Ingredient("redgem",4),Ingredient("nightmarefuel",4),Ingredient("cutstone",3),},RECIPETABS.WAR, {SCIENCE=0}, "homesign_placer")
fort_fire.atlas = "images/inventoryimages/fort_fire.xml"
local fort_icy = Recipe("fort_icy", {Ingredient("bluegem",4),Ingredient("ice",8),Ingredient("cutstone",3),},RECIPETABS.WAR, {SCIENCE=2}, "fort_icy_placer")
fort_icy.atlas = "images/inventoryimages/fort_icy.xml"
local bow = Recipe("bow", {Ingredient("twigs",5),Ingredient("pigskin",1),Ingredient("rope",2),},RECIPETABS.WAR, {SCIENCE=0})
bow.atlas = "images/inventoryimages/bow.xml"
local arrows = Recipe("arrows", {Ingredient("twigs",1),Ingredient("flint",1),Ingredient("rope",1),},RECIPETABS.WAR, {SCIENCE=0})
arrows.atlas = "images/inventoryimages/arrows.xml"
local soulstaff = Recipe("soulstaff", {Ingredient("nightmarefuel",5),Ingredient("twigs",10),Ingredient("cutreeds",1),},RECIPETABS.MAGIC, {SCIENCE=0})
soulstaff.atlas = "images/inventoryimages/soulstaff.xml"
local c4 = Recipe("c4", {Ingredient("gunpowder",3),Ingredient("goldnugget",2),Ingredient("flint",2),},RECIPETABS.SCIENCE, {SCIENCE=0})
c4.atlas = "images/inventoryimages/c4.xml"
local c4_remote = Recipe("c4_remote", {Ingredient("goldnugget",5),Ingredient("trinket_6",1),Ingredient("gears",1),},RECIPETABS.SCIENCE, {SCIENCE=0})
c4_remote.atlas = "images/inventoryimages/c4_remote.xml"
local darts = Recipe("darts", {Ingredient("flint",2),Ingredient("rocks",2),Ingredient("rope",1),},RECIPETABS.WAR, {SCIENCE=0})
darts.atlas = "images/inventoryimages/darts.xml"
--我们可以看到大体上的模式是(以物品czf为例)
czf=Recipe("czf",Ingredient,RECIPETABS,min,placer or nil)
czf.atlas="images/inventoryimages/czf.xml"
--这样我们可以用一张表来存储这些信息,之后用迭代器迭代,并送入上面的模板
Recipetable={
["bigsword "]={
{Ingredient("rocks",     10),
Ingredient("goldnugget", 5),
Ingredient("livinglog",  2),},
RECIPETABS.WAR,
{SCIENCE=0}},
["death"]={
{Ingredient("nightmarefuel",10),
Ingredient("petals_evil",20),
Ingredient("ash",40),},
RECIPETABS.MAGIC,
{SCIENCE=0}},
["mines"]={
{Ingredient("gunpowder",3),
Ingredient("flint",5),},
RECIPETABS.SCIENCE,
{SCIENCE=0}},
["gemdarts"]={
{Ingredient("redgem",1),
Ingredient("bluegem",1),
Ingredient("rope",1),},
RECIPETABS.WAR,
{SCIENCE=0}},
["poisondarts"]={
{Ingredient("monstermeat",3),
Ingredient("flint",3),
Ingredient("rope",1),},
RECIPETABS.WAR,
{SCIENCE=0}},
["blastdarts"]={
{darts,Ingredient("gunpowder",1),},
RECIPETABS.WAR,
{SCIENCE=0}},
["shadowdarts"]={
{Ingredient("nightmarefuel",5),
Ingredient("petals_evil",3),
Ingredient("rope",1),},
RECIPETABS.WAR,
{SCIENCE=0}},
["healthbox"]={
{Ingredient("spidergland",3),
Ingredient("mosquitosack",3),
Ingredient("cutstone",2),},
RECIPETABS.SURVIVAL,
{SCIENCE=0}},
["hat_sparta"]={
{Ingredient("purplegem",1),
Ingredient("log",10),
Ingredient("goldnugget",5),},
RECIPETABS.WAR,
{SCIENCE=0}},
["armor_heavy"]={
{Ingredient("cutstone",10),
Ingredient("marble",5),
Ingredient("goldnugget",5),},
RECIPETABS.WAR,
{SCIENCE=0}},
["fort_c"]={
{Ingredient("cutgrass",1),},
RECIPETABS.WAR,
{SCIENCE=0},
"homesign_placer"},
["fort_b"]={
{Ingredient("cutgrass",1),},
RECIPETABS.WAR,
{SCIENCE=0},
"homesign_placer"},
["fort_a"]={
{Ingredient("cutgrass",1),},
RECIPETABS.WAR,
{SCIENCE=0},
"homesign_placer"},
["fort_s"]={
{Ingredient("cutgrass",1),},
RECIPETABS.WAR,
{SCIENCE=0},
"homesign_placer"},
["fort_fire"]={
{Ingredient("redgem",4),
Ingredient("nightmarefuel",4),
Ingredient("cutstone",3),},
RECIPETABS.WAR,
{SCIENCE=0},
"homesign_placer"},
["fort_icy"]={
{Ingredient("bluegem",4),
Ingredient("ice",8),
Ingredient("cutstone",3),},
RECIPETABS.WAR,
{SCIENCE=2},
"fort_icy_placer"},
["bow"]={
{Ingredient("twigs",5),
Ingredient("pigskin",1),
Ingredient("rope",2),},
RECIPETABS.WAR,
{SCIENCE=0}},
["arrows"]={
{Ingredient("twigs",1),
Ingredient("flint",1),
Ingredient("rope",1),},
RECIPETABS.WAR,
{SCIENCE=0}},
["soulstaff"]={
{Ingredient("nightmarefuel",5),
Ingredient("twigs",10),
Ingredient("cutreeds",1),},
RECIPETABS.MAGIC,
{SCIENCE=0}},
["c4"]={
{Ingredient("gunpowder",3),
Ingredient("goldnugget",2),
Ingredient("flint",2),},
RECIPETABS.SCIENCE,
{SCIENCE=0}},
["c4_remote"]={
{Ingredient("goldnugget",5),
Ingredient("trinket_6",1),
Ingredient("gears",1),},
RECIPETABS.SCIENCE,
{SCIENCE=0}},
["darts"]={
{Ingredient("flint",2),
Ingredient("rocks",2),
Ingredient("rope",1),
},
RECIPETABS.WAR,
{SCIENCE=0}
}
}
for name,v in pairs(Recipetable) do
Recipe(name,v[1],v[2],v[3],v[4]).atlas="images/inventoryimages/"..name..".xml"
end

饥荒插件制作应注意的几个问题相关推荐

  1. java是如何编写我的世界_我的世界插件制作详细图文教程 教你制作强大的CraftBukkit插件...

    我的世界里有着各种不同的插件,这些插件能实现非常非常多的功能,以至于有些功能都难以置信,下面游戏园的小编就为大家解析一下我的世界怎么自己制作插件,那么到底我的世界插件制作都需要准备哪些呢?下面就请大家 ...

  2. jquery插件制作

    jquery插件丰富,很多都是很好用的,最近学习了一下如何制作jquery插件,发现jquery插件制作其实很简单,这里介绍一下. jquery插件的基本格式: (function($){$.fn.t ...

  3. WordPress插件制作教程概述

    接下来的一段时间里,开始为大家讲解WordPress插件制作系列教程,这篇主要是对WordPress插件的一些介绍和说明,还有一些我们需要注意的地方,以及需要掌握的知识. WordPress插件允许你 ...

  4. DEDECMS模块插件制作举例-模块生成向导

    下面用一个企业招聘信息作为例子让大家熟悉一下DEDECMS的模块插件制作方法. 登录DEDECMS后台,打开模块管理->模块生成向导 模块名称:企业招聘信息 联系Email:redaug@qq. ...

  5. JQuery插件制作具有动态效果的网页

    JQuery插件 制作具有动态效果的网页   前  言 JQuery 今天我给大家介绍一个运用JQuery插件制作的动态网页--iphone 5C 宣传页面.这个网页中运用到了fullpage.js和 ...

  6. [手把手教]discuzX2插件制作教程__最菜鸟级别的入门坎 一

     绝世十二少 于 2011-10-17 10:49 编辑 终于也轮到我写教程指导后人了,在此感谢会员(sw08)的热心指导,同时也感谢曾经提供各种插件资料给我的论坛朋友们,是你们帮我跨过了插件的入门坎 ...

  7. 基于jQuery日历插件制作日历

    这篇文章主要介绍了基于jQuery日历插件制作日历的相关资料,需要的朋友可以参考下 来看下最终效果图吧: 是长得丑了一点,不要吐槽我-.- 首先来说说这个日历主要的制作逻辑吧: ·一个月份最多有31天 ...

  8. Xcode7 插件制作入门

    概述 我们平时也使用了很多的xcode插件,虽然官方对于插件制作没有提供任何支持,但是加载三方的插件,默认还是被允许的.第三方的插件,需要存放在 ~/Library/Application Suppo ...

  9. chrome谷歌浏览器插件制作简易教程

    1.在磁盘上创建一个目录,用来放应用的代码和资源 2.在这个目录中,创建一个文本文件,命名为manifest.json,其内容为: {"manifest_version": 2,& ...

最新文章

  1. 2022-2028年中国电动汽车充换电站市场深度调研及投资前景预测报告(全卷)
  2. 【C++】operator bool() 和 operator const bool() const
  3. oracle加并行好不好,请教--对INSERT语句加并行度是否会提高速度
  4. 学长毕业日记 :本科毕业论文写成博士论文的神操作20170320
  5. frc机器人比赛主题_RCC机器人比赛
  6. CentOS 8部署Gitlab
  7. 后端基础概念:各种OCV一网打尽(下篇)
  8. 冯永昌:云计算与大数据时代的量化投资
  9. Ansible详解(十八)——Ansible使用小技巧
  10. centos5.8下FastDFS分布式文件系统+redis+ImageMagick
  11. NI VISION视觉安装
  12. 在vue项目中使用html2canvas实现保存网页为图片
  13. python word2vec怎么用_小白看Word2Vec的正确打开姿势|全部理解和应用
  14. 【U8+】用友U8成本管理模块下,定额分配标准中无法取到新增存货的数据。
  15. 2023年跨年代码(新年祝福语生成器)
  16. 去哪儿cli2项目总结
  17. jzyzoj 1216 poj虫洞 3259 Bellman_Ford模板
  18. 视频伪原创批量处理工具 抖音短视频解去水印
  19. 泡面吧——简单的斐波那契序列
  20. 通过下棋理解面向对象。

热门文章

  1. 如何设置计算机的休眠时间,电脑的睡眠时间如何设置?
  2. Android适配全面屏/刘海屏
  3. 成功抗“疫”背后的IT科技
  4. 夏天来了,西瓜配橙汁,来点小清新风格
  5. 鸿蒙车载系统丰田,华为公布三大鸿蒙车载操作系统
  6. 日本留学签证丢失如何补办
  7. 硬盘坏了!!!!!!
  8. 第五讲—按键控制LED
  9. 独立开发者为什么不需要运营也能月薪几万,甚至几十万?
  10. 省编码市编码区县编码_如何摆脱编码的束缚,走向事业