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

A_Higher_Plane

Veteran
Veteran
Joined
Oct 3, 2015
Messages
339
Reaction score
48
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
755
Reaction score
372
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
339
Reaction score
48
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
28
Reaction score
246
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
339
Reaction score
48
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
755
Reaction score
372
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
755
Reaction score
372
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
339
Reaction score
48
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 Profile Posts

It's a shame that Selection Control will not be ported to MZ. I found a neat way of using it to enable ways for tanks to protect squishies. lol
Lately I have been taking a break from game and resource making so I won't get burnt out. But I am also interested in the Game Jams this site periodically holds. It'd be great if I knew when the next one, if any, is planned.
elexy 1.2.pngSome bust art to be cropped and cleaned. May use in special decision moments. Depending on how it looks in game I may cut it.
Tiamat-86 wrote on ATT_Turan's profile.
apparently since its turning 15 years old this year the ps3 is considered "retro". now how old do you feel?
guess that means im an "antique" gamer since ill still play games as old as i am.
"you know your getting old when your childhood console changes ethnicity" lol it turned yellow
I'm making Toilet in Wonderland 2. and tell vinny about it lol

Forum statistics

Threads
115,347
Messages
1,089,184
Members
150,026
Latest member
Ven_0
Top