Hello, I am Raith and this is my first time joining and posting a question in RPGMakerWeb despite of almost a decade digging in this forum.
I am trying to create a crop-planting system similar to one found in Pokemon Ruby/Sapphire/Emerald, and I am trying to use regular event Script Call without any plugin help. So far, the code below is working - choosing what to plant, creating a dynamic choice message according to what seed you have, reducing the amount of seed in posession, activating self-switch for the next page etc.
However, I seemed to have bad experience on "this" keyword usage in the Script Call and desperately need your help.
The "this" problem is first noted when I use $gameSelfSwitches.setValue([this._mapId, this._eventId, "A"], true) for the choice response. That code is not working. Instead, the array must be stored first into a variable before finally working nicely
The second "this" problem is when I use "this" to declare "this._selectedCrop" variable. That code is working, the value is even passable to the second page after any switch is activated (which is great). However, that value is unchangeable in the choice response codes, which created headache for me.
Below is my code:
Oh JavaScript masters, please enlighten me!
I am trying to create a crop-planting system similar to one found in Pokemon Ruby/Sapphire/Emerald, and I am trying to use regular event Script Call without any plugin help. So far, the code below is working - choosing what to plant, creating a dynamic choice message according to what seed you have, reducing the amount of seed in posession, activating self-switch for the next page etc.
However, I seemed to have bad experience on "this" keyword usage in the Script Call and desperately need your help.
The "this" problem is first noted when I use $gameSelfSwitches.setValue([this._mapId, this._eventId, "A"], true) for the choice response. That code is not working. Instead, the array must be stored first into a variable before finally working nicely
The second "this" problem is when I use "this" to declare "this._selectedCrop" variable. That code is working, the value is even passable to the second page after any switch is activated (which is great). However, that value is unchangeable in the choice response codes, which created headache for me.
Below is my code:
Code:
//The working variables
var crop_1 = $dataItems[13];
var crop_2 = $dataItems[14];
var cancel = "Cancel"
//the problematic variables
var identifier = [this._mapId, this._eventId, "A"];
this._selectedCrop = 0;
//Creating the array of choice for plantable seed (working nicely)
var crops = [cancel];
if ($gameParty.hasItem($dataItems[13]) >= 1) {crops.unshift(crop_1.name)};
if ($gameParty.hasItem($dataItems[14]) >= 1) {crops.unshift(crop_2.name)};
//Creating the choice message (working nicely)
$gameMessage.setChoices(crops, crops.length - 1, crops.length - 1);
//Creating the choice response
$gameMessage.setChoiceCallback(function(responseIndex){
if(responseIndex == crops.indexOf(crop_1.name)) {this._selectedCrop = 1; $gameParty.loseItem(crop_1, 1, false); $gameSelfSwitches.setValue(identifier, true);};
if(responseIndex == crops.indexOf(crop_2.name)) {this._selectedCrop = 2; $gameParty.loseItem(crop_2, 1, false); $gameSelfSwitches.setValue(identifier, true);};
});
this.setWaitMode('message');
Oh JavaScript masters, please enlighten me!