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.

Help with Simple PickPocket Script (XP)

After reading some tutorials, its time to practice, right? So i'm creating my first script, a pickpocketing system, but having some issues..
Here is the code:


[rgss]### PickPocket by RamonBastos (first script) ###
 
module Vars #variables
  attr_accessor :directionplayerfacing #The direction the player is looking to
  attr_accessor :directionvictimfacing #Direction the Victim is looking to
  attr_accessor :victimintel  #Victim's Intel (The higher, the hardest to steal)
  attr_accessor :randomconstant #just a random constant
  attr_accessor :items_available_to_steal
  attr_accessor :gold_available_to_steal
  attr_accessor :chancetosteal #the equation
  attr_accessor :pplevel #pickpocket skill level
  attr_accessor :ppexp #pickpocket exp
 
end
 
#If you succeed to steal, this window will be shown
class Steal_Success < Window_Base
  def initialize
    super(220, 190, 128,150)
    self.contents = Bitmap.new(width-32, height-32)
    refresh
    self.contents.font.name = "Arial"
    self.contents.font.size = 24
  end
  def refresh
    self.contents.clear
    self.contents.draw_text(0, 0, 200, 32, "Stole "+@gold_available_to_steal.to_s+" gold") #this line is not working, it does not show the variable value.
    self.contents.draw_text(0, 32, 300, 32, "Gained 1 XP")
  end
end
 
#If you fail to steal, this window will be shown
class Steal_Fail < Window_Base
  def initialize
    super(220, 190, 200,80)
    self.contents = Bitmap.new(width-32, height-32)
    refresh
    self.contents.font.name = "Arial"
    self.contents.font.size = 24
  end
  def refresh
    self.contents.clear
    self.contents.draw_text(0, 0, 200, 32, "You failed to steal")
  end
end
 
#if you lvl your pickpocket skill, this window will be shown
class PPlvlup < Window_Base
  def initialize
    super(220, 230, 200,80)
    self.contents = Bitmap.new(width-32, height-32)
    refresh
    self.contents.font.name = "Arial"
    self.contents.font.size = 24
  end
  def refresh
    self.contents.clear
    self.contents.draw_text(0, 0, 200, 32, "Your Pickpocket skill has increased")
  end
end
 
class PickPocket  < Game_Party
  def initialize
    @gold_available_to_steal = rand(500) #amount of gold the victim has
    @victimintel=2 #victim's intel (the higher, the harder to steal)
    @ppexp=0 #Starts with 0 exp
    @pplevel=1 #Starts at lvl 1
    @randomconstant=rand(101)
    @chancetosteal = @pplevel - @victimintel + @randomconstant #equation
      if @chancetosteal > 6 then #If the resulf of the equation above is higher than six, you stole the victim
        @ppexp +=1  #add one point of PickPocketing Exp
        $game_party.gold += @gold_available_to_steal #add gold
        if @ppexp == 10 then #Condition to lvl up
          @ppexp = 0 #change exp back to 0
          @pplevel += 1 #add one pickpocket lvl
          @message=PPlvlup.new #Shows the window when you lvl up
        end
        @message=Steal_Success.new #Shows the window that tells your reward
      else
        @message=Steal_Fail.new #Shows the window that tells "You Failed"
      end
  end
end
 
 
[/rgss]

To call this script im using an event in the map with the option to steal or not, if you choose yes, it calls PickPocket.new



I want to know:
how to make the fail or success windows to disappear after the player press the confirm button (its something about dispose, but idk how to use it) ,
how to show how much gold you stole .


Also if there is any way to improve this script, because its my frist one ^^

Thanks

(if someone can add me at msn to answer some newbie questions, its ramonzinbastos@hotmail.com, i promise i wont bother u)
 
There are several problems with this script, mainly logic problems but a lot of coding problems as well.

On line 27, you are referencing a variable that doesn't exist (@gold_available_to_steal.) The @ sign denotes a variable specific to the instance of the object. It has never been defined anywhere else in the object, so it would normally return 'Nil' and cause your game to crash. Because you are using .to_s it is converting 'Nil' to the string '' which is just a blank.

I would rewrite the Steal_Success window as follows:

Code:
class Steal_Success < Window_Base

  def initialize(gold)

    super(220, 190, 128,150)

    self.contents = Bitmap.new(width-32, height-32)

    @gold = gold

    self.contents.font.name = "Arial"

    self.contents.font.size = 24

    refresh

  end

  def refresh

    self.contents.clear

    self.contents.draw_text(0, 0, 200, 32, "Stole "+@gold.to_s+" gold")

    self.contents.draw_text(0, 32, 300, 32, "Gained 1 XP")

  end

end

and then on this line:

Code:
@message=Steal_Success.new #Shows the window that tells your reward

replace it with

Code:
@message=Steal_Success.new(@gold_available_to_steal) #Shows the window that tells your reward

Because the windows are being created within the Pickpocket class, instead of within the Scene_Map class, there would be an input problem. The Scene usually handles input, either directly or within an object. There are special handlers that won't update anything but the currently active window until a key is pressed to dispose it. But because your windows are held elsewhere, you would need to reference them in the Scene.

There are a lot of logic errors in the Pickpocket class, though, so you will need to reconsider how that works. The "Vars" module seems to be something you have done because of the way another programming language handles this type of thing. It is not needed in Ruby, and I suggest looking up a guide on how Local/Instance/Global variables work within Ruby, then rewriting your script.
 
I played around with this a little.
First thing I realized was Scene_Map already has a message window, so why not just use it.
2nd thing is Game_Player already handles interaction with events, so I used that as an example.
Rather than have the event call the pickpocket, I triggered it with the Y (S on keyboard) key.
I also intended it to handle items as well as gold, but that part is not implemented.

Keep in mind this is only one way to do it, and should only serve as an example.

Code:
#=====================================================================

# ** Game_Player Pickpocket Mods

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

#  Add a pickpocketing system to Game_Player

#

#  Setup event with a comment:  pp <int> <gold> <loot>

#    <int> is intelligence of event (difficulty factor)

#    <gold> is amount of gold available for stealing

#    <loot> is an array in the form of:

#      [[<type>,<item_id>],...]

#        <type>      0: Item, 1: Weapon, 2:Armor

#        <item_id>   id # from database

#    EX:

#      @>Comment: pp 12 500 0 3 1 34

#      Intelligence: 12

#      Gold: 500

#      Item # 3

#      Weapon # 34

#

#  Enable pickpocketing by turning on Switch: PICKPOCKET_SWITCH

#====================================================================

 

class Game_Player

  

  PICKPOCKET_SWITCH = 10   # Enable/Disable Pickpocketing

  

  alias pickpocket_update update

  alias pickpocket_init initialize

  

  def initialize

    pickpocket_init  # call original initialize method

    # initialize exp & lvl 

    @pp_exp = 0

    @pp_lvl = 0  # could also be a formula based on pp_exp

  end

 

  def update

    pickpocket_update     # call original update method

    # If Y (S) Key and Pickpocket switch is on

    if Input.trigger?(Input::Y) and $game_switches[PICKPOCKET_SWITCH]

      # If Event in front is 'pickpocket-able'

      if params = check_event_pickpocket_there

        # Call Pickpocket.attempt_pick

        attempt_pick(params)

      # Else play buzzer

      else

        # Play buzzer SE

        $game_system.se_play($data_system.buzzer_se)

      end

    end

  end

  

  # Checks if there is an event in front of the player

  # and if so, returns an array of parameters

  def check_event_pickpocket_there

    result = false

    # Calculate front event coordinates

    new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)

    new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)

    # loop All events

    for event in $game_map.events.values

      # If event coordinates and triggers are consistent

      # and the first event command is a comment (code 108)

      if event.x == new_x and event.y == new_y and

      event.list[0].code == 108

        # Copy the text from the comment

        comment = event.list[0].parameters[0].dup

        # Check & remove "PP" or "pp" from the string

        if comment.sub!(/[Pp][Pp]/) {} != nil

          params = [event.id]

          params.push event.direction

          #convert comment 'words' to array and append to params

          params += comment.scan(/\w+/)  

          return params

        end

      end

    end

    # If no event in front of player, or event has no comment,

    # or the comment doesn't contain "PP", return false

    return false

  end

  

  def attempt_pick(params) # direction, int, gold, item pairs

    # note that direction & int are integers, the rest is strings

    event_dir = params[1]

    event_int = params[2].to_i

    event_gold = params[3].to_i

    chancetosteal = @pp_lvl - event_int + rand(101) #equation

    #If the result is higher than six, you stole the victim

    if chancetosteal > 6 then 

      message= "Stole #{event_gold.to_s} Gold" # set message string

      $game_party.gain_gold(event_gold)

      # Set the event's self switch A to ON

        # Make a self switch key

        key = [$game_map.map_id, params[0], "A"]

        $game_self_switches[key] = true

        $game_map.need_refresh = true

      @pp_exp +=1  #add one point of PickPocketing Exp

      if @pp_exp == 10 then #Condition to lvl up

        @pp_exp = 0 #change exp back to 0

        @pp_lvl += 1 #add one pickpocket lvl

        # append to message string

        message += "\nYour Pickpocket skill has increased" 

      end

    else

      message="You failed to steal" #Shows "You Failed"

    end

    

    # Use the message window to show the message

    $game_temp.message_text = message

    

  end

 

end

things I would change: I just used your 'equation'. Right now there is a 94% chance of success at level 0. (perhaps that was just for testing)
I'm not sure what your max_level was intended to be, but I would probably make level a function of exp, and use a shallow exponential curve to determine level.

Try to keep code & comments at least 6 characters from the right margin (grey line). Text going off the edge of the editor is hard to read, and some
forums code or codebox tags wrap lines at around 70 characters.

I would probably randomize whether the player gets gold or an item, and only one item out of what's available.

I would also set a different self switch, or a game switch if the player failed so the event could 'respond' to a failed attempt. i.e. yell at, or fight the player.

Maybe add an actual "Pickpocketing" skill, and have the script check to see if the 'thief' party member is in the party, and has the skill.

just thoughts.

Let me know if anything in the script doesn't make sense.

Be Well
 

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