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.

[Resolved] looping problem

Status
Not open for further replies.
I have a problem with 'for i in...' statements. My problem is that the game is deleting all of the sprites drawn by the 'for i in' statement instead of only deleting the one I want... i = event_id (basically)

Code:
if @shields.size >= 1
      for i in 0...50
        if @shields[i] != ""

Code:
              if $game_map.events[i].erased == true
                @shields[i].bitmap.dispose if @shields[i] != ""
                @shields[i].dispose if @shields[i] != ""
                @shields[i] = "" if @shields[i] != ""
                @time[i] = 0
                $game_map.events[i].variables[5] = 0
                return
              end

If you see the problem please tell me. If you need anymore information I'll be glad to give it.
 
Logically, the erased flag must being  set to true somewhere for all your events. Either that, or the deletion is happening elsewhere. One of the two.

Have you tried putting  something like

Code:
print "i =   #{i}: #{$game_map.events[i].erased}"

in front of the second "if"? It would at least let you see what was going on.
 
Code:
for shield in @shields
   #This MIGHT need to read if shield != nil and $game_map.events[shield.id]
   if shield != "" and $game_map.events[shield.id].erased
      shield.bitmap.dispose
      $game_map.events[shield.id].variables[5] = 0
      return
   end
end

That looks a little cleaner, and may work for you. If shield is not empty (i'm not sure what type of object you're dealing with here) and i am assuming each shield has a unique id identifier, but this should delete the bitmap if your stuff is set up properly
 
TywinLannister2":2mdchxth said:
Code:
for shield in @shields
   #This MIGHT need to read if shield != nil and $game_map.events[shield.id]
   if shield != "" and $game_map.events[shield.id].erased
      shield.bitmap.dispose
      $game_map.events[shield.id].variables[5] = 0
      return
   end
end

That looks a little cleaner, and may work for you. If shield is not empty (i'm not sure what type of object you're dealing with here) and i am assuming each shield has a unique id identifier, but this should delete the bitmap if your stuff is set up properly

I dunno what it is. It just isn't working... @shields is a Hash.new(""). That is later turned into a sprite. The id of the hash is equal to the event that spawns the shield's id.
 

e

Sponsor

After cleaning up the whole thing that you gave me and commenting it, here's what I got :

Code:
# If there is something in the @shields Hash
unless @shields.empty?
  # Do the following 50 times...! (0 to 49)
  for i in 0...50
    # Basically, if there is a shield defined (not "")
    if @shields[i] != ""
      # Collision test, I assume
      unless $game_map.screen.pictures[1].x_equals.between?(@shields[i].x - 5, @shields[i].x + @shields[i].width + 5) and 
               $game_map.screen.pictures[1].y_equals.between?(@shields[i].y - 5, @shields[i].y + @shields[i].height + 5)
        # collision test again?
        if $game_map.screen.pictures[2].x_equals.between?(@shields[i].x - 5, @shields[i].x + @shields[i].width + 5) and 
           $game_map.screen.pictures[2].y_equals.between?(@shields[i].y - 5, @shields[i].y + @shields[i].height + 5)
          # Game switches 2 is set to false, whatever that is
          $game_switches[2] = false

          # We remove picture 2
          $game_map.screen.pictures[2].erase2
          
          # If we have a score, decrement it by 1 and refresh the HUD window
          unless $game_party.score.nil?
            $game_party.score -= 1
            @hud_window.refresh
          end
        end
    end
        
    # If it's immortal
    if @time[i].nil?
      # If the event has been erased do
      if $game_map.events[i].erased
        # Unless there is nothing defined in @shields[i], dispose of the bitmap, then of the event, then set to ""
        unless @shields[i] == ""
          @shields[i].bitmap.dispose
          @shields[i].dispose
          @shields[i] = ""
        end
        
        # Time is now set at 0 and we return to avoid any more problems
        @time[i] = 0
        $game_map.events[i].variables[5] = 0
        return
      end
    # Else if we still have some time left
    elsif @time[i] > 0
      # If it's been erased, we should dispose of it
      if $game_map.events[i].erased
        # Dispose of the bitmap and the event only if there is something to dispose (and not "")
        unless @shields[i] == ""
          @shields[i].bitmap.dispose
          @shields[i].dispose
          @shields[i] = ""
        end

        # Keep multiple shields from appearing
        $game_map.events[i].variables[5] = 0
        return
      end
      
      # Decrement the time remaining by one
      @time[i] -= 1
      
    # Else if we have no time left and there is a time limit
    else
      # If there is something to dispose of, dispose of it!
      unless @shields[i] == ""
        @shields[i].bitmap.dispose
        @shields[i].dispose
        @shields[i] = ""
      end
    
      # No more time (in case it was < 0)
      @time[i] = 0
    
      $game_map.events[i].variables[5] = 0
      return
    end
  end
end

Tell me if I understood the gist of it, so I may better help you out.
 

loam

Member

etheon":1kefm9i6 said:
Code:
# If there is something in the @shields Hash
unless @shields.empty?
  # Do the following 50 times...! (0 to 49)
  for i in 0...50
    # Basically, if there is a shield defined (not "")
    if @shields[i] != ""
      # Collision test, I assume
      unless $game_map.screen.pictures[1].x_equals.between?(@shields[i].x - 5, @shields[i].x + @shields[i].width + 5) and 
               $game_map.screen.pictures[1].y_equals.between?(@shields[i].y - 5, @shields[i].y + @shields[i].height + 5)
        # collision test again?
        if $game_map.screen.pictures[2].x_equals.between?(@shields[i].x - 5, @shields[i].x + @shields[i].width + 5) and 
           $game_map.screen.pictures[2].y_equals.between?(@shields[i].y - 5, @shields[i].y + @shields[i].height + 5)
          # Game switches 2 is set to false, whatever that is
          $game_switches[2] = false

          # We remove picture 2
          $game_map.screen.pictures[2].erase2
          
          # If we have a score, decrement it by 1 and refresh the HUD window
          unless $game_party.score.nil?
            $game_party.score -= 1
            @hud_window.refresh
          end
        end
    end
        
    # If it's immortal
    if @time[i].nil?
      # If the event has been erased do
      if $game_map.events[i].erased
        # Unless there is nothing defined in @shields[i], dispose of the bitmap, then of the event, then set to ""
        unless @shields[i] == ""
          @shields[i].bitmap.dispose
          @shields[i].dispose
          @shields[i] = ""
        end
        
        # Time is now set at 0 and we return to avoid any more problems
        @time[i] = 0
        $game_map.events[i].variables[5] = 0
        return
      end
    # Else if we still have some time left
    elsif @time[i] > 0
      # If it's been erased, we should dispose of it
      if $game_map.events[i].erased
        # Dispose of the bitmap and the event only if there is something to dispose (and not "")
        unless @shields[i] == ""
          @shields[i].bitmap.dispose
          @shields[i].dispose
          @shields[i] = ""
        end

        # Keep multiple shields from appearing
        $game_map.events[i].variables[5] = 0
        return
      end
      
      # Decrement the time remaining by one
      @time[i] -= 1
      
    # Else if we have no time left and there is a time limit
    else
      # If there is something to dispose of, dispose of it!
      unless @shields[i] == ""
        @shields[i].bitmap.dispose
        @shields[i].dispose
        @shields[i] = ""
      end
    
      # No more time (in case it was < 0)
      @time[i] = 0
    
      $game_map.events[i].variables[5] = 0
      return
    end
  end
end

Should there be another

Code:
if $game_map.events[i].erased

on the else? Right now, the method is called the first time, and if the event has no time variable (I'm assuming meaning time left on invulnerability?), it is created and set to 0. The next time the method is called, the event sprite is automatically deleted (if it exists) because it now fits into the else.

It's really hard to wrap my head around the method without the rest of the script. :dead: But that looked odd so I thought I'd try, just in case.

Is there a reason "" is used instead of nil? Just wondering.
 
Status
Not open for further replies.

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