[Ace] Tweaking "More Choices" Script

So I'm trying to tweak Fomar0153's "More Choices" script (http://www.rpgmakercentral.com/topic/1195-more-choices/) to give me more than four choices for an event.


Here's what I've got so far.  And here's what I'd like to do:


After each choice, show how many of that item I have in inventory, like:


Tiny Fish: 21


After-Dinner Mint: 14


Cheese: 3


And so on.


And


Have the choice not show up at all if the corresponding item is not in inventory at all.


Thanks in advance for any help you can provide. 🙂


food script.png
For the first one, I think you can use variables in choices.  So if you set a variable to the number of items in inventory before the choice processing, I think you can say "Tiny Fish (\v[1])" and it will show Tiny Fish (8) or whatever.


Not sure on the second one.
For the first one, I think you can use variables in choices.  So if you set a variable to the number of items in inventory before the choice processing, I think you can say "Tiny Fish (\v[1])" and it will show Tiny Fish (8) or whatever.



Tried this and got: 


"Tiny Fish: \V[82]"  just like that.
🙁
So I set an event to assign the amount of each item in inventory to a variable and changed the script to this:


def more_choice(p)
case p
when "Eat Food2"
if $game_variables[82] > 0
$game_message.choices.push("Tiny Fish")
end
if $game_variables[83] > 0
$game_message.choices.push("After-Dinner Mint")
end
if $game_variables[84] > 0
$game_message.choices.push("Cheese")
end
if $game_variables[85] > 0
$game_message.choices.push("Jelly Donut")
end
if $game_variables[86] > 0
$game_message.choices.push("Potato")
end
if $game_variables[87] > 0
$game_message.choices.push("Soup")
end
if $game_variables[88] > 0
$game_message.choices.push("Spicy Curry")
end
if $game_variables[89] > 0
$game_message.choices.push("Beef Lump")
end
if $game_variables[90] > 0
$game_message.choices.push("Pizza")
end
if $game_variables[91] > 0
$game_message.choices.push("Turducken")
end
else
$game_message.choices.push(p)
end
end


And it SEEMS to work - the choices only pop up when I have the correct item in inventory.


BUT after I pick a choice, the game crashes with a "stack level too deep" error, which according to Google means infinite recursion is going on somewhere, but I don't think I know enough about Ruby to figure out where. 🙁
Tried this and got: 


"Tiny Fish: \V[82]"  just like that.

Try use double slash because the choice texts is drawn by draw_text_ex.


"Tiny Fish: \\V[82]"
Try use double slash because the choice texts is drawn by draw_text_ex.


"Tiny Fish: \\V[82]"



This works (yay!)


But I'm still getting the Stack Level Too Deep error. 🙁


Here's the whole script, my changes included.  It's a pretty short script, if anyone can see what's screwing up.

Code:
class Game_Interpreter
  #--------------------------------------------------------------------------
  # ● Edit Here
  # when handle
  #   $game_message.choices.push("a choice")
  #--------------------------------------------------------------------------
  def more_choice(p)
    case p
    when "Eat Food2"
      if $game_variables[82] > 0
      $game_message.choices.push("Tiny Fish: \\V[82]")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[83] > 0
      $game_message.choices.push("After-Dinner Mint")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[84] > 0
      $game_message.choices.push("Cheese")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[85] > 0
      $game_message.choices.push("Jelly Donut")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[86] > 0
      $game_message.choices.push("Potato")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[87] > 0
      $game_message.choices.push("Soup")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[88] > 0
      $game_message.choices.push("Spicy Curry")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[89] > 0
      $game_message.choices.push("Beef Lump")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[90] > 0
      $game_message.choices.push("Pizza")
      else
      $game_message.choices.push("---")
      end

      if $game_variables[91] > 0
      $game_message.choices.push("Turducken")
      else
      $game_message.choices.push("---")
    end
    $game_message.choices.push("Cancel")
    else
      $game_message.choices.push(p)
    end
  end
  #--------------------------------------------------------------------------
  # ● Long Choices - Don't edit this bit
  #--------------------------------------------------------------------------
  def setup_choices(params)
    for s in params[0]
      more_choice(s)
    end
    $game_message.choice_cancel_type = params[1]
    $game_message.choice_proc = Proc.new {|n| @branch[@indent] = n }
  end
end

class Window_ChoiceList < Window_Command
  #--------------------------------------------------------------------------
  # ● Feel free to change the 1 to another variable that you'd prefer
  #--------------------------------------------------------------------------
  alias mc_call_ok_handler call_ok_handler
  def call_ok_handler
    $game_variables[81] = index
    mc_call_ok_handler
  end
end
Stack level too deep usually indicates that you've got a script in there twice, or two scripts using the same alias for the same class/method.
No idea why you get that error, but I would do it like this instead:

Spoiler
Code:
class Game_Interpreter
  #--------------------------------------------------------------------------
  # ● Edit Here
  # when handle
  #   $game_message.choices.push("a choice")
  #--------------------------------------------------------------------------
  def more_choice(p)
    case p
    when "Eat Food2"
      # The numbers here are item IDs from your database.
      # Change them, remove them, add more, etc to suit your need.
      [11,12,13,14,15,16].each do |item_id|
        # $data_items[item_id] will load an Item from your database.
        # $data_weapons[item_id] will load a Weapon from your database.
        # $data_armors[item_id] will load an Armor from your database.
        item = $data_items[item_id]
        next unless $game_party.has_item?(item) # Don't add if the player got no such item.
        txt = "#{item.name}: #{$game_party.item_number(item)}" # Shown text.
        $game_message.choices.push(txt)
      end
      if $game_message.choices.empty? # No food items in inventory
        # This choice shows up only when the player got no items 
        # from the above item list.
        $game_message.choices.push("Bollocks, I got no food! o.O")
      else
        # And this shows up if the player got some of the listed items.
        # Obviously, you can use this as a Cancel handler.
        $game_message.choices.push("Cancel")
      end
    else
      $game_message.choices.push(p)
    end
  end
  #--------------------------------------------------------------------------
  # ● Long Choices - Don't edit this bit
  #--------------------------------------------------------------------------
  def setup_choices(params)
    for s in params[0]
      more_choice(s)
    end
    $game_message.choice_cancel_type = params[1]
    $game_message.choice_proc = Proc.new {|n| @branch[@indent] = n }
  end
end

class Window_ChoiceList < Window_Command
  #--------------------------------------------------------------------------
  # ● Feel free to change the 1 to another variable that you'd prefer
  #--------------------------------------------------------------------------
  alias mc_call_ok_handler call_ok_handler
  def call_ok_handler
    # Changed to store the selected choice's text instead of it's index.
    $game_variables[81] = $game_message.choices[index]
    mc_call_ok_handler
  end
end



I changed the script a bit, and made another editable place, because with dynamic lists such as yours, if there is a cancel option, it would be kinda hard to get when the player actually cancelled the selection. 


You can add more choice selection checks in the call_ok_handler method to make sure when a specific choice is selected, you set the variable to something unique so that you know for sure, which enables you to make your conditional branches for the choice variable actually useful for these lists with dynamic choice positions.


I tried to explain how the method at the choice creation works. Basically, you won't need to use any variables for that list you wanted to make this way, and it will automatically name your choices correctly too. Just change the item IDs in the array, and it will work how you wanted.


I tested this, and it works how it should. But if you still get that stack error, show us your event where these choice options happen, maybe something went wrong there (although that is not very likely, unless you made some recursive methods in a script call).


Either that, or you installed this script more than one time, in which case, you should remove the extra ones.


Edit:


Ohh, scratch that, I realized that the way I did it, the whole list becomes a big dynamic choice list, which makes the index of choices basically useless. You would need to set the choice text itself for the variable and check that in your conditional branches after the choice command in your events. That is still fairly easy to do, just use the "Script" option in the "Conditional Branch" event command, and type in something like:


$game_variables[81].include?("item name here")


For the item based choices.


And for static choices, you can simply use this:


$game_variables[81] == "choice text here"


So, instead of checking the index of the choices, you check for the name of the choices, which works, even for dynamic choice lists.


I have changed the above edited script to store the direct choice text for the variable instead of the choice index.
Stack level too deep usually indicates that you've got a script in there twice, or two scripts using the same alias for the same class/method.



*bashes head repeatedly on concrete


It was THAT SIMPLE?! -_____-;;;;;


So... yeah, I thought, to be safe, that I should have an original copy of the script in there as well as the one I edited.  Guess, uh... that you can't do that.


So the script works now. ^_^;;;;;;  Yay and thanks for all your help.
haha!


You could keep the original in there if you want.  Just do one of the following:


- search through for =begin and =end.  If you don't find any, put =begin on a new line right at the top of the script, and =end on its own line right at the bottom.  The = must be in the very first column.  This comments out the whole script, so you won't get that problem.


- search through for #~  If you don't find any, use Control+A to select the whole script, then Control+Q which will comment out the whole script by putting #~ at the start of every row.


- save it in a text file in a folder for original versions of useful scripts.  You'll have to do it this way if the script already contains a =begin/=end pair or any #~ lines.

Original thread: https://forums.rpgmakerweb.com/threads/ace-tweaking-more-choices-script.70968/