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.

HawkPut v2 - Input Replacement

HawkPut - Input Replacement
Version: 2
Last Updated: 16.04.2008


Disclaimer

I say here and now you cannot request a demo or screenshots as there is nothing to play with or see. Thank you.

Introduction

Now, I know there's at least 2 or 3 (if you include Near's Keyboard Module) other scripts that serve the same purpose but none of them fully overwrite the built-in Input module, so as any great, or desperate, scripter would do, I wrote my own!

Currently HawkPut is completely Plug 'n' Play, supports left and right [shift, ctrl, alt] and also adjusts the numpad based on if Numlock's state. I plan to offer support for different keyboard types, assuming I can find them, as well as offer more features for developers, and of course anything you people would like me to include, sans a pie machine.

Note: I've designed this to work as close to the original as possible, meaning that while I did replace the entire Input module the game won't (shouldn't) feel any different, though you can tweak to your own needs.


Dependencies
RPG Maker SDK


Scripts

Version 2
Current Version: HawkPut v2.0.0
Also avaiable via SVN: http://rmxp-scripts.googlecode.com/svn/trunk/HawkPut/
(You can view the script itself through that link)

Previous versions can be found at this Google Code mirror

Version 1
All previous versions of HawkPut are in a single archival file
HawkPut v1.x


Usage

With the addition of key bindings I figure I might as well explain everything from the beginning.

The Basics

The official Input module has 3 main functions (excluding update) which, while similar, work a little differently from each other.

To check if a key has been pressed you can use Input.press?(key), this is good for movement or modifier keys since it will always return true as long as the key is being pressed. However, if you try to use it for changing a selection, as in a menu, it'll scroll through all the selections faster than you can blink.

Code:
 

def deposit

    # Deposit 1 gold everytime.

    _deposit([1, $game_party.gold].min)

    # If the user is holding Shift, deposit another 9 for a total of 10 gold.

    if Input.press?(Input::SHIFT)

        _deposit([9, $game_party.gold].min)

        # If the user is hold Shift and Ctrl deposit another 90 after that

        # for a total of 100 gold

        _deposit([90, $game_party.gold].min) if Input.press?(Input::CTRL)

    end

end

 

This is where Input.repeat?(key) comes in handy. It adds a small delay between each time it returns true (it adds about 3 times the delay the first time it's pressed) this lets people make a selection more accurately. So, what do you use if you want to check if a key pressed down once?

Code:
 

    # Taken from Scene_Save

    if Input.repeat?(Input::DOWN)

        # The trigger check stops the cursor at the top/bottom.

        if Input.trigger?(Input::DOWN) or @file_index < 3

            $game_system.se_play($data_system.cursor_se)

            @savefile_windows[@file_index].selected = false

            @file_index = (@file_index + 1) % 4

            @savefile_windows[@file_index].selected = true

            return

        end

    end

    if Input.repeat?(Input::UP)

        if Input.trigger?(Input::UP) or @file_index > 0

            $game_system.se_play($data_system.cursor_se)

            @savefile_windows[@file_index].selected = false

            @file_index = (@file_index + 3) % 4

            @savefile_windows[@file_index].selected = true

            return

        end

    end

 

For that you'll want to use Input.trigger?(key) which only returns `true` once every time a user press the button down. This means you won't zoom through more than one window. Particularly useful for things like shops when it might cause the player to buy more than they wanted.

Code:
 

    # Basic idea, I was lazy x.x

        if Input.trigger?(Input::B)

            #[Retrun to the previous window]

            return

        end

 

        if Input.trigger?(Input::C)

            #[Buy the selected item]

            return

        end

 

So there's the basics! Next we'll talk about key bindings and how they work.

Key Bindings

Here's the part I'm sure most of you are curious about. You're wondering "What good are key bindings? All we need to do is walk around and press C to talk to people and B to open the menu!" and this is partly true, but that's because that's all you could do before!

Key Bindings give you a lot more control oever the game, now you can say "P" opens the inventory (or the "Pack") and then the user can come along and say that he's used to pressing "I" so he can set "I" to open the Pack!

Another possibility would be if you wanted to make a Nethack style game, you give people the option to use the arrow keys or to use HJKL, simply by binding the keys together. Simple as that. =]

"BRILLIANT!" You say? Of course you do =]

Code:
 

    # Assuming Input::P is the default to open the Pack

    Input::I.bindTo(Input::P)

    # You can also unbind keys

    Input::I.unbindFrom(Input::P)

    

    #For the HJKL movement

    Input::H.bindTo(Input::LEFT)

    Input::J.bindTo(Input::DOWN)

    Input::K.bindTo(Input::UP)

    Input::L.bindTo(Input::RIGHT)

    

    #Further information is listed in the script header.

 

Extras

One of the major changes in v2 is that every key (ie. Input::C) is an object. What this means is that it can store information about iself, along with state information each key also stores it's name and ID.

For example, Input::C.name returns "C" and Input::Mouse_left.id returns "1". This means that you don't need to create all sorts of funky conversation tables to return the name of a key!

Heres a fully working example you can use to experiment with bindings.
Code:
class Scene_Title

    def main

    # I would setting Input::Official to false.

        # The + operator is a synonym for key.bindTo(key)

        Input::Mouse_Left    + Input::C

        Input::Mouse_Right  + Input::B

        

        #Test binding and unbinding

        Input::P.bindTo(Input::I)

        Input::P.unbindFrom(Input::I)

        

        # Test recursive bindings.

        Input::E.bindTo(Input::Q)

        Input::Q.bindTo(Input::E)

        Input::R.bindTo(Input::R)

        

        loop do

            #Required updates

            Graphics.update

            Input.update

            

            # This set will print both if Mouse_Left is pressed

            # And only C other wise.

            p "C" if Input.trigger?(Input::C)

            p "Mouse Left" if Input.trigger?(Input::Mouse_Left)

            

            # Same as above

            p "Mouse Right" if Input.trigger?(Input::Mouse_Right)

            p "B" if Input.trigger?(Input::B)           

            

            #Should only print if P is pressed.

            p "P" if Input.trigger?(Input::P)

            

            #Should only print if Q or E are pressed.

            p "Q" if Input.trigger?(Input::Q)

            p "E" if Input.trigger?(Input::E)

            

            #Should only print if R is pressed.         

            p "R" if Input.trigger?(Input::R)

            

            if $scene != self

                break

            end

        end

    end

end


Change Log

  • v2.00
  • Initial Release
  • Cleaned up and rewrote most of the code from v1


Compatability

HawkPut should be fully compatable with any other script. If you have any problems, let me know.


Credits and Thanks
  • NearFantasica - Original Keyboard Module


Author's Notes

Given my forgetful nature some of the more advanced parts may be.... unfinished.... (But that's ok, most of them you'll never know about till I finish documenting them) So please! Report any bugs you find, and as always suggestions and criticism are welcome. Also, if anyone knows how to profile this, I'd love you forever!


Terms and Conditions

Feel free to use this script however you feel like, all I ask is you don't try to pass it off as your own, and that you don't remove the script header.
 

Toshi

Member

Very nice Script!
You should add a mouse control function too.
But i defenetly like this script.
Well done dude!
 

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