MP-compatible math.random lua function?

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
razac920
2nd Lieutenant
2nd Lieutenant
Posts: 365
Joined: Sun Jan 16, 2011 12:42 am

MP-compatible math.random lua function?

Post 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?
Marth8880
Resistance Leader
Posts: 5042
Joined: Tue Feb 09, 2010 8:43 pm
Projects :: DI2 + Psychosis
Games I'm Playing :: Silent Hill 2
xbox live or psn: Marth8880
Location: Edinburgh, UK
Contact:

Re: MP-compatible math.random lua function?

Post 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? ;)
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: MP-compatible math.random lua function?

Post 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).
razac920
2nd Lieutenant
2nd Lieutenant
Posts: 365
Joined: Sun Jan 16, 2011 12:42 am

Re: MP-compatible math.random lua function?

Post 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>
Post Reply