1
Technical forum / Re: Show inventory by pressing a keyboard button
« 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.
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.
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.
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.
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:
2. I modify the file game.script like this:
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
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
- #include "scripts\base.inc"
- #include "scripts\keys.inc"
- // store some of the game's attributes in global variables for convenience
- // this var keeps track whether the user has pressed space to show or hide the inventory
- 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
- ////////////////////////////////////////////////////////////////////////////////
- on "Keypress"
- {
- // on Esc or F1 key
- {
- // load and display the main menu window
- WinCaption.Visible = false;
- // on SPACE key
- {
- }
- // turn on and off the space switch when pressing space
- spaceSwitch = !spaceSwitch;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
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
- // display the inventory window
- {
- {
- spaceSwitch = false;
- }
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
- // display the inventory window
- {
- }
2. I modify the file game.script like this:
Code: WME Script
- ////////////////////////////////////////////////////////////////////////////////
- on "Keypress"
- {
- // on Esc or F1 key
- {
- // load and display the main menu window
- WinCaption.Visible = false;
- // on SPACE key
- {
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////