Main Menu
  • Welcome to The RPG Maker Resource Kit.

[RESOLVED] random number generator problem

Started by da good king, September 11, 2007, 03:17:22 AM

0 Members and 1 Guest are viewing this topic.

da good king

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?

Zeriab

You run the code just once...

Is it that you want lazy initialization?

da good king

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)

Irock

You know there's a random number generator event command.

da good king

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

modern algebra

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.

Zeriab

#6

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'

da good king

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

modern algebra

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.

da good king

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

WcW

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.
If you are reading this, the government of the U.S.A. is currently planting a chip in your brain.
People like decreasing my Rep. In fact, people like decreasing most others people's rep.

modern algebra

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.

da good king


da good king

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

Zeriab

What exactly is the error?
Take a screenshot of the error if you can and post it here

da good king

#15
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