How do I make a $gameVariable's value control a value in my plugin or plugin parameter?
$gameVariables in plugins
Learning JavascriptYou can get and set variables like this:
How you use them is up to you. For example, you can take a variable id as a plugin parameter and work with that.
Code:
$gameVariables.value(id);$gameVariables.setValue(id, value);
$gameVariables.setValue(5, Number(parameters['Down']); doesnt work for me.
I am using a parameter called Down assigned to var Down.
And do I have to put this where it will be constantly updated if I want it to control the value in real time?
I am using a parameter called Down assigned to var Down.
And do I have to put this where it will be constantly updated if I want it to control the value in real time?
Please post the whole plugin (in code tags) and given an idea about what you want to accomplish.
maybe you forgot to set variable of parameters
/*:* @plugindesc Breaking things.* @author Gyldstrand* @help help.** @param Down * @desc Which row is your sprite for moving down?* @default 0** @param Left * @desc Which row is your sprite for moving Left?* @default 0* * @param Right * @desc Which row is your sprite for moving Right? * @default 0** @param Up * @desc Which row is your sprite for moving Up?* @default 0** @param DownLeft * @desc Which row is your sprite for moving DownLeft?* @default 0** @param DownRight * @desc Which row is your sprite for moving DownRight?* @default 0** @param UpLeft * @desc Which row is your sprite for moving UpLeft?* @default 0** @param UpRight* @desc Which row is your sprite for moving UpRight?* @default 0** @param ResetPattern* @desc Frame number in the animation cycle that your character* will reset to after moving.* @default 0** @param FrequencyWalk* @desc Frequency of the animation while walking.* @default 1** @param FrequencyDash* @desc Frequency of the animation while dashing. * @default 1.5** @param AnimationWait* @desc Time it takes to change to the next frame of animation. * @default 3** @param AnimationCount* @desc How many frames of animation per step. * @default 0** @param DistanceBase* @desc Combine with DistanceExpo to control formula for distance traveled per step:* DistanceBase ^ (MoveSpeed / DistanceExpo).* @default 2** @param DistanceExpo* @desc Combine with DistanceBase to control formula for distance traveled per step:* DistanceBase ^ (MoveSpeed / DistanceExpo).* @default 256*/(function(){ var parameters = PluginManager.parameters('DiagonalMovement'); var Down = Number(parameters['Down']) || 0; var Left = Number(parameters['Left']) || 0; var Right = Number(parameters['Right']) || 0; var Up = Number(parameters['Up']) || 0; var DownLeft = Number(parameters['DownLeft']) || 0; var DownRight = Number(parameters['DownRight']) || 0; var UpLeft = Number(parameters['UpLeft']) || 0; var UpRight = Number(parameters['UpRight']) || 0; var ResetPattern = Number(parameters['ResetPattern']) || 0; var FrequencyWalk = Number(parameters['FrequencyWalk']) || 0.5; var FrequencyDash = Number(parameters['FrequencyDash']) || 1.5; var AnimationWait = Number(parameters['AnimationWait']) || 3; var AnimationCount = Number(parameters['AnimationCount']) || 0; var DistanceBase = Number(parameters['DistanceBase']) || 2; var DistanceExpo = Number(parameters['DistanceExpo']) || 256; var carryFrameNumber; ImageManager.getCharacterType = function(filename) { var readFiles = filename.match(/\[(d|_)(\d)(p|f|e)]/); if (readFiles && readFiles[3].contains('p')) { return (readFiles[3]); } else if (readFiles && readFiles[3].contains('f')) { return (readFiles[3]); } else if (readFiles && readFiles[3].contains('e')) { return (readFiles[3]); } }; ImageManager.getCharacterFrames = function(filename) { var readFiles = filename.match(/\[(d|_)(\d)(p|f|e)]/); if (readFiles && readFiles[3].contains('p')) { carryFrameNumber = (Number(readFiles[2])); return (Number(readFiles[2])); } else if (readFiles && readFiles[3].contains('f')) { carryFrameNumber = (Number(readFiles[2])); return (Number(readFiles[2])); } else if (readFiles && readFiles[3].contains('e')) { carryFrameNumber = (Number(readFiles[2])); return (Number(readFiles[2])); } }; ImageManager.getCharacterDiagonal = function(filename) { var readFiles = filename.match(/\[(d|_)(\d)(p|f|e)]/); if (readFiles && readFiles[3].contains('p')) { return (readFiles[1]); } else if (readFiles && readFiles[3].contains('f')) { return (readFiles[1]); } else if (readFiles && readFiles[3].contains('e')) { return (readFiles[1]); } }; Sprite_Character.prototype.initialize = function(character) { Sprite_Base.prototype.initialize.call(this); this.initMembers(); this.setCharacter(character); }; Sprite_Character.prototype.initMembers = function() { this.anchor.x = 0.5; this.anchor.y = 1; this._character = null; this._balloonDuration = 0; this._tilesetId = 0; this._upperBody = null; this._lowerBody = null; }; Sprite_Character.prototype.setCharacter = function(character) { this._character = character; }; Sprite_Character.prototype.update = function() { Sprite_Base.prototype.update.call(this); this.updateBitmap(); this.updateFrame(); this.updatePosition(); this.updateAnimation(); this.updateBalloon(); this.updateOther(); }; Sprite_Character.prototype.updateVisibility = function() { Sprite_Base.prototype.updateVisibility.call(this); if (this._character.isTransparent()) { this.visible = false; } }; Sprite_Character.prototype.isTile = function() { return this._character.tileId > 0; }; Sprite_Character.prototype.tilesetBitmap = function(tileId) { var tileset = $gameMap.tileset(); var setNumber = 5 + Math.floor(tileId / 256); return ImageManager.loadTileset(tileset.tilesetNames[setNumber]); }; Sprite_Character.prototype.updateBitmap = function() { if (this.isImageChanged()) { this._tilesetId = $gameMap.tilesetId(); this._tileId = this._character.tileId(); this._characterName = this._character.characterName(); this._characterIndex = this._character.characterIndex(); if (this._tileId > 0) { this.setTileBitmap(); } else { this.setCharacterBitmap(); } } }; Sprite_Character.prototype.isImageChanged = function() { return (this._tilesetId !== $gameMap.tilesetId() || this._tileId !== this._character.tileId() || this._characterName !== this._character.characterName() || this._characterIndex !== this._character.characterIndex()); }; Sprite_Character.prototype.setTileBitmap = function() { this.bitmap = this.tilesetBitmap(this._tileId); }; Sprite_Character.prototype.setCharacterBitmap = function() { this.bitmap = ImageManager.loadCharacter(this._characterName); this._characterType = ImageManager.getCharacterType(this._characterName); this._characterFrames = ImageManager.getCharacterFrames(this._characterName); this._characterDiagonal = ImageManager.getCharacterDiagonal(this._characterName); }; Sprite_Character.prototype.updateFrame = function() { if (this._tileId > 0) { this.updateTileFrame(); } else { if (this._characterType === 'p') { this.updatePlayerFrame(); } else if (this._characterType === 'f'){ this.updateFollowerFrame(); } else if (this._characterType === 'e') { this.updateEventFrame(); }; } }; Sprite_Character.prototype.updateTileFrame = function() { var pw = this.patternWidth(); var ph = this.patternHeight(); var sx = (Math.floor(this._tileId / 128) % 2 * 8 + this._tileId % 8) * pw; var sy = Math.floor(this._tileId % 256 / 8) % 16 * ph; this.setFrame(sx, sy, pw, ph); }; Sprite_Character.prototype.updatePlayerFrame = function() { var pw = this.playerPatternWidth(); var ph = this.playerPatternHeight(); var sx = (this.playerBlockX() + this.playerPatternX()) * pw; var sy = (this.playerBlockY() + this.playerPatternY()) * ph; this.updateHalfBodySprites(); if (this._bushDepth > 0) { var d = this._bushDepth; this._upperBody.setFrame(sx, sy, pw, ph - d); this._lowerBody.setFrame(sx, sy + ph - d, pw, d); this.setFrame(sx, sy, 0, ph); } else { this.setFrame(sx, sy, pw, ph); } }; Sprite_Character.prototype.updateFollowerFrame = function() { var pw = this.followerPatternWidth(); var ph = this.followerPatternHeight(); var sx = (this.followerBlockX() + this.followerPatternX()) * pw; var sy = (this.followerBlockY() + this.followerPatternY()) * ph; this.updateHalfBodySprites(); if (this._bushDepth > 0) { var d = this._bushDepth; this._upperBody.setFrame(sx, sy, pw, ph - d); this._lowerBody.setFrame(sx, sy + ph - d, pw, d); this.setFrame(sx, sy, 0, ph); } else { this.setFrame(sx, sy, pw, ph); } }; Sprite_Character.prototype.updateEventFrame = function() { var pw = this.eventPatternWidth(); var ph = this.eventPatternHeight(); var sx = (this.eventBlockX() + this.eventPatternX()) * pw; var sy = (this.eventBlockY() + this.eventPatternY()) * ph; this.updateHalfBodySprites(); if (this._bushDepth > 0) { var d = this._bushDepth; this._upperBody.setFrame(sx, sy, pw, ph - d); this._lowerBody.setFrame(sx, sy + ph - d, pw, d); this.setFrame(sx, sy, 0, ph); } else { this.setFrame(sx, sy, pw, ph); } }; Sprite_Character.prototype.playerBlockX = function() { if (this._characterType) { return 0; } else { var index = this._character.characterIndex(); return index % 4 * 3; } }; Sprite_Character.prototype.playerBlockY = function() { if (this._characterType) { return 0; } else { var index = this._character.characterIndex(); return Math.floor(index / 4) * 4; } }; Sprite_Character.prototype.playerPatternX = function() { return this._character.pattern(); }; Sprite_Character.prototype.playerPatternY = function() { if (this._characterDiagonal === 'd') { switch(Input.dir8) { case 1: return DownLeft; break; case 2: return Down; break; case 3: return DownRight; break; case 4: return Left; break; case 6: return Right; break; case 7: return UpLeft; break; case 8: return Up; break; case 9: return UpRight; break; default: return ResetPattern; break; } } else { return (this._character.direction() - 2) / 2; }; }; Sprite_Character.prototype.playerPatternWidth = function() { if (this._tileId > 0) { return $gameMap.tileWidth(); } else { return this.bitmap.width / this._characterFrames; } }; Sprite_Character.prototype.playerPatternHeight = function() { if (this._tileId > 0) { return $gameMap.tileHeight(); } else if (this._characterDiagonal === 'd') { return this.bitmap.height / 8; } else { return this.bitmap.height / 4; } }; Sprite_Character.prototype.followerBlockX = function() { if (this._characterType) { return 0; } else { var index = this._character.characterIndex(); return index % 4 * 3; } }; Sprite_Character.prototype.followerBlockY = function() { if (this._characterType) { return 0; } else { var index = this._character.characterIndex(); return Math.floor(index / 4) * 4; } }; Sprite_Character.prototype.followerPatternX = function() { return this._character.pattern(); }; Sprite_Character.prototype.followerPatternY = function() { return (this._character.direction() - 2) / 2; }; Sprite_Character.prototype.followerPatternWidth = function() { if (this._tileId > 0) { return $gameMap.tileWidth(); } else { return this.bitmap.width / this._characterFrames; } }; Sprite_Character.prototype.followerPatternHeight = function() { if (this._tileId > 0) { return $gameMap.tileHeight(); } else if (this._characterDiagonal === 'd') { return this.bitmap.height / 8; } else { return this.bitmap.height / 4; } }; Sprite_Character.prototype.eventBlockX = function() { if (this._characterType) { return 0; } else { var index = this._character.characterIndex(); return index % 4 * 3; } }; Sprite_Character.prototype.eventBlockY = function() { if (this._characterType) { return 0; } else { var index = this._character.characterIndex(); return Math.floor(index / 4) * 4; } }; Sprite_Character.prototype.eventPatternX = function() { return this._character.pattern(); }; Sprite_Character.prototype.eventPatternY = function() { return (this._character.direction() - 2) / 2; }; Sprite_Character.prototype.eventPatternWidth = function() { if (this._tileId > 0) { return $gameMap.tileWidth(); } else { return this.bitmap.width / this._characterFrames; } }; Sprite_Character.prototype.eventPatternHeight = function() { if (this._tileId > 0) { return $gameMap.tileHeight(); } else if (this._characterDiagonal === 'd') { return this.bitmap.height / 8; } else { return this.bitmap.height / 4; } }; Sprite_Character.prototype.updateHalfBodySprites = function() { if (this._bushDepth > 0) { this.createHalfBodySprites(); this._upperBody.bitmap = this.bitmap; this._upperBody.visible = true; this._upperBody.y = - this._bushDepth; this._lowerBody.bitmap = this.bitmap; this._lowerBody.visible = true; this._upperBody.setBlendColor(this.getBlendColor()); this._lowerBody.setBlendColor(this.getBlendColor()); this._upperBody.setColorTone(this.getColorTone()); this._lowerBody.setColorTone(this.getColorTone()); } else if (this._upperBody) { this._upperBody.visible = false; this._lowerBody.visible = false; } }; Sprite_Character.prototype.createHalfBodySprites = function() { if (!this._upperBody) { this._upperBody = new Sprite(); this._upperBody.anchor.x = 0.5; this._upperBody.anchor.y = 1; this.addChild(this._upperBody); } if (!this._lowerBody) { this._lowerBody = new Sprite(); this._lowerBody.anchor.x = 0.5; this._lowerBody.anchor.y = 1; this._lowerBody.opacity = 128; this.addChild(this._lowerBody); } }; Sprite_Character.prototype.updatePosition = function() { this.x = this._character.screenX(); this.y = this._character.screenY(); this.z = this._character.screenZ(); }; Sprite_Character.prototype.updateAnimation = function() { this.setupAnimation(); if (!this.isAnimationPlaying()) { this._character.endAnimation(); } if (!this.isBalloonPlaying()) { this._character.endBalloon(); } }; Sprite_Character.prototype.updateOther = function() { this.opacity = this._character.opacity(); this.blendMode = this._character.blendMode(); this._bushDepth = this._character.bushDepth(); }; Sprite_Character.prototype.setupAnimation = function() { if (this._character.animationId() > 0) { var animation = $dataAnimations[this._character.animationId()]; this.startAnimation(animation, false, 0); this._character.startAnimation(); } }; Sprite_Character.prototype.setupBalloon = function() { if (this._character.balloonId() > 0) { this.startBalloon(); this._character.startBalloon(); } }; Sprite_Character.prototype.startBalloon = function() { if (!this._balloonSprite) { this._balloonSprite = new Sprite_Balloon(); } this._balloonSprite.setup(this._character.balloonId()); this.parent.addChild(this._balloonSprite); }; Sprite_Character.prototype.updateBalloon = function() { this.setupBalloon(); if (this._balloonSprite) { this._balloonSprite.x = this.x; this._balloonSprite.y = this.y - this.height; if (!this._balloonSprite.isPlaying()) { this.endBalloon(); } } }; Sprite_Character.prototype.endBalloon = function() { if (this._balloonSprite) { this.parent.removeChild(this._balloonSprite); this._balloonSprite = null; } }; Sprite_Character.prototype.isBalloonPlaying = function() { return !!this._balloonSprite; }; Game_CharacterBase.prototype.initialize = function() { this.initMembers(); }; Game_CharacterBase.prototype.initMembers = function() { this._x = 0; this._y = 0; this._realX = 0; this._realY = 0; this._moveSpeed = 4; this._moveFrequency = 6; this._opacity = 255; this._blendMode = 0; this._direction = 2; this._pattern = 1; this._priorityType = 1; this._tileId = 0; this._characterName = ''; this._characterIndex = 0; this._isObjectCharacter = false; this._walkAnime = true; this._stepAnime = false; this._directionFix = false; this._through = false; this._transparent = false; this._bushDepth = 0; this._animationId = 0; this._balloonId = 0; this._animationPlaying = false; this._balloonPlaying = false; this._animationCount = 0; this._stopCount = 0; this._jumpCount = 0; this._jumpPeak = 0; this._movementSuccess = true; }; Game_CharacterBase.prototype.distancePerFrame = function() { return Math.pow(DistanceBase, this.realMoveSpeed()) / DistanceExpo; }; Game_CharacterBase.prototype.animationWait = function() { return (9 - this.realMoveSpeed()) * AnimationWait; }; Game_CharacterBase.prototype.updateAnimationCount = function() { if (this.isMoving() && this.hasWalkAnime()) { this._animationCount += FrequencyDash; } else if (this.hasStepAnime() || !this.isOriginalPattern()) { this._animationCount += FrequencyWalk; } }; Game_CharacterBase.prototype.updatePattern = function() { if (!this.hasStepAnime() && this._stopCount > 0) { this.resetPattern(); } else { this._pattern = (this._pattern + 1) % this.maxPattern(); } }; Game_CharacterBase.prototype.maxPattern = function() { return carryFrameNumber; }; Game_CharacterBase.prototype.pattern = function() { return this._pattern < carryFrameNumber ? this._pattern : 1; }; Game_CharacterBase.prototype.setPattern = function(pattern) { this._pattern = pattern; }; Game_CharacterBase.prototype.isOriginalPattern = function() { return this.pattern() === 0; }; Game_CharacterBase.prototype.resetPattern = function() { this.setPattern(ResetPattern); }; // D I A G O N A L M O V E M E N T //MBS_Dir8.js Game_Player.prototype.getInputDirection = function() { return Input.dir8; }; Game_Player.prototype.getHorzDirection = function (direction) { if (direction == 9 || direction == 3) { return 6; } else { return 4; }; }; Game_Player.prototype.getVertDirection = function (direction) { if (direction == 1 || direction == 3) { return 2; } else { return 8; }; }; Game_Player.prototype.executeMove = function(direction) { if (direction % 2 == 0) { this.moveStraight(direction); } else { this.moveDiagonally(this.getHorzDirection(direction), this.getVertDirection(direction)); }; }; Scene_Map.prototype.updateDestination = function() { if (this.isMapTouchOk()) { this.processMapTouch(); } else { $gameTemp.clearDestination(); this._touchCount = 0; } }; Scene_Map.prototype.isMapTouchOk = function() { return this.isActive() && $gamePlayer.canMove(); }; Scene_Map.prototype.processMapTouch = function() { if (TouchInput.isTriggered() || this._touchCount > 0) { if (TouchInput.isPressed()) { if (this._touchCount === 0 || this._touchCount >= 15) { var x = $gameMap.canvasToMapX(TouchInput.x); var y = $gameMap.canvasToMapY(TouchInput.y); $gameTemp.setDestination(x, y); } this._touchCount++; } else { this._touchCount = 0; } } }; })();I am just messing with everything right now trying to get diagonal sprites working on touch. I want to be able to control all the pattern tweaks in the parameters with variables in real time during game play.
Maybe it's just me (just woke up), but I couldn't find $gameVariables in your entire script.
But I would suggest one thing.
You have a variable called "Down" for your parameter.
Don't use
$gameVariables.setValue(5, Number(parameters['Down']);if you already have the value as a number in a variable.
I couldn't get it working so I didn't save it. How do I make that parameter = $gameVariable?
$gameVariables.setValue(id, value);is the way to set a game variable. There is no other way.
You said, $gameVariable.set didn't work for you, so we need to see the code inside your script that didn't work to help you.
Without that information I'd like you to try this code.
You said, $gameVariable.set didn't work for you, so we need to see the code inside your script that didn't work to help you.
Without that information I'd like you to try this code.
Code:
$gameVariables.setValue(5, Down);
Nah, that didn't work. I thought there might have been a different way to do it for parameters. I think I will have better luck looking through random plugins though.
Please tell us exactly, in pseudo code if needed, what you are trying to achieve. I'm pretty sure that you are thinking too complicated and there's an easy way to do it.
I mean, I want to make a number in my plugin controlled by a $gameVariable that can be changed with events and saved to the game. I would like to know how to make a $gameVariable into a plugin parameter also.
You should just read the variable everytime you need it, then. Do so at the start of a given function, so it gets cached for it, like this:
var myFunction = function() { var value = $gameVariables.value(...); // do something with value.};
I don't really know what "make into" means, but if you want to set a variable to a given value, do so like this:
Code:
var parameters = PluginManager.parameters(...);var myNumber = parseInt(parameters[...]) || 0;$gameVariables.setValue(..., myNumber);
All of that works but when I change the variable through an event during the game it does not change the parameter.
Why would you even use the parameter, after storing it in a variable? You should use the variable directly, so you automatically retrieve the current value on every update.
I wanted to try something. I think I can get it done. Appreciate the assistance.
Original thread: https://forums.rpgmakerweb.com/threads/gamevariables-in-plugins.52723/