Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: BeckzyBoi on Jun 27, 2018, 10:43 AM

Title: rand() and switch()
Post by: BeckzyBoi on Jun 27, 2018, 10:43 AM
rand() and switch() don't seem to work properly in onScriptLoad. Can anyone confirm this?
Title: Re: rand() and switch()
Post by: NicusorN5 on Jun 27, 2018, 12:56 PM
Show us how you used them.
Title: Re: rand() and switch()
Post by: BeckzyBoi on Jun 27, 2018, 01:16 PM
A friend has already told me that rand isn't seeded by default, and that it's a bug which causes it to not work. The only problem I am stuck with now is when I use switch() , maybe for the first time?, it's always the last case that is used. When I use if & else everything is fine.
Title: Re: rand() and switch()
Post by: NicusorN5 on Jun 27, 2018, 01:40 PM
  Have you used rand() and switch() like this?

local limit = 5;
local value = rand() % limit;
switch(value)
{
  case 0:
  {
   Message("0");
   break;
   }
  case 1:
  {
   Message("1");
   break;
   }
   .
   .
   .
}
Title: Re: rand() and switch()
Post by: BeckzyBoi on Jun 27, 2018, 02:03 PM
Something like that, without the 3 dots. I've scripted in PAWN for 12 years, I know how to use rand(om) and switch. They both definitely don't work properly initially in onScriptLoad.
Title: Re: rand() and switch()
Post by: Thijn on Jun 27, 2018, 04:24 PM
This works absolutely fine for me:
function onScriptLoad()
{
  srand(GetTickCount());
  for ( local i = 0; i<10; i++)
  {
    local rand = rand() % 10;
    print(rand);
  }

  local test = 5;
  switch(test) {
    case 0:
    case 1:
    case 2:
      print("Its 0, 1 or 2");
      break;

    case 5:
      print("Its 5");
      break;

    case 3:
      print("Its 3");
      break;
  }
}
(https://i.imgur.com/Rl1A7dr.png)
Title: Re: rand() and switch()
Post by: BeckzyBoi on Jun 27, 2018, 09:54 PM
Quote from: Thijn on Jun 27, 2018, 04:24 PMThis works absolutely fine for me:
function onScriptLoad()
{
  srand(GetTickCount());
  for ( local i = 0; i<10; i++)
  {
    local rand = rand() % 10;
    print(rand);
  }

  local test = 5;
  switch(test) {
    case 0:
    case 1:
    case 2:
      print("Its 0, 1 or 2");
      break;

    case 5:
      print("Its 5");
      break;

    case 3:
      print("Its 3");
      break;
  }
}
(https://i.imgur.com/Rl1A7dr.png)

rand() is working fine for you because you seeded it. It's not like that by default which I believe is a bug? Also for switch - Windows or Linux? 32 or 64 bit? I've got no idea why, but if I use switch() in onScriptLoad, it's always the last case that is used. I only tested by using switch() once, however. It could possibly work the second time of being used, I didn't test that.
Title: Re: rand() and switch()
Post by: Thijn on Jun 28, 2018, 06:24 AM
That depends. I would rather seed it myself, instead of having to rely on a seed that's build in.
I tested switch on windows 32 and 64 bit. If the last case is always being used you're probably forgetting a break; somewhere.