The RPG Maker Resource Kit

RMRK RPG Maker Creation => VX => VX Scripts Database => Topic started by: SeMcDun on October 30, 2011, 03:04:12 AM

Title: Keypad v1.0
Post by: SeMcDun on October 30, 2011, 03:04:12 AM
Keypad
Version: 1.0
Author: SeMcDun
Date: 10.30.11

Version History




Planned Future Versions


Description


I know this could easily be done by eventing, but I wanted to make a Keypad to add some feeling :)
(Also more scripting experience heh)
A simple keypad script which enables the creator to have keypads with numerical locks in their game.
Key Codes are 4 digits long.
When a key code is entered correctly, a switch is turned on allowing the creator to set chests to open etc etc.

Features


Screenshots

(https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg510.imageshack.us%2Fimg510%2F3786%2Fkeypad.png&hash=e4ab9f7d9b9c9b61bf1c96104f925d36d46fbe5b)

Instructions

All instructions should be near the top of the script. If there's anything you do not understand, feel free to post :)
I'll try help when I can.

Script



[/list]#=begin

#===============================================================================
# Keypad VX
# v1.0
# By SeMcDun
# Started: 28/10/2011
# Last Update: 30/10/2011
#===============================================================================
#
#===============================================================================
# Version History;
#
# v1.0
#-- A keypad. Digits 0-9.
#-- Codes are 4 digits long.
#-- Entering a correct code turns ON a switch.
#===============================================================================
# HOW TO USE:
#
# Calling the script;
#-- Set your designated variable to the index of the store you wish to call.
#-- Use $scene = Scene_Keypad.new to call the keypad.
#
# Setting up a Keypad.
#-- Create a new $Keypad_Codes variable using the formula given below :)
#-- Set up an event which is to call the keypad scene.
#-- Set your designated variable ($Keypad_Variable) to the index of the
#   $Keypad_Codes[x] you want to use.
#-- Call the scene.
#
#===============================================================================
# NOTE:
# If using a different Icon Set, search for "# Draw Icons" and edit x.
# draw_icon(x,y,z,enabled=true)
# This is where the number icons are drawn.
#===============================================================================
# * Module_Keypad_SeM
#===============================================================================
module Module_Keypad_SeM
  #==============================================#
  # Variable used when assigning a keypad a code #
  #==============================================#
  # If this is set to 5 you must change the variable 0005 to the number
  # of Keypad Code you want to use.
  $Keypad_Variable = 5
 
  #============================================#
  # This variable is the array of Keypad codes #
  #============================================#
  $Keypad_Codes = [[]] # Do not touch
 
  # $Keypad_Codes[x] = ["abcd",e]
  # x = Code index
  # a = First digit in your code.
  # b = Second digit in your code.
  # c = Third digit in your code.
  # d = Fourth digit in your code.
  # e = Switch to be turned on.
 
  # Edit below;
  $Keypad_Codes[1] = ["1001",10]
  $Keypad_Codes[2] = ["2353",11]
 
 
 
  # Stop editing.
 
  $Entered_Code = []
  $Code_Counter = 0
 
end # Module_Keypad_SeM
#-------------------------------------------------------------------------------
#===============================================================================
# * Scene_Keypad
#===============================================================================
class Scene_Keypad < Scene_Base
  def initialize
    Sound.play_save
    $Code_Counter = 1
    $Entered_Code[1] = nil
    $Entered_Code[2] = nil
    $Entered_Code[3] = nil
    $Entered_Code[4] = nil
    create_windows
    update
  end # initialize

  def update
    @Keypad_product.refresh
    @Keypad_UI.refresh
    key_input
  end # update
 
  def refresh_Keypad
    $Keypad_Cursor.refresh
  end # refresh_Keypad
 
  def key_input
    if Input.trigger?(Input::C)
      input_c
      refresh_Keypad
      return
    end # if input C
    if Input.trigger?(Input::B)
      input_b
      refresh_Keypad
      return
    end # if input B
    if Input.trigger?(Input::UP)
      input_up
      refresh_Keypad
      return
    end # if input UP
    if Input.trigger?(Input::LEFT)
      input_left
      refresh_Keypad
      return
    end # if input LEFT
    if Input.trigger?(Input::DOWN)
      input_down
      refresh_Keypad
      return
    end # if input DOWN
    if Input.trigger?(Input::RIGHT)
      input_right
      refresh_Keypad
      return
    end # if input RIGHT
  end # key_input
 
  def create_windows
    @keypad_ui_port = Viewport.new(0,0,544,416)
    @Keypad_UI = Window_Keypad.new
    @Keypad_UI.viewport = @keypad_ui_port
    @keypad_product_port = Viewport.new(0,0,544,416)
    @keypad_product_port.z += 10
    @Keypad_product = Window_Keypad_Product.new
    @Keypad_product.viewport = @keypad_product_port
   
  end # create_windows
 
  def terminate
    $Keypad_Product.dispose
    $Keypad_Cursor.dispose
    @Keypad_UI.dispose
    @Keypad_product.dispose
=begin
    $Keypad_1.dispose
    $Keypad_2.dispose
    $Keypad_3.dispose
    $Keypad_4.dispose
    $Keypad_5.dispose
    $Keypad_6.dispose
    $Keypad_7.dispose
    $Keypad_8.dispose
    $Keypad_9.dispose
    $Keypad_0.dispose
=end
  end # terminate
 
  def input_b
    Sound.play_load
    $scene = Scene_Map.new
  end # input_b
 
  def input_left
    Sound.play_cursor
    if $Key_Coords_x == 0
      # It's at the end. Do nothing
    else
      $Key_Coords_x -= 1
    end #
  end # input_left
 
  def input_right
    Sound.play_cursor
    if $Key_Coords_x == 2
      # It's at the end. Do nothing
    else
      $Key_Coords_x += 1
    end #
  end # input_right
 
  def input_up
    Sound.play_cursor
    if $Key_Coords_y == 0
      # It's at the end. Do nothing
    else
      $Key_Coords_y -= 1
    end #
  end # input_up
 
  def input_down
    Sound.play_cursor
    if $Key_Coords_y == 3
      # It's at the end. Do nothing
    else
      $Key_Coords_y += 1
    end #
  end # input_down
 
  def input_c
    if $Key_Coords_x == 0
      if $Key_Coords_y == 0
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "1"
          $Code_Counter += 1
        end
      end
    end # 0,0
    if $Key_Coords_x == 1
      if $Key_Coords_y == 0
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "2"
          $Code_Counter += 1
        end
      end
    end # 1,0
    if $Key_Coords_x == 2
      if $Key_Coords_y == 0
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "3"
          $Code_Counter += 1
        end
      end
    end # 2,0
    if $Key_Coords_x == 0
      if $Key_Coords_y == 1
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "4"
          $Code_Counter += 1
        end
      end
    end # 0,1
    if $Key_Coords_x == 1
      if $Key_Coords_y == 1
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "5"
          $Code_Counter += 1
        end
      end
    end # 1,1
    if $Key_Coords_x == 2
      if $Key_Coords_y == 1
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "6"
          $Code_Counter += 1
        end
      end
    end # 2,1
    if $Key_Coords_x == 0
      if $Key_Coords_y == 2
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "7"
          $Code_Counter += 1
        end
      end
    end # 0,2
    if $Key_Coords_x == 1
      if $Key_Coords_y == 2
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "8"
          $Code_Counter += 1
        end
      end
    end # 1,2
    if $Key_Coords_x == 2
      if $Key_Coords_y == 2
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "9"
          $Code_Counter += 1
        end
      end
    end # 2,2
    if $Key_Coords_x == 0
      if $Key_Coords_y == 3
        Sound.play_cancel
        # Reset
        $Code_Counter = 1
        $Entered_Code[1] = nil
        $Entered_Code[2] = nil
        $Entered_Code[3] = nil
        $Entered_Code[4] = nil
      end
    end # 0,3
    if $Key_Coords_x == 1
      if $Key_Coords_y == 3
        # Check it ISN'T on the 4th number already.
        if $Code_Counter == 5
          # Buzzer
        else
          Sound.play_decision
          $Entered_Code[$Code_Counter] = "0"
          $Code_Counter += 1
        end
      end
    end # 1,3
    if $Key_Coords_x == 2
      if $Key_Coords_y == 3
        # Get the entered code.
        @Player_Code = "#{$Entered_Code[1]}#{$Entered_Code[2]}#{$Entered_Code[3]}#{$Entered_Code[4]}"
        @switch_finder = $Keypad_Codes[$game_variables[$Keypad_Variable]]
        if @Player_Code == @switch_finder[0]
          # THEY ARE THE SAME!
          $game_switches[@switch_finder[1]] = true
          Sound.play_recovery
          $Code_Counter = 1
          $Entered_Code[1] = nil
          $Entered_Code[2] = nil
          $Entered_Code[3] = nil
          $Entered_Code[4] = nil
          $scene = Scene_Map.new
        else
          Sound.play_buzzer
          $Code_Counter = 1
          $Entered_Code[1] = nil
          $Entered_Code[2] = nil
          $Entered_Code[3] = nil
          $Entered_Code[4] = nil
        end
      end
    end # 2,3
   
   
  end # input_c
   
   
   
end # Scene_Keypad
#-------------------------------------------------------------------------------
#===============================================================================
# * Window_Keypad
#===============================================================================
class Window_Keypad < Window_Base
  def initialize
    super(218,145,128,160)
    create_contents
    create_sprites
    refresh
  end # initialize

  def refresh
   
   
  end # refresh
 
  def create_sprites
=begin
    # Now the keys
    $Keypad_x = 0
    $Keypad_y = 0
    $Keypad_1 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 1
    $Keypad_y = 0
    $Keypad_2 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 2
    $Keypad_y = 0
    $Keypad_3 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 0
    $Keypad_y = 1
    $Keypad_4 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 1
    $Keypad_y = 1
    $Keypad_5 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 2
    $Keypad_y = 1
    $Keypad_6 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 0
    $Keypad_y = 2
    $Keypad_7 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 1
    $Keypad_y = 2
    $Keypad_8 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 2
    $Keypad_y = 2
    $Keypad_9 = Sprite_Keypad_Key.new(@keypad_Product_port)
   
    $Keypad_x = 1
    $Keypad_y = 3
    $Keypad_0 = Sprite_Keypad_Key.new(@keypad_Product_port)
=end   

    $Key_Coords_x = 0
    $Key_Coords_y = 0
    $Keypad_Cursor = Sprite_Keypad_Select.new(@keypad_Product_port)

    # Draw icons
    # 1
    $Keypad_x = 0
    $Keypad_y = 0
    draw_icon(8030,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 2
    $Keypad_x = 1
    $Keypad_y = 0
    draw_icon(8031,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 3
    $Keypad_x = 2
    $Keypad_y = 0
    draw_icon(8026,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 4
    $Keypad_x = 0
    $Keypad_y = 1
    draw_icon(8027,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 5
    $Keypad_x = 1
    $Keypad_y = 1
    draw_icon(8028,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 6
    $Keypad_x = 2
    $Keypad_y = 1
    draw_icon(8024,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 7
    $Keypad_x = 0
    $Keypad_y = 2
    draw_icon(8025,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 8
    $Keypad_x = 1
    $Keypad_y = 2
    draw_icon(8010,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 9
    $Keypad_x = 2
    $Keypad_y = 2
    draw_icon(8011,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # 0
    $Keypad_x = 1
    $Keypad_y = 3
    draw_icon(8034,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # Ok
    $Keypad_x = 2
    $Keypad_y = 3
    draw_icon(516,5+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
    # Cancel
    $Keypad_x = 0
    $Keypad_y = 3
    draw_icon(517,4+($Keypad_x * 32),4+($Keypad_y * 32),enabled = true)
   
  end # create_sprites
 
end # Window_Keypad
#-------------------------------------------------------------------------------
#===============================================================================
# * Sprite_Keypad_Product
#===============================================================================
class Sprite_Keypad_Product < Sprite_Base
  def initialize(viewport = nil)
    super
    self.bitmap = Cache.system("Keypad_Product")
    self.x = 226
    self.y = 106
  end # initialize
end # Sprite_Keypad_Product
#-------------------------------------------------------------------------------
#===============================================================================
# * Sprite_Keypad_Key
#===============================================================================
class Sprite_Keypad_Key < Sprite_Base
  def initialize(viewport = nil)
    super
    self.bitmap = Cache.system("Keypad_Key")
    self.x = 234+($Keypad_x * 32)
    self.y = 161+($Keypad_y * 32)
  end # initialize
end # Sprite_Keypad_Key
#-------------------------------------------------------------------------------
#===============================================================================
# * Sprite_Keypad_Select
#===============================================================================
class Sprite_Keypad_Select < Sprite_Base
  def initialize(viewport = nil)
    super
    self.bitmap = Cache.system("Keypad_Select")
    self.x = 234+($Key_Coords_x * 32)
    self.y = 161+($Key_Coords_y * 32)
  end # initialize
 
  def refresh
    self.x = 234+($Key_Coords_x * 32)
    self.y = 161+($Key_Coords_y * 32)
  end # refresh
end # Sprite_Keypad_Select
#-------------------------------------------------------------------------------
#===============================================================================
# * Window_Keypad_Product
#===============================================================================
class Window_Keypad_Product < Window_Base
  def initialize(viewport = nil)
    super(218,94,128,56)
    create_contents
    create_sprites
    refresh
  end # initialize
 
  def refresh
    self.contents.clear
    self.contents.draw_text(0,0,128-32,24,"#{$Entered_Code[1]} #{$Entered_Code[2]} #{$Entered_Code[3]} #{$Entered_Code[4]}",1)
  end # refresh
 
  def create_sprites
    @keypad_Product_port = Viewport.new(0,0,544,416)
    @keypad_Product_port.z += 15
    $Keypad_Product = Sprite_Keypad_Product.new(@keypad_Product_port)
  end # create_sprites
end # Window_Keypad_Product
#-------------------------------------------------------------------------------

#=end


Credit




Thanks


Support



Post any problems here :) I'll reply when I can.
NOTE: Images used and the icon set I used can both be found in the demo.

Known Compatibility Issues

There shouldn't be any compatibility issues.

Demo



See attached.

Restrictions

Only for use in non-commercial games.
Title: Re: Keypad v1.0
Post by: LoganF on October 30, 2011, 04:52:06 AM
My biggest issue is the amount of variables you've brought into global scope. The symbol character preceding a variable name determines the scope: '$' allows a variable to be accessed from anywhere in the program during runtime.

It's not a particularly good thing to have variables put into global scope. It allows very easy and quick manipulation over the system. There's a reason why variables for objects like $game_system and $data_skills are accessible globally; they contain important information that may be required to be accessed in a large number of classes. Your Keypad button locations, keypad codes, keypad cursor graphics (all examples of things you have in global space) are very class specific and should be kept within that scope.

If you aren't familiar with ways to pass required information between classes without having to resort to global scope, I'd suggest that's something you should work on.

When it comes to things like passing information between class A and class B, this is something that the scene holding the control on those two classes can handle. If class A has data it wants to send to class B then class B can be given a method that receives that data and the scene can simply act as the mail man to complete the delivery.

Below is an example:
[spoiler=Some code that works]

class Scene_X

  def initialize
    @a = A.new
    @b = B.new
  end
 
  def deliver_data_to_b_from_a
    @b.data_to_receive(@a.data_to_send)
  end
 
  def a
    return @a
  end

  def b
    return @b
  end

end

class A

  def initialize
    @data = "some new text data"
  end
 
  def data_to_send
    return data
  end
 
  def data
    return @data
  end
 
end

class B

  def initialize
    @data = "old text data"
  end
 
  def data_to_receive(data)
    @data = data
  end
 
  def data
    return @data
  end
 
end

=begin
Executing program:

scene_obj = Scene_X.new
p scene_obj.a.data  #  "some new text data"
p scene_obj.b.data  #  "old text data"
scene_obj.deliver_data_to_b_from_a
p scene_obj.b.data  #  "some new text data"
=end


[/spoiler]

Rather than making everything global, consider making set and get style methods that can be used to send and receive the information a given class needs.

The other problem with creating variables in the global namespace is that the memory space those variables need will not be released until program shut down (thus why are accessible at all times). Whilst not a huge problem for RMVX, in a memory dependant system it's essentially a waste of resources if they are only used in a few situations.

I don't mean to put you down. I just like to offer advice where it's useful. I'm no expert, by any means, but some things I do know and some things I can help with.

There's nothing wrong with the actual idea of the script though - it's a good system. However, the actual programming isn't very good. At least not in my opinion anyway.
Title: Re: Keypad v1.0
Post by: SeMcDun on October 30, 2011, 11:49:03 AM
Well yeh I take this a more constructive criticism rather than a put down. I appreciate when people try to help out. I agree I should take more time to learn to be a better programmer rather than just rattle out a script a way that works.

I tried using @ local variables for graphics and couldn't get it to work, but yeh if I want to progress I should take the time to learn instead of using a quick-fix way out.

Thanks for spurring me on to actually learn again xD