RMMZ [Solved] Question about Nested Choices via JS

Status
Not open for further replies.

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
I have a wardrobe that I want the player to pick items from. The wardrobe is a dictionary, and the choices offered to the player should be nested. I.e.: "Pants > Jeans" or "Top > Tee".

I do get the first level working, but then the messaging system stops reacting. I somewhat understand that this due to how the external JS is interpreted by RPGM. I know I can do nested selection using Event's UI, I wrote a post below explaining why this is not an option.

Here is my code. Could somebody please kindly help with RPGM's API - how can I do a nested selection using JS? I am failing even at showing a message after the selection has been made.

JavaScript:
//This is a dictionary with the 2-level nested choice
my_wardrobe = {
    "Pants"    :    ["Jeans", "Shorts"],
    "Top"    :    ["Shirt", "Tee"]
};

//This is the callback function factored out
function on_wardropbe_selection(items, selection){
    message = `Selected: ${items[selection]}`
    console.log(message)
 
    //$gameMessage.newPage();
    $gameMessage.add(message);
}

//This function displays selection prompt
(function() {
    offer_choice = function(message, my_items) {    
        const items = my_items
        $gameMessage.add(message)
        $gameMessage.setChoices(items, 0, -1)
        $gameMessage.setChoiceBackground(0);
        $gameMessage.setChoicePositionType(2)
        $gameMessage.setChoiceCallback(function(selection) {
            on_wardropbe_selection(items, selection)
        });  
    }
})();

//This takes dictionary's keys and turns them into 1st level selection prompt
function chose_from_dictionary(message, my_dict){
    offer_choice(message, Object.keys(my_dict))
}

//This takes dictionary's values and turns them into arrays
function chose_from_array(my_array){
    offer_choice(message, my_array)
}

//Function called from an event via Script: my_wardrobe_menu()
(function() {
    my_wardrobe_menu = function() {    
        chose_from_dictionary("Wardrobe choices?", my_wardrobe)
    }
})();

Thank you.

PS I saw a similar post asking for help, yet it was unsolved: link
 
Last edited:

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
I.e. below is an example of nested choice that I am trying to accomplish vis JS. Reason I need JS over event's UI is that my game is a "trainer" with 50 items of wardrobe per category. It is simply unwieldy to setup a wardrobe using Event's UI.

Code:
◆Show Choices:Pants, Top (Window, Right, #1, #2)
:When Pants
  ◆Show Choices:Jeans, Shorts (Window, Right, #1, #2)
  :When Jeans
    ◆
  :When Shorts
    ◆
  :End
  ◆
:When Top
  ◆Show Choices:Shirt, Tee (Window, Right, #1, #2)
  :When Shirt
    ◆
  :When Tee
    ◆
  :End
  ◆
:End

Example.png
 
Last edited:

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
One candidate for solution that I see is looping JS's query while maintaining where in the nested selection the player is, like this, with all proper loop breaks, etc:

Candidate.png

But Jesus, I hope there is a better way.
 
Last edited:

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
So, I managed to get a nested selection going using event's loop:

01.png

And the in-game result:

02.png03.png

But if anyone knows a JS-only solution, avoiding the need for an event loop, please let me know. Thank you.
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
59
Reaction score
29
First Language
English
Primarily Uses
RMMZ
Illustration of the JS-based nested-selection wardrobe system:

 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
9,112
Reaction score
5,963
First Language
Dutch
Primarily Uses
RMXP

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.

Latest Threads

Latest Posts

Latest Profile Posts

1638170340913.png
Crossover Inn is getting very crowded, and I love it
Climbing to the top #mapping
1638160732198.png
I've started the first steps of the final few maps that I need to make for B:ms. I haven't been able to say that in a really, really long time. It'd probably be a good idea to blog about this or something.
Question for the Mods: What am I supposed to do when I can't make a synopsis 350 words long? (for project recruitment thread) @Shaz @mlogan
The game is meant to be more on the simple side in order to be achievable so I'm a bit stuck...

Forum statistics

Threads
117,043
Messages
1,104,077
Members
152,987
Latest member
mrsmatmoney
Top