The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: da good king on September 11, 2007, 03:17:22 AM

Title: [RESOLVED] random number generator problem
Post by: da good king on September 11, 2007, 03:17:22 AM
A variable in my game (acting as an actor attribute) is a random number. I use var = rand(#) to generate it...blah blah
The problem is: the value is supposed to be determined randomly and then remain constant FOR THE REST OF THE GAME, but using the rand( function makes it ever changing!

How do I tell the script to determine the number as random, but then save that first result as the value permanantly?
Title: Re: random number generator problem
Post by: Zeriab on September 11, 2007, 04:20:05 AM
You run the code just once...

Is it that you want lazy initialization?
Title: Re: random number generator problem
Post by: da good king on September 11, 2007, 04:43:18 AM
run it once? how?

my script looks like:

x = 25
n = rand(50)
return x * 2 + n

this is for something like max HP, but the max and total HP are different and it changes everytime i close and re-open the menu. How do i keep it from doing this? (btw im brand new at scripting and learning quickly if u havent noticed)
Title: Re: random number generator problem
Post by: Irock on September 11, 2007, 04:48:38 AM
You know there's a random number generator event command.
Title: Re: random number generator problem
Post by: da good king on September 11, 2007, 05:53:13 PM
yes i no that but it might not work correctly if i change the value that way. Cuz it's an actor attribute in the Game_Actor script which performs on multiple objects, so it might make every characters....rand_str (just an example) stat ALL THE SAME random #. Where as I want every characters different
Title: Re: random number generator problem
Post by: modern algebra on September 11, 2007, 06:54:12 PM
Well, you need to initialize it just once. If it's something in Game_Actor, then the best thing to do would be to put it in the initialize method of Game_Actor. If you put it into the initialize method, then it will not be the same random number for every actor either. If you need to save that random number, then save it as a class variable and it will remain constant for the entire game. Or you could even make it a constant.
Title: Re: random number generator problem
Post by: Zeriab on September 11, 2007, 07:05:38 PM

class Game_Actor
  attr_reader :random_hp_add
  def random_hp_add=(value)
    return  unless @random_hp_add.nil?
    @random_hp_add = value
  end
end


Usage:

x = 25
actor.random_hp_add = rand(50)
return x * 2 + actor.random_hp_add


actor is here assumed to be a Game_Actor. If you are in the Game_Actor class itself then just use 'self.random_hp_add' instead of 'actor.random_hp_add'
Title: Re: random number generator problem
Post by: da good king on September 12, 2007, 04:01:19 AM
thx zeriab ur class variable method ALMOST worked...it does stay constant, but while i can tell both characters had at sometime had different values, (there total HP starts different) their maxhp....'corrects' itself and they all become equal. -_-

thx again for helping though
Title: Re: random number generator problem
Post by: modern algebra on September 12, 2007, 04:16:42 AM
ermm... Are you retaining the original levelling method? Because, if you want your value to effect all levels you will need to overwrite that levelling method.
Title: Re: random number generator problem
Post by: da good king on September 12, 2007, 06:01:37 PM
no, im using my own level method actually.

What i mean is that all ACTORS have the same random_value which I don't want. I used a max/min like HP b/c it actually doubles the test results. (total HP and max HP being two separate values)

Using ur method the values dont reset themselves, but all actors have the same random_value (in this case maxhp) And no its not luck, i re-checked and re-checked and their equal. (:|

The total (initial) HP of each actor start different though, so somehow THAT value works, but the other (maxhp) does not???    ?_?

I think the problem is that I left the value maxhp (NOT base_maxhp) completely alone and have been experimenting with base_maxhp only, but  im at school now, so ill post later when i get home and test this fact. thx agn
Title: Re: random number generator problem
Post by: WcW on September 15, 2007, 04:32:31 PM
How about make the random number a class variable of Game_Actor and let it be referenced instead of rand() each time? That should do what you want.
Title: Re: random number generator problem
Post by: modern algebra on September 15, 2007, 04:52:27 PM
Well, the solution Zeriab posted does pretty much that. @random_hp_add is in Game_Actor, and it will initialize if it has not already been initialized. My guess would still be that this method:


  #--------------------------------------------------------------------------
  # * Get Maximum HP
  #--------------------------------------------------------------------------
  def maxhp
    n = [[base_maxhp + @maxhp_plus, 1].max, 9999].min
    for i in @states
      n *= $data_states[i].maxhp_rate / 100.0
    end
    n = [[Integer(n), 1].max, 9999].min
    return n
  end


needs to be overwritten, but of course I could be wrong.
Title: Re: random number generator problem
Post by: da good king on September 30, 2007, 11:55:22 PM
ya thats wat i thought. thx
Title: Re: random number generator problem
Post by: da good king on December 15, 2007, 12:49:14 AM
bit of a necropost but... originally i had not used zeriabs idea in its fullest and used the self. function, only the class variable idea. Now, I just basically copied down what Z wrote just for good measure, and it says method 'random_hp_add' is undefined...

ps-yes, i did define it as Z wrote it
Title: Re: random number generator problem
Post by: Zeriab on December 15, 2007, 06:45:34 AM
What exactly is the error?
Take a screenshot of the error if you can and post it here
Title: Re: random number generator problem
Post by: da good king on December 16, 2007, 08:22:43 PM
NEVERMIND!!! figured it out :)

originally i kept making @random_hp_add = value, then in the maxhp definition += rand(50) or saying @raandom_hp_add = rand(50), the trick was to define that in the initialization, not the defintion of maxhp. (tho i have a feeling u said that, i just didnt read it right ;)

so, thx Z and the rest a yall 4 ur help  ;8