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,271
Reaction score
11,658
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,496
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,137
Reaction score
8,417
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

Another plugin complete and ready for Wednesday!
Just for the record I have greatly enjoyed the past couple months of using RPG Maker MV - enough that I went ahead and bought MZ although it's going to be a bit before I seriously start a project with that.
I have a pretty reasonable alpha of one game that's got roughly 4 hours of content and some bits and pieces of others after only about 300 hours total time and about 2 1/2 months of owning RPG Maker.....
I completely forgot which forum had the personal blogs for members. :LZSlol: It was probably a forum for the Ace program. I was going to see about posting a blog post, but I'm too tired, and if its associated with Ace, then its a moot point. :LZSlol:

Forum statistics

Threads
102,970
Messages
996,379
Members
134,432
Latest member
beebTime
Top