MP-compatible math.random lua function?
Posted: Mon Aug 12, 2013 1:31 am
I've read in the forums here many times that math.random() does not work in multiplayer (causing crashes for non-host players), and I was wondering if perhaps the cause is that math.random() would not give the same results for each computer that ran the function ScriptPostLoad() -- or maybe, this is just an idea.
In the Zombie Infection mode I'm working on, I would like to (for online play) randomly choose a (human?) player to be the first zombie. I thought maybe setting a seed myself based on a time could work (ScriptCB_GetMissionTime() should? maybe be a good choice). At any rate, I've tried running this code:
online (but alone with no bots) and have gotten different results, which is promising, suggesting that ScriptCB_GetMissionTime() doesn't just return 15 each time (the amount of time it takes to first spawn).
My question is, has anyone ever tried setting the seed for the pseudo-random number generator to make it multiplayer compatible, and if not, would anyone be willing to help me test this idea?
In the Zombie Infection mode I'm working on, I would like to (for online play) randomly choose a (human?) player to be the first zombie. I thought maybe setting a seed myself based on a time could work (ScriptCB_GetMissionTime() should? maybe be a good choice). At any rate, I've tried running this code:
Code: Select all
if ScriptCB_InMultiplayer() then
firstspawn = OnCharacterSpawn(function(c)
local temp = ScriptCB_GetMissionTime()
temp = temp - math.floor(temp)
math.randomseed(math.floor(1/temp))
math.random(1,100) -- because for some reason the first number is ALWAYS 1
SetTeamReinforcements(1, math.random(1,100))
end)
end
My question is, has anyone ever tried setting the seed for the pseudo-random number generator to make it multiplayer compatible, and if not, would anyone be willing to help me test this idea?