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.

[XP] Window_BattleResult Conditional

Basically, what I'm trying to do is get the window for battle results (EXP earned, Gold earned, and Treasure) to only display if there's anything to display. I've eliminated EXP entirely (as this is a part of a Level/EXP Eliminator script for my Ability Grid that I'm editing), so I only need to worry about gold and treasure. The obvious method (to me) to do this is to use conditional branches, which you'll see at lines 18 and 31.

[rgss]#==============================================================================
# ** Window_BattleResult
#------------------------------------------------------------------------------
#  This window displays amount of gold and EXP acquired at the end of a battle.
#==============================================================================
 
class Window_BattleResult < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     exp       : EXP
  #     gold      : amount of gold
  #     treasures : treasures
  #--------------------------------------------------------------------------
  def initialize(exp, gold, treasures)
    @exp = exp
    @gold = gold
    @treasures = treasures
    if @gold > 0 or @treasures.size > 0
      super(240, 0, 160, @treasures.size * 32 + 64)
      self.contents = Bitmap.new(width - 32, height - 32)
      self.y = 160 - height / 2
      self.back_opacity = 160
      self.visible = false
      refresh
    end
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    if @gold > 0 or @treasures.size > 0
      self.contents.clear
      x = 4
      self.contents.font.color = normal_color
      cx = contents.text_size(@gold.to_s).width
      self.contents.draw_text(x, 0, cx, 32, @gold.to_s)
      x += cx + 4
      self.contents.font.color = system_color
      self.contents.draw_text(x, 0, 128, 32, $data_system.words.gold)
      y = 32
      for item in @treasures
        draw_item_name(item, 4, y)
        y += 32
      end
    end
  end
end
[/rgss]

I'm thinking about adding another conditional (so it'll only show treasures if no coins are gained), but at the moment I've hit a snag. But it's not with that piece of script, which appears to work as I wanted it to.
scenebattle2error.png


That is line 19 here:
[rgss]#==============================================================================
# ** Scene_Battle (part 2)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================
 
class Scene_Battle
  #--------------------------------------------------------------------------
  # * Frame Update (after battle phase)
  #--------------------------------------------------------------------------
  def update_phase5
    # If wait count is larger than 0
    if @phase5_wait_count > 0
      # Decrease wait count
      @phase5_wait_count -= 1
      # If wait count reaches 0
      if @phase5_wait_count == 0
        # Show result window
        @result_window.visible = true
        # Clear main phase flag
        $game_temp.battle_main_phase = false
        # Refresh status window
        @status_window.refresh
      end
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Battle ends
      battle_end(0)
    end
  end
end
 
[/rgss]

I'm assuming I'd just need to throw in a conditional branch there, but the problem is that I'm not sure what to reference because @gold and @treasures.size aren't defined in this class. I've tried a few things, but I always end up creating different errors, so I thought I'd ask someone who knows what they're doing.

If you can explain how to do this, it would be very much appreciated. I'm trying to learn RGSS at least a little, so I'd rather have an explanation than just be handed a script. :P
 
First, I would check in Scene_Battle (2) where your results window is displayed.

Code:
  #--------------------------------------------------------------------------

  # * Frame Update (after battle phase)

  #--------------------------------------------------------------------------

  def update_phase5

    # If wait count is larger than 0

    if @phase5_wait_count > 0

      # Decrease wait count

      @phase5_wait_count -= 1

      # If wait count reaches 0

      if @phase5_wait_count == 0

        # Show result window

        @result_window.visible = true

        # Clear main phase flag

        $game_temp.battle_main_phase = false

        # Refresh status window

        @status_window.refresh

      end

      return

    end

    # If C button was pressed

    if Input.trigger?(Input::C)

      # Battle ends

      battle_end(0)

    end

  end

Ok. So when the timer strikes 0 (100 frames have passed), the gold window is displayed and then you wait for the C trigger to move on to battle_end processing.

I suggest instead adding a method into Window_BattleResults that lets you know if it even needs to be displayed.
Code:
class Window_BattleResult

  def display?

    return @exp > 0 || @gold > 0 || @treasures.size > 0

  end

end

Right now that method will return true if the any exp, gold or treasures were gained (you can remove @exp > 0 || if you don't want it to display if exp was gained).

Now in Scene_Battle#update_phase5, where it makes the window visible, add your conditional there on whether to make the window visible or not.

Code:
        # Show result window

        @result_window.visible = @result_window.display

Now the window will be visible only when there are results to be shown.


Hope that helps ya. Happy scripting.
 
OK I'm pretty sure I understand what's happening and it's working, but I have some questions if you don't mind answering them. :P

I don't fully understand what "return" does. I see how it works with the method I created - returning the method as true or false (I think?), but it's also in update_phase5 and I'm not sure what its purpose is there. From the looks of things, it's simply re-checking the conditional branch to check the timer, but then it's acting differently. So I could probably use some clarification on that. XD

And what is ||? I remember reading about that once when I was going through RGSS in an attempt to learn it, but I've completely forgotten what it does now. :x

Thanks for helping me out with this! Some things still confuse me (and it took me quite a bit of re-reading what you said to understand it), but I'm trying to learn.
 
Return simply returns a value back to the original caller. This value can be anything, or nothing at all. It also prevents anything from being executed within the current block or method. Such as
Code:
def something

  p 1

  return

  p 2

end

Calling something would only yield 1 being printed because the method ended and the "p 2" line never was reached.

Or you could have something like this:
Code:
def something

  return 5

end

 

a = something

something

Now a would get the value 5 from that method and assign 5 to your a variable. The second "something" line would call the method, but not assigned to anything. No error would be returned.

I also included a ? at the end of the method. Query methods, methods that return true or false, are best named with a ? at the end just to follow typical standards.

In update_phase5 as you asked, the return method simply stops the rest of the method from being executed. This is used quite a bit and extremely useful. Say you have a list of something, and you are checking if a value is within a certain list and returning the current position within the list. However if nothing if ever found, it returns a default value. You would have something like this:
Code:
def find_something(list, object)

  for i in 0...list.size

    if list[i] == object

      return i

    end

  end

  return nil

end

 

array1 = [0, 1, 2, 3, 4]

array2 = [5, 6, 7, 8, 9]

 

a = find_something(array1, 2)

b = find_something(array1, 7)

c = find_something(array2, 2)

d = find_something(array2, 5)

In the above example, a would = 2, b would = nil, c would = nil, d would = 2. It check each object at a certain index and returns that index if the object equals the testing object. If it isn't found within that block, it returns nil. Once the object is found, the block and method stop executing. Say in a, the it checks array1 for each object; starting with 0, then 1, and when it gets to 2, it returns the index 2 is at. It does not check the 3 or 4, nor does it get to the final line where it returns end.

|| is the same as writing or, just as && is the same as writing and. Its just a logic used with expressions.


If you are trying to learn, feel free to use this thread to ask me anything. I'll do my best to answer any question you have about anything.
 
Oh wow thanks for all your help! You've answered all my questions for now, but I'll probably be doing some scripting things here and there so I'll be sure to ask if I need help with anything. :)
 

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