Please login or register.

Login with username, password and session length
Advanced search  

News:

Forum rules - please read before posting, it can save you a lot of time.

Author Topic: Show inventory by pressing a keyboard button  (Read 2471 times)

0 Members and 1 Guest are viewing this topic.

MARTIN151

  • Lurker
  • *
  • Karma: 1
  • Offline Offline
  • Posts: 7
    • View Profile
Show inventory by pressing a keyboard button
« on: November 19, 2024, 03:03:32 PM »

Hi,

I have a problem. In the template, the inventory shows when the pointer is near the upper side of the screen.

I'd like to show the inventory, when I press a button (for example the spacebar), but I'm not very good at coding and there is no topic here that could help me. Some have a code that seems suitable, but finally it doesn't work.

Could anyone please help me?

Thanks.
Logged

Mot

  • Occasional poster
  • **
  • Karma: 3
  • Offline Offline
  • Posts: 56
    • View Profile
Re: Show inventory by pressing a keyboard button
« Reply #1 on: March 06, 2025, 02:07:17 AM »

As an example, I implemented your idea in (a copy of) the wme demo project (also known as wme_demo.wpr).

I'm not sure if you want to add the functionality of displaying the inventory when pressing SPACE (while keeping the former method of navigating to the top of the screen) or you want to replace one by the other. If you want to keep both, follow Walkthrough #1. If you want to replace one by the other, Walkthrough #2 (down below this post).

Walkthrough 1

1. I open wme_demo.wpr
2. In the project tree pannel, I locate the folder named scripts, and open the file base.inc
3. This file is loaded on SciTE Script Editor.
4. I add a global variable to keep track of whether the SPACE key has been pressed to show/hide the inventory. I'll name it spaceSwitch.

Code: WME Script
  1. #include "scripts\const.inc"
  2.  
  3. global Scene;
  4. global Keyboard;
  5. global actor;
  6.  
  7. // this var keeps track whether the user has pressed space to show or hide the inventory
  8.  global spaceSwitch;

5. In the project tree pannel, I locate the folder named scripts, and open the file game.script
6. I give an initial value to the var spaceSwitch at the start of the code - false since the space key has not been pressed yet.


Code: WME Script
  1. #include "scripts\base.inc"
  2. #include "scripts\keys.inc"
  3.  
  4. // store some of the game's attributes in global variables for convenience
  5.  
  6. // this var keeps track whether the user has pressed space to show or hide the inventory
  7.  spaceSwitch = false;

7. I go to the bottom of the code and locate the portion that tells the engine what to do when the player presses a key on the keyboard. I add a new portion of code that is triggered when the user presses the SPACE key.

This portion makes sure that the main character is not currently having a dialogue with other characters, and that the Win menu is not visible, before showing the inventory, as there's no use for the inventory to show up when those are visible. In the same way, we tell the engine to hide the inventory if the main character starts a dialogue or when the game stops being interactive.

At the end, I add a "switch" that is changed from false to true, and from true to false, each time the user presses the SPACE key.


Code: WME Script
  1. ////////////////////////////////////////////////////////////////////////////////
  2. on "Keypress"
  3. {
  4.   // on Esc or F1 key
  5.   if(Keyboard.KeyCode==VK_ESCAPE || Keyboard.KeyCode==VK_F1)
  6.   {
  7.     // load and display the main menu window
  8.     WinCaption.Visible = false;
  9.     var WinMainMenu = Game.LoadWindow("interface\system\mainmenu.window");
  10.     WinMainMenu.Center();
  11.     WinMainMenu.GoSystemExclusive();
  12.     Game.UnloadObject(WinMainMenu);
  13.   // on SPACE key
  14.   } else if (Keyboard.KeyCode==VK_SPACE)
  15.   {    
  16.     if (!Game.ResponsesVisible && !WinMenu.Visible) {
  17.       Game.InventoryVisible = true;
  18.     } else if (Game.ResponsesVisible || !Game.Interactive) {
  19.       Game.InventoryVisible = false;
  20.     }
  21.     // turn on and off the space switch when pressing space
  22.     spaceSwitch = !spaceSwitch;
  23.     }
  24. }
  25.  
  26. ////////////////////////////////////////////////////////////////////////////////

8. In the project tree pannel, I locate the folder named scripts, and open the file game_loop.script

9. I go to the bottom of the code and locate the portion that tells the engine to show the inventory when the user navigates to the top of the screen.

I modify the code, so the inventory only dissapears from the screen if the user has not previously triggered it by means of the SPACE key. If I didn't make this modification, considering that the mouse cursor can be in positions outside the top, the inventory would never stick around when the user pressed SPACE: the engine would display it just for an instant before making it dissapear right away.

Finally I set spaceSwitch to false to avoid a conflict between the different methods of toggling on and off the inventory. If you're curious, you can delete spaceSwitch = false; and see what happens.

Code: WME Script
  1. // display the inventory window
  2. {
  3. } else if((Game.MouseY > 100 && spaceSwitch == false)|| Game.ResponsesVisible|| !Game.Interactive)
  4. {
  5.   spaceSwitch = false;
  6. }

Walkthrough 2

If your intention is to replace the former method of displaying the inventory (ie. navigating to the top of the screen) by a new method that shows/hides the inventory when pressing the SPACE key.

1. In the file game_loop.script, I modify the portion of code that tells the engine to show the inventory when the user navigates to the top of the screen like this:


Code: WME Script
  1. // display the inventory window
  2. if(Game.ResponsesVisible|| !Game.Interactive)
  3. {
  4. }

2. I modify the file game.script like this:

Code: WME Script
  1. ////////////////////////////////////////////////////////////////////////////////
  2. on "Keypress"
  3. {
  4.   // on Esc or F1 key
  5.   if(Keyboard.KeyCode==VK_ESCAPE || Keyboard.KeyCode==VK_F1)
  6.   {
  7.     // load and display the main menu window
  8.     WinCaption.Visible = false;
  9.     var WinMainMenu = Game.LoadWindow("interface\system\mainmenu.window");
  10.     WinMainMenu.Center();
  11.     WinMainMenu.GoSystemExclusive();
  12.     Game.UnloadObject(WinMainMenu);
  13.   // on SPACE key
  14.   } else if (Keyboard.KeyCode==VK_SPACE)
  15.   {    
  16.     if (!Game.ResponsesVisible && !WinMenu.Visible) {
  17.       Game.InventoryVisible = true;
  18.     } else if (Game.ResponsesVisible || !Game.Interactive) {
  19.       Game.InventoryVisible = false;
  20.     }
  21.   }
  22. }
  23.  
  24. ////////////////////////////////////////////////////////////////////////////////
« Last Edit: March 06, 2025, 04:57:29 AM by Mot »
Logged
 

Page created in 0.477 seconds with 24 queries.