Need help with $gameMessage.setChoiceCallback

I want to present the player with a choice and do something based on the choice they select.


I am able to get the choice to popup and receive the given choice, but I am unable to keep track of useful variables that were encapsulated in the function that assigns the callback.


Farming.prototype.PlantSeedSelection = function (obj) {

//Collect all player items that are crops and create a choice array from them
var partyItems = $gameParty.allItems();
var possibleSeeds = [];
for (var i = 0; i < partyItems.length; i++) {
//TODO: Filter by notetags
possibleSeeds.push(partyItems);
}

if (possibleSeeds.length < 1)
{
$gameMessage.add("Go get some seeds!");
return;
}

//Display choices
$gameMessage.add("Choose a seed.");
choices = []; params = [];
for (var i = 0; i < possibleSeeds.length; i++) {
choices.push(possibleSeeds.name);
}

$gameMessage.setChoices(choices, 0);
$gameMessage.setChoiceCallback(function (choice) {
console.log(choice);
$farming.PlantSeed(obj, possibleSeeds[choice]); //ERROR. These parameters are not accessible
});

params.push();
}




I have tried to store the obj and choices in the $farming object (Which is a solution I wasn't fond of) but even that doesn't work.


Do I need to clone the objects in order to prevent them from being wiped?


Ideally I would be able to pass all my parameters into the callback because that would be the cleanest in my mind.


Please let me know if you have any ideas!
$gameMessage.setChoiceCallback(function (choice) {
console.log(choice);
$farming.PlantSeed(obj, possibleSeeds[choice]); //ERROR. These parameters are not accessible
}.bind(this); // <---- this is different.


the bind function will take the function it's called on (in this and most cases a callback given to a function) and make 'this' refer to what's passed in. By doing this we're giving the callback access to everything the current object, in this case the instance of Farming. However this alone won't fix your issue. The variables obj and possibleSeeds are local variables, and thus will not be stored after the function ends, so you'll still get errors. Make them variables of the object (this.obj and this.possibleSeeds) in order to keep their values for use at a later time.
Hey Zalerinian! Thanks for that. That does get me closer, but I have a problem now with the selectedObj not actually keeping its values.


When I assign this.selectedObj, it receives the correct values, but once that function has been called all values are all reset.



obj in this context refers to a Game_Interpreter object that gets passed in from an events script field. It seems to me like I am keeping a reference to an object that gets refreshed internally. What would you recommend I do in this case? 
 


-Take the appropriate data from the game interpreter and put it into an ambiguous object that gets passed through the method?


-Make a deep copyof the Game_Interpreter obj?


EDIT: I figure I could just pass through the event ID instead of the entire object since I only care about the event ID anyway. That should solve my problem. Will post update later


EDIT2: That solved the problem and probably makes more logical sense! Thanks again Zalerinian, I have marked your post as best answer 🙂

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-gamemessage-setchoicecallback.66484/