MZ RPG Maker MZ Javascript Show Choices Help

For those still searching in the future. I figured a solution. Not sure about why it's still a problem biut found a workaround that works. This allows items to add conditional branch just off of being in the inventory. It isn't complete but it works for what I needed it for.


This is also a valid option as well.
Thank you @AquaEcho
Thank you @Trihan

Little Backstory,
I was wondering if someone would be able to assist me in debugging why this function is not working? I'm not a master programmer and alot of this is written with ChatGPT's help on some things. My goal of this is that it will display the names of the seeds that are currently in your inventory and when selected perform the common event that the constant assigned to them.

Current Problem,
- Does not display any of the choices ingame. Just a blank menu I can exit out of.

How I call this is with a script in an event by just calling the function

Code:
function checkSeedAvailability() {
    const seedCommonEvents = [
        { name: "Carrot Seeds", eventId: 125 },
        { name: "Corn Kernals", eventId: 123 },
        { name: "Potato Sprouts", eventId: 122 },
        { name: "Cabbage Seeds", eventId: 124 },
        { name: "Radish Seeds", eventId: 126 },
        { name: "Onion Seeds", eventId: 127 },
        { name: "Strawberry Seed", eventId: 128 },
        { name: "Pumpkin Seeds", eventId: 129 },
        { name: "Bean Seeds", eventId: 130 },
        { name: "Cashew Seeds", eventId: 131 },
        { name: "Tomato Seeds", eventId: 132 },
        { name: "Wheat Seeds", eventId: 133 },
        { name: "Zuchinni Seeds", eventId: 134 },
        { name: "Lettuce Seeds", eventId: 135 },
        { name: "Blueberry Seeds", eventId: 136 },
        { name: "Broccoli Seeds", eventId: 137 },
    ];

    const isEquippedWithSeedBag = $gameParty.leader().equips().some(function (equip) {
        return equip && equip.meta.SeedBag === 'true';
    });

    if (isEquippedWithSeedBag) {
        const availableSeeds = $dataItems.filter(function (item) {
            return item && $gameParty.numItems(item) > 0;
        });

        const seedChoices = availableSeeds.map(function (seed) {
            return seed.name;
        });

        if (seedChoices.length > 0) {
            $gameMessage.setChoices(seedChoices, 0, -2, 0);
            $gameMessage.setChoiceCallback(function (choiceIndex) {
                if (choiceIndex === -2) {
                    $gameTemp.reserveCommonEvent(20);
                    return;
                }

                const selectedSeed = availableSeeds[choiceIndex];
                const selectedSeedName = selectedSeed.name;

                const selectedEventId = seedCommonEvents.find(seed => seed.name === selectedSeedName)?.eventId;

                if (selectedEventId) {
                    $gameTemp.reserveCommonEvent(selectedEventId);
                } else {
                    $gameMessage.add(`You planted ${selectedSeedName}!`);
                }

                const selectedSeedId = selectedSeed.id;
                $gameParty.gainItem($dataItems[selectedSeedId], -1);
            });
        } else {
            $gameMessage.add("You don't have any seeds.");
        }
    } else {
        $gameMessage.add("You need to equip a Seed Bag weapon to plant seeds.");
    }
}
You might want to use this instead
That might work so I'll try giving it a try if no one has a better solution. I think it's the $gameMessage giving me trouble. Thank you

Ok so I adjusted the code a big. This now produces me a exact replica of what should work in a $gameMessage.setChoices()
Truly do not know what I am doing wrong. I'll even put the console log into here as well.

Spoiler: Event Execution
◆If:Seed Bag Detect > 0
◆Control Variables:#0057 FarmPlots = 1
◆Script:checkSeedAvailability();
◆Script:this.setWaitMode("Please select a seed to be planted...");
◆
:Else
◆Plugin Command:CGMZ_ToastManager, Create Text Toast
: :Line 1 = You need a seed bag to plant seeds!
: :Line 1 Alignment = center
: :Line One Color = 0
: :Line 2 =
: :Line 2 Alignment = center
: :Line Two Color = 0
: :height = 1
: :Width = 315
: :Sound Effect = Bell3
: :Display Time = 60
: :Background Style = Window
: :Windowskin Tone = {"Red":"0","Green":"0","Blue":"0"}
: :windowskin = Alpha/Window
◆
:End

Spoiler: Console
PKD_SimpleFishing.js:7879 KDCore is loaded 3.2.7
PKD_MapInventory.js:9752 KDCore is loaded 3.2.9
Alpha_NETZ.js:2211 XDev KDCore 3.2.7 skipped by new or exists version
Alpha_NETZ.js:9365 Socket.io is loaded 2.3.0
Alpha_NETZ.js:13364 Load extension: YEP_BattleEngineCore | VisuMZ_1_BattleCore
CGMZ_Core.js:806 Warning! Out of date CGMZ Plugin(s) found:
(anonymous) @ CGMZ_Core.js:806
CGMZ_Temp.checkCGMZPluginVersions @ CGMZ_Core.js:803
(anonymous) @ CGMZ_Core.js:1049
CGMZ_Core.js:815 CGMZ Toast Manager had a local version of 1.3.0 but a server version of 1.3.1
You can download an update from https://www.caspergaming.com/plugins/cgmz/toastmanager/
(anonymous) @ CGMZ_Core.js:815
CGMZ_Temp.checkCGMZPluginVersions @ CGMZ_Core.js:803
(anonymous) @ CGMZ_Core.js:1049
FTFarming.js:544 Starting recurring farming action.
FTFarming.js:533 Variable 24 has been updated: 0
FTFarming.js:533 Variable 24 has been updated: 1
FTFarming.js:544 Starting recurring farming action.
$gameVariables.value(60);
(4) ["Carrot Seeds", "Corn Kernals", "Cabbage Seeds", "Onion Seeds"]

Code:
 function checkSeedAvailability() {
    
    const seedCommonEvents = [
        { name: "Carrot Seeds", growthTimeTotal: 6, commonEventId: 125, gameFarmVariableId: 1 },
        { name: "Corn Kernals", growthTimeTotal: 6, commonEventId: 123, gameFarmVariableId: 1 },
        { name: "Potato Sprouts", growthTimeTotal: 6, commonEventId: 122, gameFarmVariableId: 2 },
        { name: "Cabbage Seeds", growthTimeTotal: 6, commonEventId: 124, gameFarmVariableId: 1 },
        { name: "Radish Seeds", growthTimeTotal: 6, commonEventId: 126, gameFarmVariableId: 2 },
        { name: "Onion Seeds", growthTimeTotal: 6, commonEventId: 127, gameFarmVariableId: 1 },
        { name: "Strawberry Seed", growthTimeTotal: 6, commonEventId: 128, gameFarmVariableId: 2 },
        { name: "Pumpkin Seeds", growthTimeTotal: 6, commonEventId: 129, gameFarmVariableId: 2 },
        { name: "Bean Seeds", growthTimeTotal: 6, commonEventId: 130, gameFarmVariableId: 2 },
        { name: "Cashew Seeds", growthTimeTotal: 6, commonEventId: 131, gameFarmVariableId: 2 },
        { name: "Tomato Seeds", growthTimeTotal: 6, commonEventId: 132, gameFarmVariableId: 3 },
        { name: "Wheat Seeds", growthTimeTotal: 6, commonEventId: 133, gameFarmVariableId: 1 },
        { name: "Zucchini Seeds", growthTimeTotal: 6, commonEventId: 134, gameFarmVariableId: 3 },
        { name: "Lettuce Seeds", growthTimeTotal: 6, commonEventId: 135, gameFarmVariableId: 1 },
        { name: "Blueberry Seeds", growthTimeTotal: 6, commonEventId: 136, gameFarmVariableId: 3 },
        { name: "Broccoli Seeds", growthTimeTotal: 6, commonEventId: 137, gameFarmVariableId: 3 },
        // Add more seed entries as needed
    ];
    

    const gameFarmVariableIdFilter = $gameVariables.value(57);

    const filteredSeeds = seedCommonEvents.filter(seed => {
        const seedItem = $dataItems.find(item => item && item.name === seed.name);
        const hasSeedBagNotetag = seedItem && /<seedbag:true>/i.test(seedItem.note);
        return seed.gameFarmVariableId === gameFarmVariableIdFilter && hasSeedBagNotetag;
    });

    const seedChoices = filteredSeeds.map(seed => ({
        name: seed.name,
        commonEventId: seed.commonEventId,
        growthTimeTotal: seed.growthTimeTotal || 6
    }));

    if (seedChoices.length > 0) {
        // Initialize Choices Array using Variable #60
        $gameVariables.setValue(60, seedChoices.map(seed => seed.name));

        // Set Message Choices
        $gameMessage.setChoices($gameVariables.value(60), 0, -2);

        // Set Message Background and Position (if needed)
        $gameMessage.setChoiceBackground(0);
        $gameMessage.setChoicePositionType(1);

        // Record Outcome in Variable #1
        $gameMessage.setChoiceCallback(choiceIndex => {
            if (choiceIndex === -2) {
                $gameTemp.reserveCommonEvent(20);
                return;
            }

            const selectedSeed = seedChoices[choiceIndex];
            const selectedSeedName = selectedSeed.name;
            const selectedEventId = selectedSeed.commonEventId;
            const growthTimeTotal = selectedSeed.growthTimeTotal;

            if (selectedEventId) {
                $gameTemp.reserveCommonEvent(selectedEventId);
            } else {
                $gameMessage.add(`You planted ${selectedSeedName}! Growth time: ${growthTimeTotal} days.`);
            }

            // Adjust as needed based on your game logic
            // const selectedSeedId = selectedSeed.id;
            // $gameParty.gainItem($dataItems[selectedSeedId], -1);
        });

        // Add this otherwise it will not work.
        $gameMap._interpreter.setWaitMode("What seed would you like to plant");
    } else {
        $gameMessage.add("You don't have any seed bags.");
    }
}
I wish I could help you with this but I have a boundary of not assisting with anything that was written using AI. Good luck with it though!
It wasn't written using AI the AI is an assistant for me a novice programmer. It's one of those where if I forget what something does I consult with it and ask for information. So say for instance I wanted to know how to use "!==" or what it meant i'd simply ask it to explain how it works and give a few examples of its usage. If thats ya'lls boundary then it's no different from saying that you wouldn't assist someone to learn to code through a textbook. The only difference is ChatGPT is free and a textbook is not. But that could be said of free information on the internet to...
It wasn't written using AI the AI is an assistant for me a novice programmer. It's one of those where if I forget what something does I consult with it and ask for information. So say for instance I wanted to know how to use "!==" or what it meant i'd simply ask it to explain how it works and give a few examples of its usage. If thats ya'lls boundary then it's no different from saying that you wouldn't assist someone to learn to code through a textbook. The only difference is ChatGPT is free and a textbook is not. But that could be said of free information on the internet to...
If you'd explained that and actually respected my boundary I'd have changed my mind, but the fact that you can't even acknowledge my right to have it while correcting my misunderstanding has ensured that won't happen.
Then why even post if you weren't going to assist? Hows that any different to say that the tools that you used are bad and the ones that you use are good but despite having the knowledge to assist someone else in a situation that is asking for help you chose to state that you could but chose not to due to a boundary that only exists from a tool? Id understand if I fully designed from Chat but like I said above "I'm not a master programmer and alot of this is written with ChatGPT's help on some things." To me that means that someone utilizes it similar to anything else
Then why even post if you weren't going to assist?
Because I was responding to literally every other post in the recents and didn't want to give the impression that I was ignoring this for some reason.
We get a post like this at least once a month where someone dumps ChatGPT code and expects a programmer to fix it. Predictably no one will fix it. Almost all the time a plugin already exists for it.

I don't know why you didn't use Caethyril's select item filter. It's what I would use if I wanted to do this.
Like I said would you blame a mechanic that fixes your car with the assistance of an assistant? I wrote the code here. ChatGPT assisted me in writing the code. So next time you would like someone to replace a motor or transmission you expect them to do it alone or receive help? Most of this information is locked behind ya'lls pseudo walls anyway of magical fairy information like the last two posts i've received.
Like I said would you blame a mechanic that fixes your car with the assistance of an assistant? I wrote the code here. ChatGPT assisted me in writing the code. So next time you would like someone to replace a motor or transmission you expect them to do it alone or receive help? Most of this information is locked behind ya'lls pseudo walls anyway of magical fairy information like the last two posts i've received.
Like I said. If you'd just gone "I respect your boundary in cases where it was entirely AI-generated, but I wrote it myself, I just used ChatGPT for reminders of how certain things work!" I'd have been like "Oh, sorry! That's a different story, let me see if I can figure this out..." and we'd be well on our way to solving your problem.

But ya didn't. You implied that it's not a valid boundary to have, and that I have no valid reason for having it. That does not endear you to me, and in this exchange I'm the one with the knowledge and you're the one who needs something, so I hope I don't have to explain to you why endearing yourself to the other party is only to your advantage here. 😛
I gave you a recommendation: use an existing plugin where the work is already done. It is a recommendation I would personally do myself. You ignored it.
So this is how this forum operates then huh? I guess I won't worry about asking for assistance again in the future. Because the plugin will add additional to the large amount of plugins already in use. Trying to reduce my plugin dependencies currently not increase them

Golden rule of knowledge. Those who hoard it are consumed by it. Library of Alexandria...
So this is how this forum operates then huh? I guess I won't worry about asking for assistance again in the future. Because the plugin will add additional to the large amount of plugins already in use. Trying to reduce my plugin dependencies currently not increase them
Even now, literally all you have to say is "Sorry for dismissing your boundary, I do respect it. Could you please help me now that I've explained I wrote this myself?" and I'd do it.

But I don't imagine you will, because you've already convinced yourself that you're the poor innocent victim here and we're the mean bullies hoarding our knowledge instead of sharing it, when the only reason that happened in the first place was your doubling down on how right you are instead of acknowledging that getting all passive-aggressive with the people you're asking for help from isn't going to get you the results you want.
Because the plugin will add additional to the large amount of plugins already in use. Trying to reduce my plugin dependencies currently not increase them
Your ChatGPT code is written to be put in a plugin file. You're rejecting a premade plugin so you can use your ChatGPT plugin instead.
I never said I disrespected your boundary at all. I never said I respect it either. I simply sidestepped the question due to how alot of people react to those questions now. I gave you the respect of agreeing to disagree. Gave my opinion on the subject and ya'll continued to say "I know how to do it but not gonna tell ya."
Didnt get Aqua till after. I have a farming plugin that this portion goes into. Its a function inside of the plugin itself but is not called by the plugin it's called by a script in a event.
I never said I disrespected your boundary at all. I never said I respect it either. I simply sidestepped the question due to how alot of people react to those questions now. I gave you the respect of agreeing to disagree. Gave my opinion on the subject and ya'll continued to say "I know how to do it but not gonna tell ya."
Didnt get Aqua till after. I have a farming plugin that this portion goes into. Its a function inside of the plugin itself
"If thats ya'lls boundary then it's no different from saying that you wouldn't assist someone to learn to code through a textbook. The only difference is ChatGPT is free and a textbook is not. But that could be said of free information on the internet to..."

^ this is not sidestepping.

The one and only single thing I've ever been asking of you since this exchange started is an apology and you've still been unable to do it. XD
You know what i'm not going to argue semantics with a person over the internet. I respect your boundary. Now please respect mine as well.
You know what i'm not going to argue semantics with a person over the internet. I respect your boundary. Now please respect mine as well.
You haven't stated one.
"You know what i'm not going to argue semantics with a person over the internet." <= Boundary

Original thread: https://forums.rpgmakerweb.com/threads/rpg-maker-mz-javascript-show-choices-help.165548/