Main Menu
  • Welcome to The RPG Maker Resource Kit.

Global Random?

Started by shintashi, November 11, 2010, 04:10:39 PM

0 Members and 1 Guest are viewing this topic.

shintashi

How do I take this script

#==============================================================================
# ** die tumbler
#------------------------------------------------------------------------------
#  This is a test script
#  it is designed to create random numbers
#==============================================================================

class Die_Tumbler
    def initialize
x = rand(10) + 1
     if x == 10
       x = 10 + rand(10)
else
   x = x
  end # end if

  y = rand(10) + 1
     if y == 10
       y = 10 + rand(10)
else
  y = y
end # end if

#print x + y
$d20 = x + y
 
  end # end def

 
end # end class



and make it global so I can access the total of x + y?

Also, how do I access it anywhere inside the script? do I just put in something like
+ $d20 or  + Die_Tumbler

I'm really new to making classes in ruby.

P.S. I've been testing this using an event

p $d20

to see if I could access it outside itself, and was met with repeated failure and "nil".

modern algebra

#1
Well, I don't see any reason to use a class for that. Maybe a module might be better?

Like:


module Die_Tumbler
  def self.d20
    val = 0
    2.times do
      x = rand(10) + 1
      x += rand(10) if x == 10
      val += x
    end
    return val
  end
end


You could then run the method anytime with the code:


Die_Tumbler.d20

   

I am curious what you want to do with it though, since it is a very strange method. Anyway, the above code does what you apparently want to do.

Note, of course, that whenever you use it, it will rerun the method, not retain the first result. So, if you want to only do it once and operate on that result, you will need to save it to a variable.

P.S. why is your indenting so crazy? It makes it hard to read.

shintashi

Quote from: modern algebra on November 11, 2010, 05:52:23 PM
I am curious what you want to do with it though, since it is a very strange method. Anyway, the above code does what you apparently want to do.
I've never used modules before, and only recently learned about using classes, but it worked.
The method is weird because I'm simulating a particularly quirky game mechanic known as "exploding dice". I'm using it to simulate wild surges, backlash, and botches.

Quote
Note, of course, that whenever you use it, it will rerun the method, not retain the first result. So, if you want to only do it once and operate on that result, you will need to save it to a variable.
Since it is a dice simulator, having it rerun the method every time is fine. The actual "variable" will be a byproduct of the total, ala West End's old Torg system.

Quote
P.S. why is your indenting so crazy? It makes it hard to read.
I was using spaces instead of indents to distinguish one "end" from the next, since some of the code is new to me and I tend to omit or add one too many 'ends'.