q964391128 发表于 2022-9-5 22:35:05

冒险岛脚本编写基础教程八

第三课: 学习使用变量
在你学习编写时,你一定有发现一些以 var 起头的东西出现在脚本开头,那就
是变量。简单来说,变量是用来「取代」东西的。你可以用你命名的变量来
「取代」数字或文字等。一般来说,你会想要用变量来缩短脚本,即使只是几
个字而已。范例:
var gl = 4000313;
function start() {
cm.sendOk("哈啰,你带来 #v" + gl + "#了吗?"); // Calls the variable
gl, and the information it is replacing
cm.dispose();
}
As you can see from the example, the item id 4000313 is being replaced with the variable "gl". Even though it's only a minor difference, I shortend the script by 1 character. Now imagine having hundreds of places on a script where 4000313 was replaced by "gl". It adds up on how much space you save by using variables. That was an example on how to use a variable for a number. Here is one on how to use a variable for a string, or text sentence.
var yes = "太好了,你带齐了黄金枫叶!";
var no = "抱歉,你身上的黄金枫叶不够。";
var status;
function start() {
status = -1;
action(1, 0, 0);
}
function action(mode, type, selection) {
if (mode == 1) {
status++;
}else{
status--;
}
if (status == 0) {
cm.sendSimple("#L0# 我有 10 个黄金枫叶 #l \r\n #L1# 我有 20 个黄金
枫叶 #l");
}else if (status == 1) {
if (selection == 0) {
if (cm.haveItem(4000313, 10)) {
cm.sendOk(yes); // 使用变量「yes」,并显示你设定给他的内容
cm.dispose();
} else {
cm.sendOk(no); // 使用变量「no」,并显示你设定给他的内容
cm.dispose();
}
} else if (selection == 1) {
if (cm.haveItem(4000313, 20)) {
cm.sendOk(yes); // 使用变量「yes」,并显示你设定给他的内容
cm.dispose();
} else {
cm.sendOk(no); // 使用变量「no」,并显示你设定给他的内容
cm.dispose();
}
}
}
}

如你清楚地看见,使用这些变量省下了很大的空间。我只用了 111 个字就打 完了脚本,还包含开头的变量,而不用打长长的 202 个字。未来如果要编辑 脚本,有使用变量的话就更加简单了。

页: [1]
查看完整版本: 冒险岛脚本编写基础教程八