RMMV Struct parameter problem- JSON.parse doesn't work

A_Higher_Plane

Veteran
Veteran
Joined
Oct 3, 2015
Messages
314
Reaction score
44
First Language
Russian
Primarily Uses
RMMV
Please help me out. I have been doing just this for hours. I am learning RMMV plugin development. Here is my code:

/*:
*@author Yevgeniy Kizhner
*@plugindesc This is a testing plugin.

*@param Cus
*@type struct<Custom>

*@help Just testing things out!
*/

/*~struct~Custom:
*@param Custom Energy
*@type boolean
*@default false

*@param Custom Energy Name
*@type text
*/

var params = PluginManager.parameters("MyPlugin")["Cus"];
console.log(params);
console.log(params["Custom Energy Name"]);
//console.log(JSON.parse(params));
var val = JSON.parse(params);
console.log(val);
console.log(val['Custom Energy Name']);

var Win = Window_Gold.prototype.refresh;
Window_Gold.prototype.refresh = function() {
Win.apply(this, arguments);
var energy1 = JSON.parse(params);
var energy2 = energy1["Custom Energy Name"];
this.drawText(energy2, 2, 0, 20, 'left');

I get the error "unexpected end of json input" at the Json.parse code.
 
Last edited:

Dev_With_Coffee

Veteran
Veteran
Joined
Jul 20, 2020
Messages
680
Reaction score
320
First Language
PT-BR
Primarily Uses
RM2k3
From what I've seen, the spaces between the letters don't work, you need to use underline.
  • Custom_Energy
  • Custom_Energy_Name
Because the JSON function will convert to an object and the space will make it understand that it is not an "Object":

{"Custon_Energy":123,"Custom_Energy_Name":"Fire"}
Object.Custom_Energy return 123
 
Last edited:

A_Higher_Plane

Veteran
Veteran
Joined
Oct 3, 2015
Messages
314
Reaction score
44
First Language
Russian
Primarily Uses
RMMV
@Dev_With_Coffee seems to have solved this issue for me! Thanks. But please help me with my one other issue in this code. Here it is:

I have added a conditional about a boolean parameter in the plugin. But it executes the first condition regardless of whether that plugin's parameter is true or false. It always fulfils that condition. What is wrong? Please help me out!!

Here is the code:

var energyType;
var energy1 = JSON.parse(params);
var chosenNRG = energy1["Custom_Energy"];
if(!chosenNRG) {energyType = "Ki"; console.log(1);}
else{
console.log(2);
energyType = energy1["Custom_Energy_Name"];

}
 

Beckx

Villager
Member
Joined
Mar 29, 2021
Messages
20
Reaction score
185
First Language
german
Primarily Uses
RMMV
this could on the one hand be one of those examples where Javascript is really wonky.
is your value really a boolean value or maybe a string "true" or a number?
have you logged chosenNRG?
the "!" in front negates the value you're getting, so in your condition you're actually asking if chosenNRG is false or not set and then executes the first stuff.
(sorry english isn't my first language and i can't explain stuff right now)

try removing the ! in your condition, i think that's you're problem
edit: wait no. that's what you want to do here right? if there is no value, set it to "Ki" otherwise use the value? if so, the "!" should be correct... i think it's one of those JS issues, but it's hard to tell without knowing what chosenNRG is supposed to be. at least if you're actually getting something from energy1 it should work, so you should log that before and check what you're getting

you could try setting your variable with var chosenNRG = energy1["Custom_Energy"] || false;
(or || null)

edit 2:
Do you actually have a value at "Custom_Energy" or is the name of that energy maybe something different and you maybe forgot to set the correct name here?
Because there would also be a potential error in your else part where you want to set your energy to params["Custom_Energy_Name"]
 
Last edited:

A_Higher_Plane

Veteran
Veteran
Joined
Oct 3, 2015
Messages
314
Reaction score
44
First Language
Russian
Primarily Uses
RMMV
@Beckx I have done some of the things you have said but nothing. Thanks for at least trying to help me out though!

I still need help. Anyone can help me out here.

Here is the JS struct in my code:

/*~struct~Custom:
*@param Custom_Energy
*@type boolean
*@default false

*@param Custom_Energy_Name
*@type text

*/
 

Dev_With_Coffee

Veteran
Veteran
Joined
Jul 20, 2020
Messages
680
Reaction score
320
First Language
PT-BR
Primarily Uses
RM2k3
chosenNRG will always exist, so use the "!" in the condition it will have no effect.

Try:
JavaScript:
if (chosenNRG == false) {
    energyType = "Ki";
}else{
    energyType = energy1["Custom_Energy_Name"];
}

Try to make code cleaner visually, because at first I barely understood.
 

Dev_With_Coffee

Veteran
Veteran
Joined
Jul 20, 2020
Messages
680
Reaction score
320
First Language
PT-BR
Primarily Uses
RM2k3
It could be that the parameter is returning a String, try with quotes:
== "false"

Good luck
 

A_Higher_Plane

Veteran
Veteran
Joined
Oct 3, 2015
Messages
314
Reaction score
44
First Language
Russian
Primarily Uses
RMMV
You've solved it for me! If these threads get locked, then you guys can lock this thread.
 

Latest Threads

Latest Posts

Latest Profile Posts

So many ideas, so little energy.
SA57Face.pngSA57_IDLE.gifThis is just a "Chinese opera actor" who is totally not a serial killer. Now, we just need to clean up a pile of dead bodies found in his backyard. :kaojoy:
E7DsT3VXsAInVpu

working on my avatar, because i couldn't stand looking at it anymore :D
still not finished though...
alpacaraptors = love!

Forum statistics

Threads
113,585
Messages
1,076,017
Members
147,571
Latest member
haleszarz
Top