1

我是使用这种语言的新手,我正在尝试编写自己的机器人。我已经掌握了基础知识并设法使用变量和别名,但是我期待在我的聊天中做一个迷你游戏,你可以在其中拥有自己的宠物,命名并升级它。

我可以做到这一切,但我的问题在于,在一天结束时,我会关闭程序,所有的宠物都会消失,这样就破坏了它的目的。

我想知道是否有任何方法可以保存这些变量并在每次打开程序时重新加载它们,也许将它们保存在 .txt 中?

任何建议都非常感谢。

4

1 回答 1

1

我同意其中一个评论,即最好使用.ini 文件来解决这个问题。

语法示例,取自上面链接的 url:

writeini reminder.ini birthday jenna 2/28/1983
writeini reminder.ini birthday Mike 10/10/1990

这会产生以下文件:

[birthday]
jenna  = 2/28/1983
Mike   = 10/10/1990

并且是这样阅读的:

echo -a Mike: $readini(reminder.ini, n, birthday, mike)
echo -a Jenna: $readini(reminder.ini, n, birthday, jenna)

如果您想更灵活地定义自己的数据格式,还可以恢复为纯文本文件。basic/write$readfunctions 具有一些非常简洁的功能:请参阅文档

像这样的东西应该适用于写作:

; search if the pet is already saved
$read(pets.txt,ns,%petname)
if ($readn == 0) {
    ; append to end of file
    write pets.txt %petname %age %mood
}
else {
    ; replace line
    write -l $readn pets.txt %petname %age %mood
}

检索特定宠物:

var %pet_info = $read(pets.txt, ns, %petname)
; Split the found line on space (ASCII-code 32)
tokenize 32 %pet_info
var %age = $2
var %mood = $3

这将返回以您要查找的宠物名开头的行。

于 2015-07-16T20:50:15.707 回答