Help with a script call.

Started by Simon Greedwell, July 17, 2015, 03:21:07 pm

Previous topic - Next topic

Simon Greedwell

I'm trying to use this roulette script I found on a Spanish RM forum:

=begin

  Script creado por newold
  Versión 1.0 - 04-10-2014
  De uso libre

  Crea una ruleta con varias opciones y guarda el resultado de la tirada
  en una variable para poder ser usada por los eventos.


                                  INSTRUCCIONES
                                 
  * comando llamar script en los eventos y poner
 
      $ruleta = Roulette.new
     
  * Configuración de la ruleta (escribir dentro del comando llamar script):
 
    $ruleta.width = número  <- Ancho de la ruleta
   
    $ruleta.height = número <- Alto de la ruleta
   
    $ruleta.x = número <- Posición x en la pantalla de la ruleta
   
    $ruleta.y = número <- Posición y en la pantalla de la ruleta
   
    $ruleta.minMov = número <- Menor valor de desplazamiento
                               horizontal del selector
   
    $ruleta.maxMov = número <- Mayor valor de desplazamiento
                               horizontal del selector
   
    $ruleta.mov = número <- Valor fijo para calcular movimiento del selector
   
    $ruleta.randMov = número <- Valor aleatorio para calcular
                                movimiento del selector
   
    $ruleta.se = "Ruta de un efecto de sonido" <- efecto de sonido al
                                                  mover el selector
   
    $ruleta.variable = número <- número de variable a la que se le asignará el
                                 valor de dónde se ha parado la ruleta
                                 (empezando por 0)
                                 
    $ruleta.fontName = "nombre tipografía" <- nombre de la tipografía cuando se
                                              escribe texto
                                             
   
    $ruleta.fontSize = número <- Tamaño del texto
   
    $ruleta.fontColor = color <- Color con el que se dibujará el texto
                                 color = Color.new(0-255,0-255,0-255,0-255)
                                 
  * Añadir datos a la ruleta:
 
    la estructura básica para añadir cosas a la ruleta es como sigue:
   
    $ruleta.set_data(tipo,id,porciento,color de fondo)
 
    - Añadir un objeto: $ruleta.set_data('item',ID item, Porciento, Color fondo)
   
    - Añadir un protector: $ruleta.set_data('armor',ID armor, Porciento, Color fondo)
   
    - Añadir un arma: $ruleta.set_data('weapon',ID weapon, Porciento, Color fondo)
   
    - Añadir una imagen: $ruleta.set_data('ruta imagen',0, Porciento, Color fondo)
 
    - Añadir una texto: $ruleta.set_data('texto',0, Porciento, Color fondo)
   
    - Añadir porción vacía: $ruleta.set_data('',0, Porciento, Color fondo)
   
  * lanzar la ruleta:
 
    Una vez configurada la ruleta y añadido los datos, lanzar la ruleta con
   
        $ruleta.start
       
    la ruleta empezará a girar y al detenerse asignará la posición del cursor
    a la variable especificada en la configuración.
   
    Si por ejemplo has añadido 4 cosas a la ruleta, y cuando se para ésta está
    la segunda cosa que añadiste, el valor de la variable será entonces 1. Si
    está en la primera posición será 0, y así con todos los datos (0,1,2,3,4,5...)
   
    Con ese dato ya puedes configurar un evento para que reaccione con el premio
    que ha tocado :D
=end

#===============================================================================
class Roulette
  # ----------------------------------------------------------------------------
  attr_accessor :width,     :height,      :x,      :y,
                :minMov,    :mod,         :se,     :variable,
                :mov,       :randMov,     :maxMov,
                :fontName,  :fontSize,    :fontColor
  # ----------------------------------------------------------------------------
  def initialize
    @data = []
    @perc = 0
    @width = 320
    @height = 42
    @x = RUBY_VERSION < "1.9" ? 160 : 112
    @y = RUBY_VERSION < "1.9" ? 233 : 369
    @mov = 750
    @randMov = 1000
    @maxMov = 12
    @minMov = 0.2
    @mod = 12
    @se = 'Audio/SE/cursor'
    @variable = 1
    @fontName = ["Verdana","Arial"]
    @fontSize = 18
    @fontColor = Color.new(255,255,255)
  end
  # ----------------------------------------------------------------------------
  def set_data(type,id,perc,color=nil)
    @data << [type,id,perc,(color.is_a?(Color) ? color :
    Color.new(rand(255),rand(255),rand(255)))]
    @perc += perc
  end
  # ----------------------------------------------------------------------------
  def start
    draw
    mov = @mov + rand(@randMov)
    mod = @mod.to_f
    maxMov = @maxMov
    minMov = @minMov
    xx = @line.x
    n = 0
    while true
      Graphics.update
      if mov / mod >= 0.25
        x = [[mov / mod,maxMov].min,minMov].max
        xx += x
        n += x
        if n >= 5
          Audio.se_play(@se)
          n = 0
        end
        @line.x = xx
        if @line.x > @x + @width - 3
          @line.x = @x; xx = @line.x;
        end
        mov -= x
      else
        break
      end
      mod += 1
    end
    12.times {Graphics.update}
    reward = 0
    x = @x + 2
    for i in 0...@data.size
      data = @data[i]
      x2 = x + (data[2] * (@width-4) / @perc)
      if @line.x + 2 >= x and @line.x + 2 <= x2
        break
      else
        reward += 1
        x = x2
        next
      end
    end
    $game_variables[@variable] = reward
    dispose
  end
  # ----------------------------------------------------------------------------
  def draw
    w, h = @width, @height
    @bar = Sprite.new
    @bar.x = @x
    @bar.y = @y
    @bar.z = 10000000
    @bar.bitmap = Bitmap.new(w,h)
    @bar.bitmap.fill_rect(0,0,w,h,Color.new(235,235,235))
    @bar.bitmap.fill_rect(1,1,w-2,h-2,Color.new(160,160,160))
    @bar.bitmap.fill_rect(2,2,w-4,h-4,@data[-1][3])
    b2 = Bitmap.new(w,h)
    x = 2
    sep = []
    for data in @data
      w2 = (data[2] * (w-4) / @perc)
      @bar.bitmap.fill_rect(x,2,w2,h-4,data[3])
      item = nil
      case data[0]
      when 'item','objeto',1
        item = $data_items[data[1]]
      when 'weapon','arma',2
        item = $data_weapons[data[1]]
      when 'armor','armadura',3
        item = $data_armors[data[1]]
      else
        if (data[0].class == Bitmap)
          item = data[0]
        elsif (data[0].class == String)
          begin
            item = Bitmap.new(data[0])
          rescue
            begin
            b = Bitmap.new(1,1)
            b.font.name = @fontName
            b.font.size = @fontSize
            size = b.text_size(data[0])
            b = Bitmap.new(size.width,size.height)
            b.font.name = @fontName
            b.font.size = @fontSize
            b.font.color = @fontColor
            b.draw_text(0,0,b.width,b.height,data[0])
            b2.blt(x+w2/2-b.width/2,(h-4)/2-b.height/2,b,b.rect)
            rescue
            end
          end
        end
      end
      if [RPG::Item,RPG::Weapon,RPG::Armor].include?(item.class)
        b = RPG::Cache.icon(item.icon_name)
        b2.blt(x+w2/2-b.width/2,(h-4)/2-b.height/2,b,b.rect)
      elsif item.is_a?(Bitmap)
        b = item
        b2.blt(x+w2/2-b.width/2,(h-4)/2-b.height/2,b,b.rect)
      end
      x += w2
      sep << x unless data == @data[-1]
    end
    @bar.bitmap.blt(0,0,b2,b2.rect)
   
    color = Color.new(0,0,0)
    sep.each {|x|
      @bar.bitmap.fill_rect(x,2,1,h-4,color)
    }
   
    @line = Sprite.new
    @line.bitmap = Bitmap.new(5,h+4)
    @line.bitmap.fill_rect(0,0,1,h+4,Color.new(255,0,0,55))
    @line.bitmap.fill_rect(1,0,1,h+4,Color.new(255,0,0,160))
    @line.bitmap.fill_rect(2,0,1,h+4,Color.new(255,0,0,255))
    @line.bitmap.fill_rect(3,0,1,h+4,Color.new(255,0,0,160))
    @line.bitmap.fill_rect(4,0,1,h+4,Color.new(255,0,0,55))
    @line.x = @x
    @line.y = @y - 2
    @line.z = 10000001
  end
  # ----------------------------------------------------------------------------
  def dispose
    @bar.dispose
    @line.dispose
  end
  # ----------------------------------------------------------------------------
end
#===============================================================================


In order to use, I need to cram a code similar to this into the script window:



But when I attempt to do the same in my game, this is the result:

a = Roulette.new
a.set_data('',0,20,Color.new(160,0,0,100))
a.set_data('item',1,10)
a.set_data('',0,20,Color.new(160,0,0,100))
a.set_data('armor',1,35)
a.set_data('',0,20,Color.new(160,0,0,100))
a.set_data('weapon',1,10)
a.set_data('',0,20,Color.new(160,0,0,100))


I'm left with a few lines missing due to space constraints. My question is: is there a way to bypass the character limit of the Script window to make this work as intended?

I wish I could ask the author of this script but s/he's nowhere to be found.
Bury with my...money!

KK20


Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Simon Greedwell

July 17, 2015, 05:16:42 pm #2 Last Edit: July 17, 2015, 05:19:57 pm by Simon Greedwell
I tried calling the roulette after dividing the script call in two parts and an error pops out:

SyntaxError occurred while running script


It seems the code must be just like in the example from the demo for this to work i.e like this:

a = Roulette.new
a.set_data('',0,20,Color.new(160,0,0,100))
a.set_data('item',1,10)
a.set_data('',0,20,Color.new(160,0,0,100))
a.set_data('armor',2,35)
a.set_data('',0,20,Color.new(160,0,0,100))


And not like I had on my project:

a = Roulette.new
a.set_data('',0,20,Color.new
(160,0,0,100))
a.set_data('item',1,10)
a.set_data('',0,20,Color.new
(160,0,0,100))
a.set_data('armor',2,35)
a.set_data('',0,20,Color.new
(160,0,0,100))


So the character limit per line in the Script window is the actual problem here.
Bury with my...money!

KK20

It's really a matter of knowing how to structure your code while still keeping it syntactically correct.

a = Roulette.new
a.set_data('',0,20,
Color.new(160,0,0,100))
a.set_data('item',1,10)
a.set_data('',0,20,
Color.new(160,0,0,100))
a.set_data('armor',2,35)
a.set_data('',0,20,
Color.new(160,0,0,100))


For long script lines, return at the end of commas (as shown above) or the opening of a parenthesis

a.set_data("",0,20,Color.new(
160,0,0,100))

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Simon Greedwell

That did the trick. A million thanks!  :)

Can't help but wonder why the Script call window is so cripplingly limited though. But again, guess most scripts only need a line or two to be called. 
Bury with my...money!

KK20


Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Simon Greedwell

Thanks for the advice. I think it'll be useful to have around should I decide to use any other script that requires long script calls.
Bury with my...money!