Toggle Items Command On/Off while in Battle

Status
Not open for further replies.

Zelfore

Villager
Member
Joined
Apr 5, 2019
Messages
9
Reaction score
0
First Language
English
Primarily Uses
RMVXA
I've been scouring the internet for an answer to this for awhile with no clear result, so I'll make it as clear as possible so there's no confusion.

I need a way/method to hide/remove/disable the Items Command while in the middle of a battle (imperative), and also to turn it back on again (less important). Also, unless there's a pre-built-in script call to remove :item from the commands list after it's added, that won't work for this.

If "Items" is just not selectable without a toggle of some sort, that's fine too (if not better).

I've been experimenting with Yanfly Battle Commands a bit, but yielded no clear results. Closest I got is:
Code:
return if $game_switches[98] = false
add_item_command
The only conclusion being if I set it to FALSE, "Items" option appears. Set it to TRUE, "Items" doesn't appear. Either way it disregards whether the switch[#98] is actually on or off, before or in battle.

I'm willing to try this with or without an additional material script, so any help or suggestions are welcome.
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,418
Reaction score
591
First Language
Italian
Primarily Uses
RMVXA
That happens because the syntax in your code is wrong. You have been using
Code:
$game_switches[98] = false
which is an assignment and, as every assignment, it returns the value that has been assigned (in your case false).

If you look at it from the perspective of the "if" statement before it, it looks like this:
Code:
return if false
This is never true because the whole condition is simply false by default. After that line is executed you simply have switch 98 set to false no matter what. Similarly, if you set the switch as true, the assignment returns true.

What you should be doing is check if the said switch is true, and you can do it like this:
Code:
return if ($game_switches[98] == true)
# or
return if $game_switches[98]
Since switches store boolean values, the second statement is the same as the first one. When the switch is true, the result of "$game_switches[98]" is true, conversely, when false, the result is false.

However, it is always a bad idea to change a method that has already been written in a script, regardless of who wrote the original script. You might forget that you changed it one day and issues might arise. If that happens, pinpointing the cause becomes much harder. A much better solution is that of adding a small script below the one you want to modify and alias the method with a new one. It will be absolutely compatible and, should any issue arise, it is easy to see and comment.

To do that you can add the return line to the add_item_command method itself.
Code:
#------------------------------------------------------------------------------
# ** Window_ActorCommand class
#------------------------------------------------------------------------------
class Window_ActorCommand
  #----------------------------------------------------------------------------
  # * Add Item Command
  #----------------------------------------------------------------------------
  alias hrk_add_item_command_old add_item_command
  def add_item_command
    return if $game_switches[98]
    hrk_add_item_command_old
  end # Add Item Command
end # end of Window_ActorCommand class
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,290
Reaction score
11,698
First Language
English
Primarily Uses
RMVXA
[move]Learning Ruby and RGSSx[/move]
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
5,573
Reaction score
6,499
First Language
Indonesian
Primarily Uses
RMVXA
Or if you don't want to hide/remove it, instead make it grayed out, you can always do this
Code:
  def add_item_command
    add_command(Vocab::item, :item, $game_switches[98])
  end
Depends on what you need, you can flip the condition like
Code:
  def add_item_command
    add_command(Vocab::item, :item, !$game_switches[98])
  end
 

Zelfore

Villager
Member
Joined
Apr 5, 2019
Messages
9
Reaction score
0
First Language
English
Primarily Uses
RMVXA
Thank you both, Heirukichi and TheoAllen, this is exactly what I've been looking for!
I've also learned a lot from this as well, especially about script etiquette. This will make things much smoother going forward.
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
15,233
Reaction score
8,473
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Dev stream today, come on by and ask questions about rpg maker!
Fun fact: If your story introduces a variety of different races, the "human race" will automatically become the most corrupt race in general.
Finally finished my late start entry for the jam. Now. I. Sleep.
Just made 3 easy, simple and small MZ plugins today(12 hours straight), and I never thought I could ever do this.
Now I feel myself being very productive, even though I know I'm just utterly flattering myself lol
Mind: "I have cool ideas! Let's make it real!

Body: "You wanna make sumthin? I'm in."

Eyes: "Go to sleep, go to sleep."

*Sleep*

Forum statistics

Threads
104,647
Messages
1,007,818
Members
136,133
Latest member
Cashews
Top