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

A_Higher_Plane

Veteran
Veteran
Joined
Oct 3, 2015
Messages
339
Reaction score
47
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
737
Reaction score
360
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
47
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
245
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
47
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
737
Reaction score
360
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
737
Reaction score
360
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
47
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

How do I find the threads I have watched? It's been so long I've forgotten :kaoswt:
So far, this is how the wall tileset its looking in-editor. :kaoswt2:
1631863283853.png
Still thinking on details to add.
Added a plugin to RMMV Plugin Releases with all relevant info but it looks like the topic wasn't approved for whatever reason. Cool.
Working in the IT industry:
- "How long does it take?".
- "It takes 1 month".
- "What if we add more people to the project?".
- "It would take 2 months".
Doing RPG Maker News for 16th September 2021

Forum statistics

Threads
115,126
Messages
1,087,424
Members
149,619
Latest member
IMOKO
Top