MV How can I make those permanent? (Solved)

This is probably very simple, but maybe not... I wouldn't know. I don't know any javascript beyond a hand full script calls.

I do understand java formulas though. And those I use a lot.

Simple example:
JavaScript:
var cardId = $gameVariables.value(1);
var name = "Card" + cardId
var slot = $gameVariables.value(3);
var x = $gameVariables.value(14)[slot];
var y = $gameVariables.value(15)[slot];
$gameVariables.setValue(slot, cardId);
$gameScreen.showPicture(slot, name, 1, x, y, 100, 100, 255, 0);

In this example, I save the card ID in the variable with the same ID as the slot, which is stored in \v[3]. I also use this slot ID as index to find the right coordinates. Then it shows an image with the name "Card1.png" or "Card2.png" on the right coordinates.

However... Those var values never change. So the first 5 lines in this case (sometimes it is more like 10 or 12) are always what they are. X is always v[14].(v[3]) and name is always "Card" + v[1].

The commands I made with those var values are different with each action, but I use the same var values over and over.

However, in every single script call, I need to put all those var= this and var= that again, even though they are always the same.

So now I would want to make this part permanent:
JavaScript:
var cardId = $gameVariables.value(1);
var name = "Card" + cardId
var slot = $gameVariables.value(3);
var x = $gameVariables.value(14)[slot];
var y = $gameVariables.value(15)[slot];

So I can make all kinds of formula's, like this, without the need of the above:
JavaScript:
$gameVariables.setValue(slot, cardId);
$gameScreen.showPicture(slot, name, 1, x, y, 100, 100, 255, 0);

This is probably really easy to do, or maybe it isn't even possible. I wouldn't know. That is how much I know of javascript. 😛

I don't need anyone to make a full plugin for me. I am a quick learner, so if anyone can show me how to do this for 1 or 2 var values, I would be able to do the other 30 or so myself. It would make my life a lot easier and I'd be happy as a clown. :hhappy:

Spoiler: Note
Note that I am not trying to do this just for things like "var slot = $gameVariables.value(3)". Most formula's I use are a bit longer, but still are always the same. Like:
Code:
var stat = $dataEnemies[$gameVariables.value($gameVariables.value(3))].params

So in my formulas I can use "stat[2]" to find the ATK of enemy which ID is saved in the var with the ID which is saved in var 3... Do you still follow? This is making things so complicated for anyone with no idea what I am using this for. And it gets way more complicated that this.

So, to avoid confusion, I made up the easiest example I could think of.

After all, when I know how to make a plugin that permanently makes "slot" into "$gameVariables.value(3)", then I also know how to do the same thing with larger, more complicated formula's.
The code itself might be constant, but the values are not.
which means all those lines need to be executed anyway to get the current values, you can't make the lines themselves permanent.

what you can do is write a plugin that creates a function like initpic() with those lines and then call

Code:
initpic()
$gameVariables.setValue(slot, cardId);
$gameScreen.showPicture(slot, name, 1, x, y, 100, 100, 255, 0);

You might even add the showpicture to the plugin function, reduce that to a single line depending on what exactly you want there (you might have to give arguments for the functions dep0ending on the exact situation)
Andar's solution could work, but you'd need to make those variables global, e.g. window.cardId = bla instead of var cardId = bla.

I would consider writing a plugin to define a custom "getter" for each identifier (cardId, name, etc), i.e. a function to return the associated value whenever it is requested. E.g. something like this:
Spoiler: plugin code
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Description
 * @author Author
 * @url https://forums.rpgmakerweb.com/threads/166239/
 * @help Help
 */

// Author namespace.
window.JDN || (window.JDN = {});

// Define getters for frequently-needed values.
Object.defineProperties(window.JDN, {
  cardId: { get: function() { return $gameVariables.value(1); } },
  name:   { get: function() { return "Card" + this.cardId; } },
  slot:   { get: function() { return $gameVariables.value(3); } },
  x:      { get: function() { return $gameVariables.value(14)[this.slot]; } },
  y:      { get: function() { return $gameVariables.value(15)[this.slot]; } }
});
Then use ES6 destructuring assignment to declare the values locally in your events, e.g.
JavaScript:
const { cardId, name, slot, x, y } = JDN;
$gameVariables.setValue(slot, cardId);
$gameScreen.showPicture(slot, name, 1, x, y, 100, 100, 255, 0);
The code itself might be constant, but the values are not.
which means all those lines need to be executed anyway to get the current values, you can't make the lines themselves permanent.

what you can do is write a plugin that creates a function like initpic() with those lines and then call

Code:
initpic()
$gameVariables.setValue(slot, cardId);
$gameScreen.showPicture(slot, name, 1, x, y, 100, 100, 255, 0);

You might even add the showpicture to the plugin function, reduce that to a single line depending on what exactly you want there (you might have to give arguments for the functions dep0ending on the exact situation)
Indeed the values are not constant. That is why I use variables.
I will look into initpic() and what that even is. 😛 Like I said, I know no javascript. 😛

The last line, that is not supposed to go into the plugin. Those commands are not always the same. Like when I want to flip a card around, I will move the card (using slot, name X and Y) to the same position, but width 0, then show the back of the card in the same spot with 0 width, and then move it to 100%. So, the scale value and the name, both change a lot. Suddenly the name is no longer "card"+id but "cardBack.png"

So, that was the whole idea, to have the values all standard, so I can easily make the commands. Those commands are not often the same.

I'll look up some more info about initpic and how to use that in a plugin. 🙂 Thanks.

Andar's solution could work, but you'd need to make those variables global, e.g. window.cardId = bla instead of var cardId = bla.

I would consider writing a plugin to define a custom "getter" for each identifier (cardId, name, etc), i.e. a function to return the associated value whenever it is requested. E.g. something like this:
Spoiler: plugin code
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Description
 * @author Author
 * @url https://forums.rpgmakerweb.com/threads/166239/
 * @help Help
 */

// Author namespace.
window.JDN || (window.JDN = {});

// Define getters for frequently-needed values.
Object.defineProperties(window.JDN, {
  cardId: { get: function() { return $gameVariables.value(1); } },
  name:   { get: function() { return "Card" + this.cardId; } },
  slot:   { get: function() { return $gameVariables.value(3); } },
  x:      { get: function() { return $gameVariables.value(14)[this.slot]; } },
  y:      { get: function() { return $gameVariables.value(15)[this.slot]; } }
});
Then use ES6 destructuring assignment to declare the values locally in your events, e.g.
JavaScript:
const { cardId, name, slot, x, y } = JDN;
$gameVariables.setValue(slot, cardId);
$gameScreen.showPicture(slot, name, 1, x, y, 100, 100, 255, 0);
Yes!!! This is exactly what I hoped for! I'll test it in the morning, but since it comes from you, I am confident this will work. I'd put you in my credits, but you're already in there. 😉


Edit: Did a quick test, and it seems to work perfectly! I learned so much from this! Thanks again!
I'll look up some more info about initpic and how to use that in a plugin.
just to be clear: initpic does not exist - it is a fictional name for the currently fictional function that would have been created by the plugin.
because you need a unique function name to call a plugin function, and that name should be chosen to tell you what it does to help you remember which of the x-dozens of functions in the engine you need
just to be clear: initpic does not exist - it is a fictional name for the currently fictional function that would have been created by the plugin.
because you need a unique function name to call a plugin function, and that name should be chosen to tell you what it does to help you remember which of the x-dozens of functions in the engine you need
Oh... Yeah, I know so little about javascript, that was straight over my head.
But Caethyril's solution did solve the problem. With a very exact example, I was able to create this:

Spoiler: Plugin (wip)
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Description
 * @author Author
 * @url https://forums.rpgmakerweb.com/threads/166239/
 * @help const { imgName, cImg, tImg, cSlot, tSlot, cId, tId, arrX, arrY, cX, cY, tX, tY, cMHP, cHP, cMMP, cMP, cATK, cDEF, cMAT, cMDF, cAGI, cLUK, tMHP, tHP, tMMP, tMP, tATK, tDEF, tMAT, tMDF, tAGI, tLUK } = JDN;
 */

// Author namespace.
window.JDN || (window.JDN = {});

// Define getters for frequently-needed values.
Object.defineProperties(window.JDN, {
  imgName:  { get: function() { return '../enemies/Card' + $gameVariables.value(1); } },
  cImg:     { get: function() { return '../enemies/Card' + $gameVariables.value($gameVariables.value(3)); } },
  tImg:     { get: function() { return '../enemies/Card' + $gameVariables.value($gameVariables.value(4)); } },
  cSlot:    { get: function() { return $gameVariables.value(3); } },
  tSlot:    { get: function() { return $gameVariables.value(4); } },
  cId:      { get: function() { return $gameVariables.value($gameVariables.value(3)); } },
  tId:      { get: function() { return $gameVariables.value($gameVariables.value(4)); } },
  arrX:     { get: function() { return [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,936,1464,1704,120,1752,0,0,0,0,0,120,264,408,552,696,552,744,936,1128,1320,936,1464,1704,120,1752,0,0,0,0,0,120,264,408,552,696,552,744,936,1128,1320]; } },
  arrY:     { get: function() { return [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,864,912,912,624,624,0,0,0,0,0,912,912,912,912,912,624,624,624,624,624,192,144,144,432,432,0,0,0,0,0,144,144,144,144,144,432,432,432,432,432]; } },
  cX:       { get: function() { return this.arrX[this.cSlot]; } },
  cY:       { get: function() { return this.arrY[this.cSlot]; } },
  tX:       { get: function() { return this.arrX[this.tSlot]; } },
  tY:       { get: function() { return this.arrY[this.tSlot]; } },
  cMHP:     { get: function() { return $gameActors.actor(this.cId).mhp; } },
  cHP:      { get: function() { return $gameActors.actor(this.cId).hp; } },
  cMMP:     { get: function() { return $gameActors.actor(this.cId).mmp; } },
  cMP:      { get: function() { return $gameActors.actor(this.cId).mp; } },
  cATK:     { get: function() { return $gameActors.actor(this.cId).atk; } },
  cDEF:     { get: function() { return $gameActors.actor(this.cId).def; } },
  cMAT:     { get: function() { return $gameActors.actor(this.cId).mat; } },
  cMDF:     { get: function() { return $gameActors.actor(this.cId).mdf; } },
  cAGI:     { get: function() { return $gameActors.actor(this.cId).agi; } },
  cLUK:     { get: function() { return $gameActors.actor(this.cId).luk; } },
  tMHP:     { get: function() { return $gameActors.actor(this.tId).mhp; } },
  tHP:      { get: function() { return $gameActors.actor(this.tId).hp; } },
  tMMP:     { get: function() { return $gameActors.actor(this.tId).mmp; } },
  tMP:      { get: function() { return $gameActors.actor(this.tId).mp; } },
  tATK:     { get: function() { return $gameActors.actor(this.tId).atk; } },
  tDEF:     { get: function() { return $gameActors.actor(this.tId).def; } },
  tMAT:     { get: function() { return $gameActors.actor(this.tId).mat; } },
  tMDF:     { get: function() { return $gameActors.actor(this.tId).mdf; } },
  tAGI:     { get: function() { return $gameActors.actor(this.tId).agi; } },
  tLUK:     { get: function() { return $gameActors.actor(this.tId).luk; } }
});
As far as I can tell, everything works just as I want it to. I have a bit more to do, because I also need the stats for enemies, and their gold and experience, their drops etc. Those stats is what my whole card game is build upon.

Even though I don't use the battle system, I do make full use of my actors and enemies, in different ways. (Enemies are the stats per card Id. Actors are the stats of cards that are in play) So shortening "$gameActors.actor($gameVariables.value($gameVariables.value(3))).luk" to "cLUK" is a huge difference for me. 😛 For enemies it will be even nicer, because there I can't even use ".agi" and ".luk" at the end, but "parameter[6]" and "parameter[7]" 😛

So yeah, this plugin is a bit of a chore to set up once, but after that it is just going to speed up my process by... uhm... by a lot! 😛

(The plugin is probably pretty useless for anyone else, so I won't bother sharing it officially. But I like to show what I did with the input I got.)
Great! Just to be clear, if you only need a few specific values in an event, you don't need to declare the others, e.g.
JavaScript:
const { imgName, cImg, tImg } = JDN;
You can use the big long version every time if you want, though...it'll probably compile to the same instructions regardless. 👋
Great! Just to be clear, if you only need a few specific values in an event, you don't need to declare the others, e.g.
JavaScript:
const { imgName, cImg, tImg } = JDN;
You can use the big long version every time if you want, though...it'll probably compile to the same instructions regardless. 👋
Yeah, thanks. I did try that out. 😛 I also tested if I would need to add arrX and arrY when I use cX and cY for example, because cX and cY would read from those arrays, but it seems to work fine if I just include the ones I actually use.

The reason the whole command is in the help section (which I assume is the reason you mention this) is for me to easily copy/paste it over when I need it, and then I just delete the ones I don't use.

After a while it will probably be faster to type that line manually, but for now this is the easy way. 😛

However, the list is getting a little long, so I might put a few commonly used combinations in the help section or something. Or not... I will probably copy that line from script call to script call anyway, if I need the same or a similar combination.

You got a sharp eye, by the way. I would have expect most readers would focus on the colored text, instead of the help section. 😛
If anyway was wondering what I did with this code... This is what it looks right now. These are all bits of information that I call upon a lot, so having them go from "Math.ceil($gameActors.actor($gameVariables.value($gameVariables.value(4))).hp / $gameActors.actor($gameVariables.value($gameVariables.value(4))).mhp * 100)" to "tHP100" is quite an improvement.

Of course I picked the vary largest formula shortened by the plugin as example... That is how I role. 😛

Spoiler: Full code
JavaScript:
/*:
 * @target MV
 * @plugindesc This plugin is purely written to make my life a lot easier. It won't work for your project. Sorry.
 * @author JohnDoeNews
 * @url https://www.johndoenews.nl
 * @help Place this at the start if a script call: 
 * const { x , x , x } = JDN;
 * 
 * Replace x with getters from the plugin, which are used in the script call.
 * 
 * Available getters:
 * 
 * Arrays:   arrX , arrY
 * Hero:     hId , hBack , hFace
 * Boss:     bId , bBack , bFace
 * Enemy:    eId , eImg , eMHP , eMMP , eATK , eDEF , eMAT , eMDF , eAGI
 *           eLUK , eGLD , eEXP
 * Caster:   cSlot , cId , cImg , cX , cY , cMHP , cHP , cMMP , cMP
 *           cATK , cDEF , cMAT , cMDF , cAGI , cLUK , cHP100 , cMP100
 * Target:   tSlot , tId , tImg , tX , tY , tMHP , tHP , tMMP , tMP
 *           tATK , tDEF , tMAT , tMDF , tAGI , tLUK , tHP100 , tMP100
 * Formula:  cPostDMG , tPostDMG
 * Animaton: frames
 */
// Author namespace.
window.JDN || (window.JDN = {});
// Define getters for frequently-needed values.
Object.defineProperties(window.JDN, {
  arrX:     { get: function() { return [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,936,1464,1704,120,1752,0,0,0,0,0,120,264,408,552,696,552,744,936,1128,1320,936,1464,1704,120,1752,0,0,0,0,0,120,264,408,552,696,552,744,936,1128,1320]; } },
  arrY:     { get: function() { return [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,864,912,912,624,624,0,0,0,0,0,912,912,912,912,912,624,624,624,624,624,192,144,144,432,432,0,0,0,0,0,144,144,144,144,144,432,432,432,432,432]; } },
//Hero
  hId:      { get: function() { return $gameVariables.value(21); } },
  hBack:    { get: function() { return 'CardBack' + this.hId; } },
  hFace:    { get: function() { return '../faces/Face' + this.hId; } },
//Boss
  bId:      { get: function() { return $gameVariables.value(41); } },
  bBack:    { get: function() { return 'CardBack' + this.bId; } },
  bFace:    { get: function() { return '../faces/Face' + this.bId; } },
//Enemy
  eId:      { get: function() { return $gameVariables.value(1); } },
  eImg:     { get: function() { return '../enemies/Card' + this.eId; } },
  eMHP:     { get: function() { return $dataEnemies[this.eId].params[0]; } },
  eMMP:     { get: function() { return $dataEnemies[this.eId].params[1]; } },
  eATK:     { get: function() { return $dataEnemies[this.eId].params[2]; } },
  eDEF:     { get: function() { return $dataEnemies[this.eId].params[3]; } },
  eMAT:     { get: function() { return $dataEnemies[this.eId].params[4]; } },
  eMDF:     { get: function() { return $dataEnemies[this.eId].params[5]; } },
  eAGI:     { get: function() { return $dataEnemies[this.eId].params[6]; } },
  eLUK:     { get: function() { return $dataEnemies[this.eId].params[7]; } },
  eGLD:     { get: function() { return $dataEnemies[this.eId].gold; } },
  eEXP:     { get: function() { return $dataEnemies[this.eId].experience; } },
//Caster
  cSlot:    { get: function() { return $gameVariables.value(3); } },
  cId:      { get: function() { return $gameVariables.value(this.cSlot); } },
  cImg:     { get: function() { return '../enemies/Card' + this.cId; } },
  cX:       { get: function() { return this.arrX[this.cSlot]; } },
  cY:       { get: function() { return this.arrY[this.cSlot]; } },
  cMHP:     { get: function() { return $gameActors.actor(this.cSlot).mhp; } },
  cHP:      { get: function() { return $gameActors.actor(this.cSlot).hp; } },
  cMMP:     { get: function() { return $gameActors.actor(this.cSlot).mmp; } },
  cMP:      { get: function() { return $gameActors.actor(this.cSlot).mp; } },
  cATK:     { get: function() { return $gameActors.actor(this.cSlot).atk; } },
  cDEF:     { get: function() { return $gameActors.actor(this.cSlot).def; } },
  cMAT:     { get: function() { return $gameActors.actor(this.cSlot).mat; } },
  cMDF:     { get: function() { return $gameActors.actor(this.cSlot).mdf; } },
  cAGI:     { get: function() { return $gameActors.actor(this.cSlot).agi; } },
  cLUK:     { get: function() { return $gameActors.actor(this.cSlot).luk; } },
  cHP100:   { get: function() { return Math.ceil(this.cHP / this.cMHP * 100); } },
  cMP100:   { get: function() { return Math.ceil(this.cMP / this.cMMP * 100); } },
//Target
  tSlot:    { get: function() { return $gameVariables.value(4); } },
  tId:      { get: function() { return $gameVariables.value(this.tSlot); } },
  tImg:     { get: function() { return '../enemies/Card' + this.tId; } },
  tX:       { get: function() { return this.arrX[this.tSlot]; } },
  tY:       { get: function() { return this.arrY[this.tSlot]; } },
  tMHP:     { get: function() { return $gameActors.actor(this.tSlot).mhp; } },
  tHP:      { get: function() { return $gameActors.actor(this.tSlot).hp; } },
  tMMP:     { get: function() { return $gameActors.actor(this.tSlot).mmp; } },
  tMP:      { get: function() { return $gameActors.actor(this.tSlot).mp; } },
  tATK:     { get: function() { return $gameActors.actor(this.tSlot).atk; } },
  tDEF:     { get: function() { return $gameActors.actor(this.tSlot).def; } },
  tMAT:     { get: function() { return $gameActors.actor(this.tSlot).mat; } },
  tMDF:     { get: function() { return $gameActors.actor(this.tSlot).mdf; } },
  tAGI:     { get: function() { return $gameActors.actor(this.tSlot).agi; } },
  tLUK:     { get: function() { return $gameActors.actor(this.tSlot).luk; } },
  tHP100:   { get: function() { return Math.ceil(this.tHP / this.tMHP * 100); } },
  tMP100:   { get: function() { return Math.ceil(this.tMP / this.tMMP * 100); } },
//HP left after hit
  cPostDMG: { get: function() { return Math.ceil(this.cHP - this.tMP); } },
  tPostDMG: { get: function() { return Math.ceil(this.tHP - this.cMP); } },
//Show and Move picture
  frames:   { get: function() { return $gameVariables.value(10); } },
});

@Andar I tried to put the calls like show picture and move picture into the plugins as well as you suggested, but because the commands are not always the same, so it would be much easier to just put the actual command in manually.

For example, when a card is placed on the board, is it shown at 37.5% of its original size. When a card it shown in detail, those are at 100 of the size. When I flip a card, the card will move to have 0% width, then the other side of the card is showing in its place at 0% width, that than moves to either 37.5% or 100%, depending on if I was flipping a full sized card or a smaller card.

The coordination of an image is always bound to the ID in variable 3 or 4 (depending if it is the attacking card or the targeted card we are controlling). So I never have to alter to coordinates, since they are all already set up in the arrays. however, the size of the image, is not bound to any variable.

This means, I would end up with a large range of combinations, because I would need to do the same thing for both sides of the card, since card backs have a different name than frontal linked to the cards ID, while the back is linked to the boss/hero. I would create them both for full sizes and for the 37.5% versions.

So, I did try to make those commands directly into the plugin, and it works, but because I would end up with too many different combination, it wouldn't be useful in this particular case. It would be useful for other commands, that are always the same.

@caethyril Last time you replied to the example in the help section. I changed it to be more useful.

Anyhow, both of you thanks for the input. This actually saves me about half an hour a day! Now I just have to memorize all those getters. 😛
This thread is being closed due to being solved. If you need it reopened, report it to let us know why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-make-those-permanent-solved.166239/