How do I implement mouse input in my game?

From OHRRPGCE-Wiki

(Redirected from Using the Mouse in the Game)
Jump to: navigation, search

It's actually a lot easier than you might expect.

Contents

[edit] Step 1: The Script

Mouse usage is entirely script driven (that's a feature, I think). You must handle the drawing of the mouse yourself. Usually this involves an NPC. But, first the framework for the script:

include, plotscr.hsd
include, mygame.hsi

plotscript, mouse script, begin
  # Must be called before any other mouse command
  init mouse
  # Infinite loop, yes, there's a reason
  while (true) do, begin
    # You can put an appropriate condition here
    if (checktag(tag:Mouse active)) then, begin
      # In the next line, change the "0" to the ID of the mouse NPC
      put NPC (0, mouse pixel x + camera pixel x, mouse pixel y + camera pixel y)
    end
    # Let game.exe breathe
    wait
  end
end

This is the skeleton of the mouse control script, and doesn't do anything but move the cursor around the screen. Here's a breakdown of each line of the script:

include, plotscr.hsd
include, mygame.hsi

Standard boilerplate, include the standard stuff.

plotscript, mouse script, begin
  # Must be called before any other mouse command
  init mouse

Init mouse tells Game.exe to look for a mouse, and to prepare to use it. Suffice it to say that this must come before any other mouse command.

  # Infinite loop, yes, there's a reason
  while (avoid warning) do, begin

We're going to be updating the mouse continously, so we need an infinite loop (you can add ways to break out of this loop if you need to for some reason)

    # You can put an appropriate condition here
    if (checktag(tag:Mouse active)) then, begin

Most games don't want the mouse going all the time. So, by setting that flag, you can "turn off" the mouse. However, you'll also need to remove the NPC at the same time, or your cursor will be stuck on that map position.

      # In the next line, change the "0" to the ID of the mouse NPC
      put NPC (0, mouse pixel x + camera pixel x, mouse pixel y + camera pixel y)

Since the cursor is just an NPC, we use put NPC to move it.

    end
    # Let game.exe breathe
    wait

Infinite loops are finnicky, and you'll lock up Game.exe without this command.

  end
end

And, close the loop and script.

[edit] Step 2: Map Setup

Now that you have the script, you're ready to use the mouse, right? Not so fast! You need to make sure the map meets your exacting standards!

Primarily, this involves making sure that there is an NPC with the right ID on the map before the mouse flag is turned on. You can place a mouse NPC on the map in Custom.exe, or you could use another script to place the NPC in Game.exe, it doesn't matter.

[edit] Step 3: Game Setup

Ok, the script is ready, the NPC is ready, now are you done? Not a chance! We may have written the script, but I haven't told you where to put it. It needs to be called in two places: the New Game script, and the Load Game script. It can either be set to be those scripts, or it can be called from inside them.

However, be warned. A script like this won't work as you expect:

plotscript, new game, begin
  init global variables # script to set stuff up
  mouse script # Init mouse stuff
  story cutscene # Show story
end

See, the mouse script is an infinite loop, and as such will "shadow" any scripts after it. It should appear at the end of the New Game and Load Game scripts.

[edit] Step 4: Customizing the Script

Ok, script? Check. NPC? Check. Script being called? Check. Ready? Nope!

Right now, if you set the flag, the mouse will show up, and will move around, etc. However, nothing happens when you click! How can it? We haven't set it to do anything!

Recall the mouse script:

plotscript, mouse script, begin
  # Must be called before any other mouse command
  init mouse
  # Infinite loop, yes, there's a reason
  while (true) do, begin
    # You can put an appropriate condition here
    if (checktag(tag:Mouse active)) then, begin
      # In the next line, change the "0" to the ID of the mouse NPC
      put NPC (0, mouse pixel x + camera pixel x, mouse pixel y + camera pixel y)
      # Insert conditionals here
    end
    # Let game.exe breathe
    wait
  end
end

I added a line, "# Insert conditionals here". This is where we check to see if the mouse is clicking on anything important.

plotscript, mouse script, begin
  # Must be called before any other mouse command
  init mouse
  # Infinite loop, yes, there's a reason
  while (true) do, begin
    # You can put an appropriate condition here
    if (checktag(tag:Mouse active)) then, begin
      # In the next line, change the "0" to the ID of the mouse NPC
      put NPC (0, mouse pixel x + camera pixel x, mouse pixel y + camera pixel y)
      if (mouse button(left button)) then, begin
        # Check what's been clicked
      end
    end
    # Let game.exe breathe
    wait
  end
end

I put in a simple If statement that gets triggered when the left mouse button (henceforth, LMB) is pressed. Inside, you need to check to see if the mouse is somewhere interesting. Here is the script revised to see if the mouse is clicking on an NPC with the ID 5:

plotscript, mouse script, begin
  # Must be called before any other mouse command
  init mouse
  # Infinite loop, yes, there's a reason
  while (true) do, begin
    # You can put an appropriate condition here
    if (checktag(tag:Mouse active)) then, begin
      # In the next line, change the "0" to the ID of the mouse NPC
      put NPC (0, mouse pixel x + camera pixel x, mouse pixel y + camera pixel y)
      if (mouse button(left button)) then, begin
        variable (NPC, Num NPC, i)
        num NPC := NPC at pixel (mouse pixel x + camera pixel x, mouse pixel y + camera pixel y, get count)
        for (i,0,Num NPC -- 1) do, begin
          NPC := npc at pixel (mouse pixel x + camera pixel x, mouse pixel y + camera pixel y, i)
          if (get NPC ID(NPC) == 5) then, begin
            show text box(513)
            wait for text box
          end
        end
        # Check what's been clicked
      end
    end
    # Let game.exe breathe
    wait
  end
end

I'll explain the new section:

        variable (NPC, Num NPC, i)

We need a few variables. NPC is an NPC reference, Num NPC is the number of NPCs at the pixel where the mouse is, and i is a simple counter.

        num NPC := NPC at pixel (mouse pixel x + camera pixel x, mouse pixel y + camera pixel y, get count)

We need to get the number of NPCs at that pixel. If there's an NPC there, you must remember that there will be at least two there: The cursor is an NPC too. So, we need to loop through all of them. get count is a constant that causes NPC at pixel to return the number of NPCs.

        for (i,0,Num NPC -- 1) do, begin

As I said, we need to loop.

          NPC := npc at pixel (mouse pixel x + camera pixel x, mouse pixel y + camera pixel y, i)

Get one of the NPCs at that spot...

          if(get NPC ID(NPC) == 5) then, begin

... and see if it's the right kind.

            show text box(513)
            wait for text box

This is just a dummy event, which you're free to change to whatever you want.

          end
        end

And, close the blocks.

[edit] Closing Words

There's other mouse commands, but I think I'll end this now. As a review, here's the commands covered:

init mouse, mouse pixel X, mouse pixel Y, mouse button

I may expand this to cover the other two commands, put mouse and mouse region, but until then, ciao!

Personal tools