1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Special random number generator for replays

Discussion in 'Programming' started by Zvoc47, Sep 25, 2016.

  1. Zvoc47

    Zvoc47 Guest

    Hello.
    I saw that on PSX games Driver 1 and Driver 2, there's always the same randomly generated set of cars that spawn as you drive if you press the exact same buttons at the exact same time as before. I think that the buttons actually control the random generator's seed dynamically as the player plays the game. This is made so that the replay manager called Film Director always makes the exact same cars spawn just like when the replay was recorded.

    Now, I see that there's only one random function which could be also called by the GUI or something that's not of the game which would cause the replay to get wrongly recorded and thereby collapse during the playback. This is a problem. If you don't understand, let's imagine that the random number generator generated numbers 1, 2, 3, 4, 5. The game calls the random function twice so there's 1 and 2 used. The player pauses the game and the pause menu has a graphical effect that always randomly bounces which calls numbers 3 and 4. Then the player resumes the game and the random function is called. The replay has been recorded with numbers 1, 2 and 5. Now, the player tries to see the replay. The replay calls the random function and the random function gives 1, 2, 3. As it gave number 3, the replay is no longer the same as when it was recorded. See the problem?

    I'm thinking that there should be some kind of extension or algorithm to use to create my own random generator which is only affected by the gameplay and not by any pause menus. Does any like that exist?
     
  2. comatsu

    comatsu Member

    Joined:
    Jun 20, 2016
    Posts:
    22
  3. Mr Magnus

    Mr Magnus Viking King

    Joined:
    Jun 22, 2016
    Posts:
    75
    Why not just set the seed? we have full access to read, set and change the random seed at any point we wish. You don't need a special random generator, just have to be sure that whatever you use that affects the seed does not count when in "paused" modes
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,994
    Either that or the Driver games used the same old randomization as most other console games in the past.

    Create a persistent object and make sure it is in the very first room of your project. In its create event set a variable stepcounter = 0 and in its Begin Step Event increment it stepcounter=++stepcounter & $FFFFFFFF. The &$FFFFFFFF is to make sure it never ever ever maxes out. (If you ever program for a system where it will simply roll over automatically, then you don't need to use &$FFFFFFFF, but GM:S doesn't roll over automatically (and if the value gets too high, it could actually go negative, which you also don't want). When you want something to happen randomly, you take the value of stepcounter, add whatever other variables you may want to it (optional), and then AND or MOD it with some other value (preferably one less than a power of 2). So for example, rand=stepcounter & 7 would give you a random value {0,7}, which is the same as rand=stepcounter mod 8. You then use that value to determine the next course of action. So if you wanted something to have a 1/n chance of happening, you'd check if !(stepcounter mod n) to see if the random check passes.

    Now to put that in perspective of a game like Driver. Suppose there were 12 models of cars that could spawn. Add those cars to a list. Now, you wouldn't want a car to spawn all the time necessarily, so add a bunch of -1 values to the list. If you had 12 cars, then adding 12 -1s would give you a 50/50 chance of spawning a car. Set the random seed to whatever value you want. Or maybe you do. Whatever; the point is you just want GM to randomize that list for you so you don't waste your time doing it yourself. If you want to do it yourself, knock yourself out - in which case you should use an array instead of a list. Shuffle that list and save the size of the list to a variable. When you need to consider spawning a car, get the random value (see previous paragraph) and then retrieve from your list of cars the value indexed by that variable. If it's -1, don't spawn any cars, otherwise spawn the car as indexed in the list.
     
  5. zircher

    zircher Member

    Joined:
    Jun 21, 2016
    Posts:
    380
    Alternate idea: Since your GUI does not need a true random function, make an array of fixed numbers that it pulls 'random' values from. Base your stating location in the array on the clock so it is semi-random even if it loops through the same set of numbers over time.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice