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.

Info text depending on where the cursor is in the command window.

Hi,

I have a question that i hope someone could answer for me.

Lets say you created a new window in the menu, and wanted it to display information about the different options in the command window.
When the cursor is at  "Items" in the command window it will display a text in the other window saying something like : "Here you can see wich items you have"
And when the cursor is at "Save" in the command window it will display another text in the other window saying something like: "Here you can save your progress".  How would you script that?

I know how to create the "Info window"  but what i would like to know is, how can you make it so that it displays different text depending on where the cursor is in the command window?

I hope i made it clear enough, if not, tell me and i will try to explain better. :thumb:
Thanks for reading my post!

Over and out - Gando
 

khmp

Sponsor

Window_Command has a method for retrieving the index of the currently selected item. Now this Window_Info is in the same scene, correct? So include an additional case statement in the update of the scene that tells the Window_Info to refresh if the index has changed. You will need to also keep track of the current Command_Window index using an instance variable. Before the case statement that asks which Window_Command.index are we at, have the conditional asking has the index changed since the last time you reached this piece of code.

Code:
current_index = Window_Command.index
if current_index != @current_index
  @current_index = current_index
  case @current_index
    when ...
      text = 'Item window info.'
  end
  Window_Info.text  = text
  Window_Info.refresh
end

Good luck with it Gando! :thumb:
 
Thanks for the reply khmp! :thumb:

Hmm..  i don't understand.. :shock:
Lets say i created a new script,and made a window like Window_Gold, and then in Scene_Menu i replaced:
(this is just and example, the size of the window would be to small to display a long text of information)
Code:
    @gold_window = Window_Info.new

with:
Code:
@info_window = Window_Info.new

then i changed all the other "@gold_window" to "@info_window" in scene menu. 
Now it displays the info window instead of the gold window.

And lets say the Window_Info script looked something like this:
(this is not the real thing, just to let you know how i'm thinking. look at the comments in def refresh)
Code:
#==============================================================================
# ** Window_Info
#------------------------------------------------------------------------------
#  This window displays information about the options in the command window.
#==============================================================================

class Window_Info < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 160, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    # Should probably put something like this here?:
    # case @current_index
    # when 1
    #self.contents.draw_text(0, -20, 100, 62, "Here you can see wich items you have", 0)
    # when 2
    #self.contents.draw_text(0, -20, 100, 62, "Here you can see wich skills your hero has", 0)
    end
  end
end

But if i understood your explanation right, i would have to create the window inside Scene_Menu?
please bare with me, i'm not very good at scripting  :down: :tongue2:

Over and out - Gando
 

khmp

Sponsor

Firstly its "which" not "wich". And you're not bad at scripting. You are getting better. The above code I gave you was psuedo code. But I'll give you a little more in depth version.

Code:
class Window_Info
  def initialize
    # standard initialization code here.
    # don't call refresh because we have nothing to draw.
  end

  def refresh
    return if @text.nil?
    # draw text
  end

  def text=(text)
    return if @text == text
    @text = text
    refresh
  end
end

class Scene_Menu
  alias_method :gand_windowinfo_scene_menu_update_command, :update_command

  def main
    ....
    # Deleted the gold window initialize and in place of it have a window_info instantiation.
    @window_info = Window_Info.new
    ...
    # Deleted the gold window dispose and in place of it have window_info dispose
    @window_info.dispose
  end

  def update_command
    gand_windowinfo_scene_menu_update_command

    if @command_window.index != @current_index
      @current_index = @command_window.index
      case @current_index
      when 0
        text = 'Item window info.'
      when 1
        text = 'Other stuff etc, etc.'
      end
      @window_info.text  = text
    end
  end
end

The case statement would not want to be used inside the window's refresh. That would be nasty and although I doubt you would this window elsewhere. You would severely limit the window's ability. When in coding in Ruby, at least for me, the more flexible an item can be the better. Even if it only supports one issue when you start it might branch out and be able to be used elsewhere.

Good luck with it Gando! :thumb:
 
aaah "Which", got it! :thumb:
Okaay, so now i've changed some stuff around in Scene_Menu.
But i get an error from the line under class Scene_Menu:
Code:
  alias_method :gand_windowinfo_scene_menu_update_command, :update_command

the error message is :
Code:
Script 'Scene_Menu' line 24 NameError occured.
undefined method `update_command' for class `Scene_Menu´

Any ideas?  :shock:
Here is my whole Scene_Menu, if you could take a look at it please. :smile:
Code:
class Window_Info < Window_Base
  def initialize
    super(0, 0, 160, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
  
  def refresh
    return if @text.nil?
    #What should i do here?
  end

  def text=(text)
    return if @text == text
    @text = text
    refresh
  end
end
#==============================================================================
# ** Scene_Menu
#------------------------------------------------------------------------------
#  This class performs menu screen processing.
#==============================================================================
class Scene_Menu
  alias_method :gand_windowinfo_scene_menu_update_command, :update_command
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     menu_index : command cursor's initial position
  #--------------------------------------------------------------------------
  def initialize(menu_index = 0)
    @menu_index = menu_index
  end
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Make command window
    s1 = $data_system.words.item
    s2 = $data_system.words.skill
    s3 = $data_system.words.equip
    s4 = "Status"
    s5 = "Save"
    s6 = "End Game"
    @command_window = Window_Command.new(160, [s1, s2, s3, s4, s5, s6])
    @command_window.index = @menu_index
    # If number of party members is 0
    if $game_party.actors.size == 0
      # Disable items, skills, equipment, and status
      @command_window.disable_item(0)
      @command_window.disable_item(1)
      @command_window.disable_item(2)
      @command_window.disable_item(3)
    end
    # If save is forbidden
    if $game_system.save_disabled
      # Disable save
      @command_window.disable_item(4)
    end
    # Make play time window
    @playtime_window = Window_PlayTime.new
    @playtime_window.x = 0
    @playtime_window.y = 224
    # Make steps window
    @steps_window = Window_Steps.new
    @steps_window.x = 0
    @steps_window.y = 320
    # Make gold window
    @window_info = Window_Info.new
    @window_info.x = 0
    @window_info.y = 416
    # Make status window
    @status_window = Window_MenuStatus.new
    @status_window.x = 160
    @status_window.y = 0
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @command_window.dispose
    @playtime_window.dispose
    @steps_window.dispose
    @window_info.dispose
    @status_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update windows
    @command_window.update
    @playtime_window.update
    @steps_window.update
    @window_info.update
    @status_window.update
    # If command window is active: call update_command
    if @command_window.active
      update_command
      return
    end
    # If status window is active: call update_status
    if @status_window.active
      update_status
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when command window is active)
  #--------------------------------------------------------------------------
  def update_command
    gand_windowinfo_scene_menu_update_command
    if @command_window.index != @current_index
      @current_index = @command_window.index
      case @current_index
      when 0
        text = 'Here you can see which items you have.'
      when 1
        text = 'Here you can see which skills you have.'
      when 2
        text = 'Here you can equip your characters with different weapons/armors.'
      when 3
        text = 'Here you can see information about your characters.'
      when 4
        text = 'Here you can save your progress.'
      when 5
        text = 'Here you can return to title or quit to windows.' 
      end
      @window_info.text  = text
    end
  end
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # If command other than save or end game, and party members = 0
      if $game_party.actors.size == 0 and @command_window.index < 4
        # Play buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Branch by command window cursor position
      case @command_window.index
      when 0  # item
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to item screen
        $scene = Scene_Item.new
      when 1  # skill
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 2  # equipment
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 3  # status
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 4  # save
        # If saving is forbidden
        if $game_system.save_disabled
          # Play buzzer SE
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to save screen
        $scene = Scene_Save.new
      when 5  # end game
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to end game screen
        $scene = Scene_End.new
      end
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when status window is active)
  #--------------------------------------------------------------------------
  def update_status
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Make command window active
      @command_window.active = true
      @status_window.active = false
      @status_window.index = -1
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Branch by command window cursor position
      case @command_window.index
      when 1  # skill
        # If this actor's action limit is 2 or more
        if $game_party.actors[@status_window.index].restriction >= 2
          # Play buzzer SE
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to skill screen
        $scene = Scene_Skill.new(@status_window.index)
      when 2  # equipment
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to equipment screen
        $scene = Scene_Equip.new(@status_window.index)
      when 3  # status
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to status screen
        $scene = Scene_Status.new(@status_window.index)
      end
      return
    end
  end

I really appreciate all your help!
Thanks khmp! :thumb:

Over and out - Gando
 

khmp

Sponsor

Psuedo code and really any code I tend to give out should be taken with a grain of salt. But If I say psuedo code in particular. I mean its not complete, it will most likely crash. It's only meant to be helpful in designing your solution. And almost always when I give out code I intend for it to be in its own section above 'Main' but below the rest of the scripts. It should not just overwrite the default code. Which your error indicates. Here's the link of someone else having that error hopefully the explanation there will be sufficient.

Ok that aside aliasing is like retaining a pointer to the existing code without just destroying it. Preserving the old functionality while still being able to add new functionality. But only to the beginning and end of a method. Which is why some times we override methods instead.

Given the example below I hope it helps you understand a little better what I often fail to describe in words.
Code:
def method
  print 'Bread'
end

alias_method :old_method, :method
def method
  old_method
  print 'is awesome.'
end

method # It will show two text boxes. 'Bread' and 'is awesome'

However you can also do this just to be awesome.
Code:
def method
  print 'Bread'
end
alias_method :bread, :method

bread # prints 'Bread'
method # prints 'Bread'

How is that relative. Well in your code below you alias the method which is great. But then you duplicate the functionality and still alias. And I don't think the code blocks match up either. If else end, def end etc. Here's what I mean:
Gando":wiopsa5y said:
Code:
  #--------------------------------------------------------------------------
  # * Frame Update (when command window is active)
  #--------------------------------------------------------------------------
  def update_command
    gand_windowinfo_scene_menu_update_command
    if @command_window.index != @current_index
      @current_index = @command_window.index
      case @current_index
      when 0
        text = 'Here you can see which items you have.'
      when 1
        text = 'Here you can see which skills you have.'
      when 2
        text = 'Here you can equip your characters with different weapons/armors.'
      when 3
        text = 'Here you can see information about your characters.'
      when 4
        text = 'Here you can save your progress.'
      when 5
        text = 'Here you can return to title or quit to windows.' 
      end
      @window_info.text  = text
    end
  end
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # If command other than save or end game, and party members = 0
      if $game_party.actors.size == 0 and @command_window.index < 4
        # Play buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Branch by command window cursor position
      case @command_window.index
      when 0  # item
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to item screen
        $scene = Scene_Item.new
      when 1  # skill
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 2  # equipment
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 3  # status
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 4  # save
        # If saving is forbidden
        if $game_system.save_disabled
          # Play buzzer SE
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to save screen
        $scene = Scene_Save.new
      when 5  # end game
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to end game screen
        $scene = Scene_End.new
      end
      return
    end
  end

*Hits you with rolled up paper* No, no. It should just be:
Code:
  #--------------------------------------------------------------------------
  # * Frame Update (when command window is active)
  #--------------------------------------------------------------------------
  def update_command
    # Call the old code.
    gand_windowinfo_scene_menu_update_command

    # If the current index does not match the window's current index.
    if @command_window.index != @current_index
      @current_index = @command_window.index
      case @current_index
      when 0
        text = 'Here you can see which items you have.'
      when 1
        text = 'Here you can see which skills you have.'
      when 2
        text = 'Here you can equip your characters with different weapons/armors.'
      when 3
        text = 'Here you can see information about your characters.'
      when 4
        text = 'Here you can save your progress.'
      when 5
        text = 'Here you can return to title or quit to windows.' 
      end
      @window_info.text  = text
    end
  end

Because we aliased that method we already have the functionality of what was previously defined. The pressing c button, and b button, and all that stuff is still called because we aliased the method. No need to paste the same code into that method. However, enough chastising you on this. You are trying and I very much appreciate your efforts.

Keep up the scripting Gando! :thumb:
 
That was very helpful! :thumb:
I looked through all the things you wrote, then i checked out the link you posted,
and then i searched the forum and found an Aliasing Tutorial made by Me(tm) : Aliasing with ease.
And now i understand it a lot more! There was one sentence in Me(tm)'s tutorial that was really helpful:
"Aliasing will help you to ADD things to methods instead of rewriting them."

Until now i kinda had no idea what aliasing did, but combined with what you wrote and Me(tm)'s tutorial, i think i understand it now! :thumb:

Anyways,
I've replaced the "@gold_window = Window_Gold.new"  with "@window_info = Window_Info.new", in the original Scene_Menu.
Then i've replaced the rest of the "@gold_window" with "@window_info".

And then i wrote a new script in a new section above main.
I have played around a bit and i wonder if i'm on the right path or if i'm totally lost :grin:
Could you take a look at it please?
Here it is:
Code:
class Window_Info < Window_Base
  def initialize
    super(0, 0, 160, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
  def refresh
    return if @text.nil?
    self.contents.draw_text(0, 0, 32, 32, text, 0)
  end

  def set_text(text, align = 0)
    # If at least one part of text and alignment differ from last time
    if text != @text or align != @align
      # Redraw text
      self.contents.clear
      self.contents.font.color = normal_color
      self.contents.draw_text(0, 0, 32, 32, text, align)
      @text = text
      @align = align
    end
  end
end

class Scene_Menu
 #alaising update_command so i can add stuff to the old update_command in Scene_Menu right?
  alias gand_windowinfo_scene_menu_update_command update_command
  
  def update_command
  #This calls and includes the old update_command in Scene_Menu right?
    gand_windowinfo_scene_menu_update_command

  #and this is the new stuff that i add in update_command.
    if @command_window.index != @current_index
      @current_index = @command_window.index
      case @current_index
      when 0
        text = 'Here you can see which items you have.'
      when 1
        text = 'Here you can see which skills you have.'
      when 2
        text = 'Here you can equip your characters with different weapons/armors.'
      when 3
        text = 'Here you can see information about your characters.'
      when 4
        text = 'Here you can save your progress.'
      when 5
        text = 'Here you can return to title or quit to windows.' 
      end
      Window_Info.set_text = text
    end
  end
end

As i said, i've played around with it a bit, and it gives me an error at this line:
Code:
 Window_Info.set_text = text


Well, once again, Thanks for all the help khmp!
It's very much appreciated! :smile:

Over and out - Gando
 

khmp

Sponsor

That should be your instance of your Window_Info class which for Scene_Menu according to your own code is called:
Code:
@window_info
So to correct that problem change "Window_Info.set_text"... to "@window_info.set_text"...

Lets introduce some standardized commenting practices. We also no longer need a refresh because you only redraw with the set_text method. Which does a good job. You have it check for changes before redrawing so also, good job. You have the draw_text rect too small. (0, 0, 32, 32). You can be lazy and just do this self.contents.draw_text(0, 0, width, 32, text, align) instead. Which will work and you'll never have to do anything out of the ordinary. But to be precise about text drawing.

Code:
text_rect = self.contents.text_size(text)
self.contents.draw_text(0, 0, text_rect.width, text_rect.height, align)

But what if you're centered or right aligned. Effort comes into play here and its really up to you how far you are willing to go.
Code:
text_rect = self.contents.text_size(text)
case align
when 1 # Center Alignment
  x = width / 2 - text_rect.width / 2
  self.contents.draw_text(x, 0, text_rect.width, text_rect.height, text)
when 2 # Right Alignment
  x = width - text_rect.width
  self.contents.draw_text(x, 0, text_rect.width, text_rect.height, text)
default # Curve balls and Left Alignment
  self.contents.draw_text(0, 0, text_rect.width, text_rect.height, text)
end

But its really up to you. And in coding it always will be.

Final result of Window_Info:
Code:
#==============================================================================
# ** Window_Info
#------------------------------------------------------------------------------
#  This window displays information in Scene_Menu based on current user 
#  selection.
#==============================================================================

class Window_Info < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 160, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
  #--------------------------------------------------------------------------
  # * Set Text
  #      text  : text you want to see in the window.
  #      align : alignment of the text you want to see in the window.
  #--------------------------------------------------------------------------
  def set_text(text, align = 0)
    # If at least one part of text and alignment differ from last time
    if text != @text or align != @align
      # Redraw text
      self.contents.clear
      self.contents.font.color = normal_color
      self.contents.draw_text(0, 0, width, 32, text, align)
      @text = text
      @align = align
    end
  end
end

Now for Scene_Menu. I actually just had an idea about it rather than a case statement inside of update_command. How about a constant Array? We'll place it at the top of Scene_Menu have something like this:
Code:
Info_Lines = [
  'Here you can see which items you have.',
  'Here you can see which skills you have.',
  'Here you can equip your characters with different weapons/armors.',
  'Here you can see information about your characters.',
  'Here you can save your progress.',
  'Here you can return to title or quit to windows.'
]

Then we could just index using @command_window.index. And because you are already checking for whether or not to redraw from within Window_Info's set_text method we don't need the check from within update_command. Which means that code could be shortened to this:
Code:
@window_info.set_text(Info_Lines[@command_window.index])

Bringing Scene_Menu finally to here:
Code:
#==============================================================================
# ** Scene_Menu
#------------------------------------------------------------------------------
#  This class performs menu screen processing.
#==============================================================================

class Scene_Menu
  #--------------------------------------------------------------------------
  # Alias Methods
  #--------------------------------------------------------------------------
  alias gand_windowinfo_scene_menu_update_command update_command
  #--------------------------------------------------------------------------
  # Constant Variables
  #--------------------------------------------------------------------------
  Info_Lines = [
    'Here you can see which items you have.',
    'Here you can see which skills you have.',
    'Here you can equip your characters with different weapons/armors.',
    'Here you can see information about your characters.',
    'Here you can save your progress.',
    'Here you can return to title or quit to windows.'
  ]
  #--------------------------------------------------------------------------
  # * Frame Update (when command window is active)
  #--------------------------------------------------------------------------
  def update_command
    # You can also have new stuff here as well.

    # This calls and includes the old update_command in Scene_Menu right? Yes.
    gand_windowinfo_scene_menu_update_command
    
    # And this is the new stuff that I add in update_command. Yup.
    @window_info.set_text(Info_Lines[@command_window.index])
  end
end

Good luck with it Gando! :thumb:
 
Yaaay! *Jumps up and down*  :lol:
It works perfectly, I owe you one!
Thanks for all the time you've spent trying to make me understand this.

Altough, i have one last question.
I have changed the info window, so now it's bigger so it can show the "info-text".
But if the text should be too long for it to fit in the window, is there a way to display the first half of the text, and then the second half under it.  So instead of this for example:

Code:
'Here you can choose which abillities your character should learn.'
could you somehow show the text like this?:
Code:
'Here you can choose which abillities
 your character should learn.'

because now, i can get the text to be either squashed or make it so the last part wont show cuz it's too long.

Thanks! :smile:
Over and out - Gando
 

khmp

Sponsor

There's a couple things you can do. You can cut the lines up and force the set text to handle arrays of strings. Another way could be splitting the string and then drawing each word until we discover we need to move down a space. Yet another way would be splitting the line by using a special character that defines a new line break. The draw back to the special character split would be that it would need to be moved several times during testing to get where you want it. Also if the window width adjusts you will have to go back and move the character. The bonus to using a special character is easier to program.

Code:
string = 'That's right mister giraffe get all the marmalade.'
words = string.split
p words # ['That's', 'right', 'mister', 'giraffe', 'get', 'all', 'the', 'marmalade.']

or

Code:
string = 'That's right mister giraffe\nget all the marmalade.'
words = string.split('\n')
p words # ['That's right mister giraffe', 'get all the marmalade.']

Now when inside a window we can do something like this:

Code:
def set_text(text, align)
  ... # Other code which I don't want to reiterate

  # The y position of where we are drawing.
  y = 0
  # Split the text into individual words
  words = text.split
  # The string we will draw with.
  draw_string = ''
  
  # Iterate through each word.
  words.each do |word|
    # If the current string plus the word is greater than the width. 
    # Its time to draw the string.
    if self.contents.text_size(draw_string + word).width >= width

      # Draw the line with the words that did fit.
      self.contents.draw_text(0, y, width, 32, draw_string, align)

      # Increment our y position.
      y += 32

      # Reset our string.
      draw_string = ''
    end
    # Throw another word into the string we will draw.
    draw_string += (word + ' ')
  end
end

Actually after writing that code here I wonder if there isn't an easier way. But I think that will work.

Good luck with it Gando! :thumb:
 
Hmm, it just displays the same text twice, for example:

Code:
Here you can choose which abillities your character..
Here you can choose which abillities your character..

do you have any suggestions on how to fix this?
I've been trying to come up with a solution, but so far, no progress..
I'll keep trying, and maybe i will stumble on the answer :tongue2:
Thanks!

Over and out - Gando
 

khmp

Sponsor

Gando":36w29f9v said:
Hmm, it just displays the same text twice, for example:

Code:
Here you can choose which abillities your character..
Here you can choose which abillities your character..

do you have any suggestions on how to fix this?
I've been trying to come up with a solution, but so far, no progress..
I'll keep trying, and maybe i will stumble on the answer :tongue2:
Thanks!

Over and out - Gando

Hmm that's odd. I'll give you the method I've used in the past. Guaranteed. :thumb:
Code:
  #--------------------------------------------------------------------------
  # * Determine Lines Being Drawn.
  #--------------------------------------------------------------------------
  def get_lines(text, width)
    # Create a bitmap to help determine what the size of text we are dealing
    # with.
    text_bitmap = Bitmap.new(1, 1)
    
    #------------------------------------------------------------------------
    # words : The individual words inside the string of text.
    # line  : The current line of text being assembled and tested for size.
    # lines : The reassembled lines constrained by text width.
    #------------------------------------------------------------------------
    words, line, lines = text.split, '', []
    
    # Are we dealing with a multiple word string?
    if words.size > 1
      # Iterate through each word assembling a line of text.
      words.each do |word|
        # If the current line plus the next word pushes us over the
        # allotted width we can work with.
        if text_bitmap.text_size(line + word).width > width
          # If there's actually something in the line
          unless line == ''
            # Append the line into our array.
            lines << line
            # Reset the assembly line.
            line = ''
          end
        end
        # Add the word and a space.
        line += (word + ' ')
      end
      # If we reached here and our array is empty then the words weren't
      # enough to max out the width. So just add the text into the array
      lines << text if lines == []
    else
      # If there was but a single word. Add the word.
      lines << text
    end
    
    # Dispose of our temporary resource used for testing text size.
    text_bitmap.dispose
    
    # Return the array of lines conformed to certain width.
    return lines
  end

Then in your set_text method:
Code:
  def set_text(text, align)
    ... # Other code which I don't want to reiterate

    lines = get_lines(text, width)
  
    y = 0
    lines.each do |line|
      # Draw the line with the words that did fit.
      self.contents.draw_text(0, y, width, 32, line, align)
      y += 32
    end
  end

Add that get_lines method into your Window class. Sorry for the bad code :)

Good luck with it Gando! :thumb:
 

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