Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

.to and Message Window

I've got a couple of questions.

First of all, could someone please explain the basic .to's.

I know .to_f is float and some stuff like that, but I would like to know how each would be used.

Next, from the script, how can I access the "Show Text" option.

Thanks in adavnce for the help!
 
Basically you've got to_f (float), to_i (integer) and to_s (string). You'd use these when you want to treat a variable or property as if it's one type without changing the type it actually is. Like, say you wanted to list a numeric variable in a string, you'd use to_s.

As for accessing show text from within the script, if you look at the Interpreter class you'll see how that command is actually handled.

Code:
def command_101
    # If other text has been set to message_text
    if $game_temp.message_text != nil
      # End
      return false
    end
    # Set message end waiting flag and callback
    @message_waiting = true
    $game_temp.message_proc = Proc.new { @message_waiting = false }
    # Set message text on first line
    $game_temp.message_text = @list[@index].parameters[0] + "\n"
    line_count = 1
    # Loop
    loop do
      # If next event command text is on the second line or after
      if @list[@index+1].code == 401
        # Add the second line or after to message_text
        $game_temp.message_text += @list[@index+1].parameters[0] + "\n"
        line_count += 1
      # If event command is not on the second line or after
      else
        # If next event command is show choices
        if @list[@index+1].code == 102
          # If choices fit on screen
          if @list[@index+1].parameters[0].size <= 4 - line_count
            # Advance index
            @index += 1
            # Choices setup
            $game_temp.choice_start = line_count
            setup_choices(@list[@index].parameters)
          end
        # If next event command is input number
        elsif @list[@index+1].code == 103
          # If number input window fits on screen
          if line_count < 4
            # Advance index
            @index += 1
            # Number input setup
            $game_temp.num_input_start = line_count
            $game_temp.num_input_variable_id = @list[@index].parameters[0]
            $game_temp.num_input_digits_max = @list[@index].parameters[1]
          end
        end
        # Continue
        return true
      end
      # Advance index
      @index += 1
    end
  end
 

khmp

Sponsor

Ok for to there's a few. There's to_f, to_a, to_i, to_s. There's probably more but those are the ones I'll try to cover.

"to_f" is short for "To Float". Whatever value is trying to call it will be converted to float. A float as compared to an integer has a radix and one or more trailing numbers to the right of the radix. They are used to represent fractions and whole numbers. If that's the case why aren't Float objects more common. For the computer its a lot quicker for it to deal with Integers than it is Floats. ".to_f" can be applied to a Numeric, a String, or a Time. I use to_f almost all the time to bring an Integer into its Float form when I want a number less than zero.

Example:
Code:
percent = game_battler.sp / game_battler.maxsp.to_f

Only the number that is on the right side of the operation needs to be converted to a Float, the first part is automatically converted before division occurs. Like I said String's and Time as well can be used. The only reason I would think for converting the Time to float is seeding random. In any case don't mind that unless you truly want to. *Float(number) is the same as number.to_f

Example:
Code:
p "534.2".to_f # 534.2

"to_a" is short for "To Array". It attempts to convert the object into an array and a surprising amount of classes can use this method because "to_a" is defined inside Object. In which case you weren't aware virtually everything in standard Ruby derives from Object. Numbers, Strings, IO etc. When would use "to_a"? The last time I used "to_a" was to convert a vector into an array of its three components but I had to write it so I don't know how much help that is. You might want to use it on a Hash object though. After sorting it of course.

Example:
Code:
test = { 4 => 'monkey', 6 => 'is', 9 => 'bad', 2 => 'testing' }
p test.to_a.sort {|a, b| a[0] <=> b[0] }.flatten.delete_if{|a| a.is_a?(Numeric)}

:crazy:

Then you have "to_i" which is short for "To Integer". This attempts to convert a value to a Fixnum. When this happens if the object was a Float is the merciless chopping off of the numbers to the right of the radix. 2.5 becomes 2. There is no rounding played out in the conversion. If the float was less than 1 like 0.99 it becomes 0. You would want to use this when you are dealing with floats but don't want to convert a value to a float. *Integer(number) is the same as number.to_i

Example:
Code:
test = game_battler.maxsp * 0.2 # What is 20% of the maximum SP?
game_battler.sp += test # We accidentally converted sp to a float
# To avoid that we use to_i
game_battler.sp += test.to_i

"to_s" short for "To String". This method will attempt to bring whatever the invoking object is to a string. This is used quite often in drawing code for RGSS. In RGSS2 they corrected this problem by always converting to a String. But in RGSS code any number you want to draw to a bitmap must be converted to a String.

Example:
Code:
# RGSS
self.contents.draw_text(0, 0, width, 32, 5.to_s)
#RGSS2
self.contents.draw_text(0, 0, width, 32, 5) # No crash here.

You would also use to_s when bringing strings together. You can't simply say 'test' + 4. This does not work. You must convert the number to a string and then add. *String(value) is the same as value.to_s

Example:
Code:
test = 'test' + 4.to_s

Good luck with it AbyssalLord! :thumb:
 

khmp

Sponsor

AbyssalLord":3cpn41h9 said:
I did read through it...multiple times.  I'll try again though...

I'll be completely honest. I've never once read through it. I was hoping you'd get it and I could bug you with questions about it. :down:
 
Well, not sure if this will work, but you may want to try :)

Code:
    @message_waiting = true
    $game_temp.message_proc = Proc.new { @message_waiting = false }
    $game_temp.message_text = @list[@index].parameters[0] + "\n"

this is basically what's needed to show text in message box. (just normal text, not choice >_>)

so, try to use event command 'call script' with this:
Code:
    @message_waiting = true
    $game_temp.message_proc = Proc.new { @message_waiting = false }
    $game_temp.message_text = "Text you want"
in 'Text you want', you can skip line by put "\n" at the place you want to start new line :)

Since you can get value from variables in Game_Interpreter by @variable_name, e.g. @event_id,
so I think you can control @message_waiting in Game_Interpreter too.

If it cannot control from call script, make @message_waiting as writer or accessor variable,
Code:
class Interpreter
  attr_accessor :message_waiting
end
If you're using VX, change class Interpreter to class Game_Interpreter
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top