help with deleting array from array

Started by diagostimo, August 31, 2012, 07:57:03 pm

Previous topic - Next topic

diagostimo

hey there, i have been making a script to manage pushing movable object, the main part of it is changing the events previous x and y as unpassable until the event has finished moving, this is so that the event cant be spammed and so the graphics doesnt overlap the player while its moving to the next tile, heres my script:

class Interpreter
  #--------------------------------------------------------------------------
  # * push_object(event to push)
  #--------------------------------------------------------------------------
  def push_object(event_id)
    #set event to push
    event = $game_map.events[event_id]
    # store x and y
    old_x, old_y = event.x, event.y
    #push the info into array
    $game_system.movable_object.push([event_id, old_x, old_y])
    p $game_system.movable_object
    #p $game_system.movable_object
    #event move down if player direction is 2 and event can enter the tile
    event.move_down if $game_player.direction == 2 &&
      $game_map.passable?(event.x, event.y + 1, 8)
    #event move left if player direction is 4 and event can enter the tile
    event.move_left if $game_player.direction == 4 &&
      $game_map.passable?(event.x - 1, event.y, 6)
    #event move right if player direction is 6 and event can enter the tile
    event.move_right if $game_player.direction == 6 &&
      $game_map.passable?(event.x + 1, event.y, 4)
    #event move up if player direction is 8 and event can enter the tile
    event.move_up if $game_player.direction == 8 &&
      $game_map.passable?(event.x, event.y - 1, 2)
    return unless event.moving?
    #play the push sound if the event is moving
    $game_system.se_play(RPG::AudioFile.new("bump"))
  end
end
#
#
#
class Game_System
  attr_accessor :movable_object
  alias init_push initialize
  def initialize
    init_push
    @movable_object = []
  end
end
#
#
#
class Game_Player
  def passable?(x, y, d)
    # Get new coordinates
    new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
    new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)



    array = $game_system.movable_object
    p array
    for event in $game_map.events.values
      #p array
      if array.include?([event.id, new_x, new_y])
        return false if event.moving?
        array.delete([event.id, new_x, new_y) if $game_map.events[event.id].moving? == false
      end
    end



    # If coordinates are outside of map
    unless $game_map.valid?(new_x, new_y)
      # Impassable
      return false
    end
    # If debug mode is ON and ctrl key was pressed
    if $DEBUG and Input.press?(Input::CTRL)
      # Passable
      return true
    end
    super
  end
end

this code works fine, my main question is about how im deleting the array, at the moment its only deleting the array if i am entering the tile that was locked, i think the best way would be to delete any array if the asociated event is not moving, i did try it like this:

for i in 0...array.size
          array.delete(array[i]) if array[i][0] == event.id &&
            $game_map.events[event.id].moving? == false
        end

but that didnt work :(

KK20

Just out of curiosity, is there any way why you can't just pause the player from moving for a few frames (a la Pokemon boulder puzzles)?

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!

diagostimo

August 31, 2012, 08:38:55 pm #2 Last Edit: August 31, 2012, 08:57:01 pm by diagostimo
i did actually do that before doing this by getting it to void $game_player.update if the pushing flag was true, on that note what method do you think is better and faster? i implemented this so there wouldnt be a freeze in movement and the player could move to other tiles and move other objects while the object is moving

edit:

for i in 0...array.size
  array.delete(array[i]) if array[i][0] == event.id &&
    $game_map.events[event.id].moving? == false
end

that actually worked, the problem was i was putting it inside the "if array.include?([event.id, new_x, new_y])" condition, so it wasnt deleting it until i went to those coordinates, i should probably add it to an update function  :roll:

KK20

Personally, I would have modified def passable? in either Game_Character or Game_Player. While checking each event in a loop, compare real_x and real_y values. Something like
if (event.real_x - new_x*128).abs < 128 and (event.real_y - new_y*128).abs < 128
  return false
end
Also, you didn't have to void out $game_player.update. You could have just put a Move Route within the pushing event and put a Wait of 10 frames on the player.

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!

diagostimo

Quote from: KK20 on August 31, 2012, 09:23:13 pm
Personally, I would have modified def passable? in either Game_Character or Game_Player.

what i did modify it in Game_Player  :^_^': also wow, i have no idea how it works but that code snipet is really good, that poops all over my array :P well i sorta get how it works, it checking the sprite coordinates right?

KK20

Think of real_x and real_y as where the graphic is drawn at (at least that's how I think of it as--I know it's wrong but it makes it easier for me to understand it and explain it). Each tile is 128 by 128 in the 'real' values. So, yes, it is essentially checking the sprite coordinates. It helps to prevent sprite overlap when one character is moving onto the tile at the same time another character is leaving the same tile.

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!

diagostimo