Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

Plugin parameter returning "true" when set to "false"?

Status
Not open for further replies.

Diremane

Villager
Xy$
0.00
So first I should probably mention I'm completely new to scripting in any form, and all my knowledge comes from video tutorials and a small amount of online research. I've been trying to apply what I learn and do something more than what I was shown with it, to be sure I'm actually learning something, which led to this more-complex-than-intended variation on a plugin to overlay a tiled sprite on the title screen (as per one of Soul's video tutorials). The intent is to be able to not only scroll the image in custom directions/speeds, but to have the option of "bouncing" or "wiggling" as well (reversing movement along the X and/or Y axes at set intervals). The result so far is that movement of the tilesprite is handled exactly as I intended it to be, but for some reason it seems to be setting one of my parameters to "true" regardless of what I set it to in the plugin manager.

Here's the script so far:
//====================================================================
// Sprite Test.js
//====================================================================

/*:
* @param Image
* @desc The image you are using
* @default Image
*
* @param Image Opacity
* @desc The level of opacity for your image
* @default 0
*
* @param X Axis Origin
* @desc The starting position of the image on the X axis
* @default 0
*
* @param Y Axis Origin
* @desc The starting position of the image on the Y axis
* @default 0
*
* @param X Axis Increment
* @desc The speed of the image's horizontal movement, in pixels per increment
* @default 0
*
* @param Y Axis Increment
* @desc The speed of the image's vertical movement, in pixels per increment
* @default 0
*
* @param Wiggle
* @desc Periodically reverse direction along the X axis
* @default false
*
* @param Bounce
* @desc Periodically reverse direction along the Y axis
* @default false
*
* @param Wiggle Distance
* @desc Number of increments between direction change along X axis
* @default 5
*
* @param Bounce Distance
* @desc Number of increments between direction change along Y axis
* @default 5
*/

var params = PluginManager.parameters("Sprite Test");
var my_image_name = String(params['Image'] || "Image");
var myImageOpacity = Number(params['Image Opacity'] || 0);
var myXPos = Number(params['X Axis Origin'] || 0);
var myYPos = Number(params['Y Axis Origin'] || 0);
var myXIncrement = Number(params['X Axis Increment'] || 0);
var myYIncrement = Number(params['Y Axis Increment'] || 0);
var myWiggle = Boolean(params['Wiggle'] || false);
var myBounce = Boolean(params['Bounce'] || false);
var myWiggleDistance = Number(params['Wiggle Distance'] || 5);
var myBounceDistance = Number(params['Bounce Distance'] || 5);

var myWiggleCount = 0;
var myBounceCount = 0;
var myWiggleReverse = false;
var myBounceReverse = false;

var createALIAS = Scene_Title.prototype.create;
var updateALIAS = Scene_Title.prototype.update;

Scene_Title.prototype.myImage;

Scene_Title.prototype.create = function () {
createALIAS.call(this);
this.create_my_image();
};

Scene_Title.prototype.create_my_image = function () {
this.myImage = new TilingSprite();
this.myImage.bitmap = ImageManager.loadPicture(my_image_name);
this.myImage.opacity = myImageOpacity;
this.myImage.x = myXPos;
this.myImage.y = myYPos;
this.myImage.move(0, 0, Graphics.width, Graphics.height);
this.addChild(this.myImage);
};

Scene_Title.prototype.update = function () {
updateALIAS.call(this);
if (myWiggle === true && myBounce === true) {
if (myWiggleReverse === false && myWiggleCount < myWiggleDistance) {
this.myImage.origin.x += myXIncrement;
myWiggleCount += 1;
} else if (myWiggleReverse === false && myWiggleCount === myWiggleDistance) {
myWiggleReverse = true;
} else if (myWiggleReverse === true && myWiggleCount !== 0) {
this.myImage.origin.x -= myXIncrement;
myWiggleCount -= 1;
} else if (myWiggleReverse === true && myWiggleCount === 0) {
myWiggleReverse = false;
}

if (myBounceReverse === false && myBounceCount < myBounceDistance) {
this.myImage.origin.y += myYIncrement;
myBounceCount += 1;
} else if (myBounceReverse === false && myBounceCount === myBounceDistance) {
myBounceReverse = true;
} else if (myBounceReverse === true && myBounceCount !== 0) {
this.myImage.origin.y -= myYIncrement;
myBounceCount -= 1;
} else if (myBounceReverse === true && myBounceCount === 0) {
myBounceReverse = false;
}
} else if (myWiggle === true && myBounce === false) {
if (myWiggleReverse === false && myWiggleCount < myWiggleDistance) {
this.myImage.origin.x += myXIncrement;
myWiggleCount += 1;
} else if (myWiggleReverse === false && myWiggleCount === myWiggleDistance) {
myWiggleReverse = true;
} else if (myWiggleReverse === true && myWiggleCount !== 0) {
this.myImage.origin.x -= myXIncrement;
myWiggleCount -= 1;
} else if (myWiggleReverse === true && myWiggleCount === 0) {
myWiggleReverse = false;
}

this.myImage.origin.y += myYIncrement;
} else if (myWiggle === false && myBounce === true) {
if (myBounceReverse === false && myBounceCount < myBounceDistance) {
this.myImage.origin.y += myYIncrement;
myBounceCount += 1;
} else if (myBounceReverse === false && myBounceCount === myBounceDistance) {
myBounceReverse = true;
} else if (myBounceReverse === true && myBounceCount !== 0) {
this.myImage.origin.y -= myYIncrement;
myBounceCount -= 1;
} else if (myBounceReverse === true && myBounceCount === 0) {
myBounceReverse = false;
}

this.myImage.origin.x += myXIncrement;
} else if (myWiggle === false && myBounce === false) {
this.myImage.origin.x += myXIncrement;
this.myImage.origin.y += myYIncrement;
}
};

console.log("myWiggle = " + myWiggle);
console.log("myBounce = " + myBounce);

Even with the little I know, I'm pretty sure I can cut some things out or work in shortcuts, but I'm more concerned about the mystery around the "myBounce" variable; I've been testing it with the Wiggle parameter set to true and the Bounce parameter set to false, but after adding my debug console logs to the end, it's telling me both are "true" when i start my test play. Am I missing something, is it pulling the value from somewhere else? Any help understanding this would be appreciated.

Edit: I've done a little further testing, and apparently no matter what I set the Wiggle and Bounce parameters to, they're responding true to the variables assigned to them. I tried setting them both to false manually, in the script, and they stayed false, so apparently it is definitely happening when it grabs that value from that parameter, but I don't understand why.
 
Last edited:

Run

Towns Guard
Xy$
0.00
Solved it.
The String false being passed in as a parameter returns true.
Most Strings, except maybe an empty one will return true as a boolean.

You can solve it by changing var myBounce = Boolean(params['Bounce'] || false);
to var myBounce = (Boolean(params['Bounce']) == 'true');
 
Last edited:

LTN Games

Master Mind
Resource Team
Xy$
0.01
Looking at the code gave me a headache lol, not trying to sound mean but it's extremely difficult to read your code with so many nested statements. Are you up for some feedback and recommendations to help improve your script? If so let me know and I will do a quick re-write of your code and explain the parts I changed.
 

Diremane

Villager
Xy$
0.00
@Run, so to be sure I'm understanding this right...the parameters being entered as "true" and "false" aren't being taken as boolean values, but as strings? Is there no way to enter a boolean value into the plugin manager, then? Just want to make sure I know what I did wrong. Thanks for the fix!

@LTN Games, lol that's fair, it's the only real script I've attempted, and it was really just to test myself. I would love some feedback though, if you're up for it. I've been relying solely on video tutorials and what I can puzzle out of what I've seen, so far.
 

Run

Towns Guard
Xy$
0.00
@Diremane As far as I know, all the parameters being entered are String values. Boolean(value), Number(value) and so on, are used to change those Strings into other types.
 

Diremane

Villager
Xy$
0.00
I think I understand. I had thought that Boolean(params['Bounce'] || false) meant it would grab the first value in the parentheses that returned a boolean, which I guess was a misunderstanding; so with (Boolean(params['Bounce']) == 'true') it's checking the entered parameter to see if it is the string "True", then returning false if it's not? Is that right?
 

Run

Towns Guard
Xy$
0.00
@Diremane That's correct.
I had thought that Boolean(params['Bounce'] || false) meant it would grab the first value in the parentheses that returned a boolean, which I guess was a misunderstanding;
I believe this is correct too. However the value of the String "false" is actually the Boolean value true.
This is because Boolean() treats anything that is a "Real" value as true and every other value as false.
The examples from this page may help you understand it a little better.
http://www.w3schools.com/js/js_booleans.asp
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Here is the first part I would highly recommend getting used to, it's a great way of creating parameters and variables.

JavaScript:
var DM = DM || {};
// Dev Name Space, This is excellent for storing any number of things, from
// Strings to booleans, objects and aliases. I would highly recommend using this
// technique for all plugins you create. You may use any initials you like, I picked
// DM for you because your community name is Diremane.

DM.SpriteTest = DM.SpriteTest || {};
// I'm not 100% sure why some scripters do this, but my conclusion is for other 
// users to  be able to create compatibility patches. A simple conditional will
// check if the plugin is in the users plugin list. You should notice, I am 
// using my newly created dev namespace to store this in.


DM.Param = DM.Param || {};
// Create you params that will be used in your script and interacts with plugin manager.
// Again I'm using dev namespace to store my parameters and create a new object called
// Param, which is a part of DM. 

DM.Parameters = PpluginManager.parameters("Sprite Test");
//Assign DM.Parameters to get the current plugins param information.


DM.Param.myImageName    = String(DM.Parameters['Image FileName']);
DM.Param.imageOpacity   = Number(params['Image Opacity'] || 0);
DM.Param.xPos           = Number(params['X Axis Origin'] || 0);
DM.Param.yPos           = Number(params['Y Axis Origin'] || 0);
DM.Param.xIncrement     = Number(params['X Axis Increment'] || 0);
DM.Param.yIncrement     = Number(params['Y Axis Increment'] || 0);

// I changed Wiggle & bounce to a string and convert it to lowerCase, I personally
//Don't use Boolean for parameters and never had done so, I think it's preference
// so this is up to you. I also changed the parameter names from Wiggle to 
// Wiggle Value, I find names for parameters should be dot notation.
DM.Param.wiggle         = String(params['Wiggle Value'] || 'false').toLowerCase();
DM.Param.bounce         = String(params['Bounce Value'] || 'false').toLowerCase();
DM.Param.WiggleDistance = Number(params['Wiggle Distance'] || 5);
DM.Param.BounceDistance = Number(params['Bounce Distance'] || 5);

Also when alising with your dev namespace it would go something like this.

JavaScript:
DM.ST_oldSceneTitle_create = Scene_Title.prototype.create;
// DM is our namespace, we store our alias inside this. Notice how I named the alias
// this is great to do because it's unique. So I use Dev namespace then the
// scripts initials Sprite Test = ST, then I type oldClassName_functionname
I will get around to the rest of your code, but my recommendation is putting a majority of those nested statments into their own functions which can then be called into the update function, a lot of issues can arise when you alias a function like that and slam it with a ton of code especially statments.
 

Diremane

Villager
Xy$
0.00
Oh wow! Took a minute to digest it all, but that was super helpful, thanks a ton! I'm not quite sure I understand the purpose of the "|| {}" at the end of some of the lines here. Sorry if that's a dumb question; I've learned everything I know by seeing it done, so far. Seems an easy enough habit to get into, though, and I can ignore it if I'm asking too many questions, lol.
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Ask all the questions you need it's the only way to learn. As for the || {}, I asked the same question when I first seen it, So first Ill explain each individual part The double || symbol literally means "or", and when declaring an empty object you use {} so with this new knowledge try and read it for yourself. Check comment to see how to read it
Code:
var DM = DM || {};
// var declare a new variable named DM
// make it equal to itself
// OR
// an empty object
Also, take a look at this Thread, and at the bottom of it, @Soul has a pretty good explanation on the dev namespace container in case you want to learn more, I find it helps quite a bit to hear more than one person explain it in their own ways.
 

Diremane

Villager
Xy$
0.00
So wouldn't it always become equal to itself? Or--Oh wait, so when it's established, there's nothing in that variable for it to equal, right? So it becomes an empty object, which is later filled in further down the script--but at that point it's identified, so the next script I write that starts off with the devnamespace will inherit all those values? Is it something like that?

I feel like I either understand it or got it wrong to an embarrassing degree, lol.
 

Boy Who Codes

Praised Adventurer
Xy$
0.00
About the true or false thing, actually, all of those who commented are right. All parameter values are strings. So if you want to use a true or false function for any of your plugins in the future, do something like this:

Code:
var direMane_trueFalse = PluginManager.parameters('DireManeTest')['ShowFace'] === "true" ? true : false;
PluginManager.parameters('DireManeTest')['ShowFace'] is basically taking the value of the parameter ShowFace and store it to direMane_trueFalse. What I did is to make the value of direMane_trueFalse to "true", and then convert its value to boolean true. If it isn't true then it automatically says that it is valued false.

When I was starting I also had this same problem. Doing boolean functions are tricky in JS, compared to other languages.
 

Diremane

Villager
Xy$
0.00
@Soul, ahh it's really cool to hear from you, I dove into all this with your youtube tutorials just a few days ago and that's where I've learned just about everything I know, haha. Thanks for the tip! Having to store a string and check for "true" every time felt like unnecessary work compared to just being able to check for (value) or (!value).
 

Boy Who Codes

Praised Adventurer
Xy$
0.00
It would be the same after :)

Code:
if(direMane_trueFalse)
You can do conditional checks like this after doing that on the parameter. I rarely used this on my first scripts because using the true / false strings are much easier and less errors when someone mistyped anything rather than the word true, but its pedestrian, so using === "true" ? true : false is much better. :)
 

eivl

Local Hero
Xy$
0.00
JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:

JavaScript:
'' == '0'   // returns false
    0 == ''             // returns true
    0 == '0'            // returns true

    false == 'false'    // returns false
    false == '0'        // returns true

    false == undefined  // returns false
    false == null       // returns false
    null == undefined   // returns true

    ' \t\r\n ' == 0     // returns true
There are much more to tell about this, but let us leave that to another time.
 
Status
Not open for further replies.
Top