能不能在 2 个 NPC 上使用 self.math.random?

我有这个脚本在 2 个 NPC 上,但两者都选择相同的随机数(7),我该如何让它们各自选择自己的数字?我尝试了用 self.math.random,但出现了一个错误 那么解决方案是什么呢?我需要为每个 NPC 创建与随机函数相关的不同变量吗?

function Behavior:Awake()
    math.randomseed (os.time())
self.destino = math.random( 1, 7 )

在我使用的引擎中,它说要放置 self. 来实现独立性...

点赞
用户3574628
用户3574628

将下面翻译成中文并且保留原本的 markdown 格式

The fact that you call `math.randomseed` inside a function suggests to me that you're 
calling it every time you want a random number. The purpose of `math.randomseed` is 
to _initialize_ the RNG, which means you're initializing the RNG multiple times, hence 
the repetition. Usually, you need to call `math.randomseed` exactly once in the entire 
program.

> it says to put self. for independence...

That's not a great explanation of how `self` works. `self` is a function parameter that gets 
automatically declared when you declare a function with colon notation.

事实上你在一个函数内调用math.randomseed的事实给了我暗示,你是在每次需要一个随机数的时候都调用它。math.randomseed的目的是对随机数生成器进行_初始化_,这意味着你正在多次初始化随机数生成器,因此出现了重复。通常情况下,你只需要在整个程序中调用math.randomseed一次。

它说要放置self.来获得独立...

这并不是关于self如何工作的很好的解释。self是一个函数参数,在使用冒号符号声明函数时会自动声明。

2021-01-01 17:46:50