Page 1 of 1

MP-compatible math.random lua function?

Posted: Mon Aug 12, 2013 1:31 am
by razac920
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:

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
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?

Re: MP-compatible math.random lua function?

Posted: Mon Aug 12, 2013 2:13 am
by Marth8880
I personally think utilizing os.time() is a better idea - like base the chosen mode on the current minute (or minute range such as 00-30) or hour (probably not hour though because of time zone differences and junk; it could also be used to implement time-based weather and the like. :o

http://www.lua.org/pil/22.1.html

It's really a pretty flawless(-seeming) theory because since all Windows PCs (default to) grab the time off of an Internet time server. Now, if somebody makes the argument that the player might not have an Internet connection, how would they even play in a multiplayer match at all? ;)

Re: MP-compatible math.random lua function?

Posted: Mon Aug 12, 2013 8:38 am
by Maveritchell
I'm pretty sure that SWBF2 .lua doesn't support the OS functions, unless you've verified that it can, Marth. This was actually something I was playing around with just recently and saw no real headway on. I was playing around a lot with os.write, but I'm pretty sure I used os.time just as a test case (to see if I could even get a printout with some feedback).

Re: MP-compatible math.random lua function?

Posted: Mon Aug 12, 2013 8:49 am
by razac920
I just tried using os.time() in offline singleplayer (actually printing it out) but got a error in the log:
Message Severity: 3
.\Source\LuaHelper.cpp(312)
CallProc failed: (none):0: attempt to call field `time' (a nil value)
stack traceback:
(none): in function <(none):100>