MV Polishing my rudimentary code to take up less space, basic js question SOLVED

Hello, I am trying to reduce the amount of scripting I have with some conditional statements. Here is my base code:

JavaScript:
       <Custom Passive Condition>
  if (
user.equips()[0] && user.equips()[0].wtypeId === 1)
{
    condition = true;
 } else if (
user.equips()[0] && user.equips()[0].wtypeId === 2)
{
    condition = true;
     } else if (
user.equips()[0] && user.equips()[0].wtypeId === 3)
{
    condition = true;
     } else if (
user.equips()[0] && user.equips()[0].wtypeId === 4)
{
    condition = true;
         } else if (
user.equips()[0] && user.equips()[0].wtypeId === 5)
{
    condition = true;
         } else if (
user.equips()[0] && user.equips()[0].wtypeId === 6)
{
    condition = true;
          } else if (
user.equips()[0] && user.equips()[0].wtypeId === 7)
{
    condition = true;
         } else if (
user.equips()[0] && user.equips()[0].wtypeId === 8)
{
    condition = true;
         } else if (
user.equips()[0] && user.equips()[0].wtypeId === 9)
{
    condition = true;
 } else {
 
 condition = false;
  }
  </Custom Passive Condition>


I know I can change it to the following that would be better:


JavaScript:
       <Custom Passive Condition>
  if (
user.equips()[0] && user.equips()[0].wtypeId === 1 || user.equips()[0] && user.equips()[0].wtypeId === 2 || user.equips()[0] && user.equips()[0].wtypeId === 3 || user.equips()[0] && user.equips()[0].wtypeId === 4 || user.equips()[0] && user.equips()[0].wtypeId === 5 || user.equips()[0] && user.equips()[0].wtypeId === 6 || user.equips()[0] && user.equips()[0].wtypeId === 7 || user.equips()[0] && user.equips()[0].wtypeId === 8 || user.equips()[0] && user.equips()[0].wtypeId === 9)
{
    condition = true;
 } else {
 condition = false;
  }
  </Custom Passive Condition>


However I am pretty sure I can have this instead first check if user.equips()[0] & then check if wtypeId = 1:9 to cut down on this even further.

It certainly wouldn't be something as simple as the following, would it?

JavaScript:
       <Custom Passive Condition>
  if (
user.equips()[0] && user.equips()[0].wtypeId === 1:9)
{
    condition = true;
 } else {
 condition = false;
  }
  </Custom Passive Condition>

What would the best way to go about this? And am I correct in assuming that more refined code that takes up less space would be less exhaustive on the player's device?

Thank you

Actually just found out that it would be as simple as using >= && <=
Meaning the optimized code would be:

JavaScript:
<Custom Passive Condition>
  if (
user.equips()[0] && user.equips()[0].wtypeId >= 1 && user.equips()[0] && user.equips()[0].wtypeId <= 9)
{
    condition = true;
 } else {
 condition = false;
  }
</Custom Passive Condition>

So I suppose this is solved, unless there is an even better way to go about this? I am also still curious about how important minimizing the size of data structures is. TY
You could try this:
JavaScript:
<Custom Passive Condition>
const wtypeIds = [1, 2, 3, 4, 5, 6, 7, 8, 9];
condition = user.equips()[0] && wtypeIds.includes(user.equips()[0].wtypeId);
</Custom Passive Condition>

And am I correct in assuming that more refined code that takes up less space would be less exhaustive on the player's device?
Depends on the scenario, but in this case i can't see making the code more compact bringing much performance benefits.
Only benefit i see is readability.

Things change when we're talking about loops or things that run every frame(i'm talking about update methods).
More optimized code will bring some performance benefits in those cases.
It's also worth considering if you really need to use a loop or update your code every frame.
It's not always worth it, but again, it depends on the scenario at hand.
So I suppose this is solved, unless there is an even better way to go about this?
A small tweak would be to assign the value to condition directly - the expression inside that if statement is already either true or false. There's also no need to ensure user.equips()[0] exists twice, once is enough.
condition = user.equips()[0] && user.equips()[0].wtypeId >= 1 && user.equips()[0].wtypeId <= 9;
I am also still curious about how important minimizing the size of data structures is.
With a script like this, the performance impact is rather negligible - the main benefit is that when you come back to it one year later you'll be able to tell what it's doing a lot easier.
However I am pretty sure I can have this instead first check if user.equips()[0] & then check if wtypeId = 1:9 to cut down on this even further.

What would the best way to go about this?
theres no need to repeat the check to see if slot 0 is null. when you use AND, both sides of the operator has to be true have it return true. if your first operand was already user.equips()[0], then checking it again at any point past it is redundant. additionally, JS boolean operaors short circuit. if that slot is null, the rest of the code following the && doesnt ever get evaluated anyways.

you dont need to repeat the check for wtype IDs either. you can put the values youre looking for into an array and use includes to see if the slot's id matches an element of it

Code:
<Custom Passive Condition>
condition = user.equips()[0] && [1,2,3,4,5,6,7,8,9].includes(user.equips()[0].wtypeId);
</Custom Passive Condition>
Hey thanks all of you.

Will do some reading into the const function as I see this often in plugins and don't really know much about it. Maybe spend a bit more time learning the shorthand versions as well.

Appreciate the input about loops and updates causing lag as well. That makes a lot of sense and will be something I keep in mind as I go forward. Lag = dealbreaker and I want to avoid it all costs.

Thanks again all.
Hey thanks all of you.

Will do some reading into the const function as I see this often in plugins and don't really know much about it. Maybe spend a bit more time learning the shorthand versions as well.

Appreciate the input about loops and updates causing lag as well. That makes a lot of sense and will be something I keep in mind as I go forward. Lag = dealbreaker and I want to avoid it all costs.

Thanks again all.
const isn't a function, it's a way of declaring a variable.

Javascript used to only have one declarative statement: var. But it had several issues and people tend to avoid it now in favour of its replacements, let and const.

Use let if the value might change within the scope where it's declared. Use const if it will retain its initial value and remain unchanged.
For @TobyYasha 's code, could the wTypeIds be presented in the form of a range, or would they have to be entered in individually?

JavaScript:
<Custom Passive Condition>
const wtypeIds = [1, 2, 3, 4, 5, 6, 7, 8, 9];
condition = user.equips()[0] && wtypeIds.includes(user.equips()[0].wtypeId);
</Custom Passive Condition>
For @TobyYasha 's code, could the wTypeIds be presented in the form of a range, or would they have to be entered in individually?

JavaScript:
<Custom Passive Condition>
const wtypeIds = [1, 2, 3, 4, 5, 6, 7, 8, 9];
condition = user.equips()[0] && wtypeIds.includes(user.equips()[0].wtypeId);
</Custom Passive Condition>
You could do

JavaScript:
const wtypeIds = Array.from({length:9}, (value, index) => 1 + index);
Awesome, thank you @Trihan. Arrays are next on my list of things I need to read about. Seems like they can get very complex but are likewise very useful. I think the lack of using them is why my display sprite in window plugin edit ends up repeating, showing all the sprites in party multiple times over, which is causing lag and some other graphical issues in my menu. Will make another thread when I get into fixing that bit. Without a doubt I wouldn't be able to complete my project without all the help I am getting here. My gratitude is yours.

One more thing now that I am thinking about it, if I were to adjust this to include 1:9 as well as 21:29, what would be the proper way to format it?
One more thing now that I am thinking about it, if I were to adjust this to include 1:9 as well as 21:29, what would be the proper way to format it?
There are many different ways of doing this. Here's what I'd do for a more general-purpose solution:
JavaScript:
const ids = [[1,9], [21,29]]; //EDIT: the initial version missed a 'const' at the beginning of this line
condition = user.equips()[0] && ids.some(x=> user.equips()[0].wtypeId >= x[0] && user.equips()[0].wtypeId <= x[1]) //EDIT2: Added wtypeId, thanks Turan
That is awesome. some is something I haven't worked with either, so I will add that to the list along with array to read about next. I am guessing that it would mean 'at least one'? I think I get let and const since they are just alternative ways of establishing variables. Either way, this is all really useful info and I will be bookmarking this thread to reference later as I add new functions or rewrite old ones.
I am guessing that it would mean 'at least one'?
You are guessing correctly. It's definitely worth learning the basics of arrays first, but some, all, forEach, map and filter are all things that can make using them a bit faster.
You are guessing correctly. It's definitely worth learning the basics of arrays first, but some, all, forEach, map and filter are all things that can make using them a bit faster.
Don't forget .reduce!
For something like this, where your IDs are adjacent values, it's much more efficient to use the simple math operators than to do anything with arrays.

So @Mac15001900's original suggestion in Post #3 is the best solution for your current setup.

If you wanted to add a second range, you could do what he did with the two arrays, but it would still be significantly more efficient to just do:
Code:
condition = user.equips()[0] && (user.equips()[0].wtypeId > 0 && user.equips()[0].wtypeId < 10 ||
                                 user.equips()[0].wtypeId > 20 && user.equips()[0].wtypeId < 30);

The array solution would only really be desirable if you have a list of IDs that are not mathematically related, they're all over the place.

Also, the code in Post #10 is missing the wtypeId property, it's trying to see whether the equipped Item is greater than or equal to the IDs 😉
Thanks @ATT_Turan. I ended up going with a solution pretty much the same as Mac15001900's suggestion, just in the more longhand form.

One thing I have been thinking, and I believe some of your posts have clued me onto this, is that the heavy use of passive states is not recommended. I currently have about 50 passive states per battle actor that have conditions as to whether they are enabled or not, and I use this to determine projectile weapon range in the battle system I am using. With these 50 states, I then run a conditional check that draws the attack distance based on which state is enabled. This ends up with a relatively small conditional check at the end of all this making it easier for me to edit at my discretion.

The alternative here would be having everything checked at the end of the function and to not use any (or many) passive states at all. This would be a larger code js wise and not as easy to sort through as the passive conditional state->conditional if function, however if I am not mistaken, this would be a lot less resource intensive as there wouldn't be 50+ passive states (constantly?) being checked per actor to see if the conditions match.

In this case, would it be better for a longer conditional check at the end of the sequence of events than the solution I am using with all these states? I hate to rewrite everything, but I just cannot tolerate lag if it can be avoided through better programming practices.

And am I correct that multiple passive states can considerably increase processor load?

Thank you.
That's definitely not how passives are intended to be used.

Every time each battler is refreshed (which is every frame, IIRC), it iterates through their list of passive states and calculates whether they meet their conditions. So piling 50 onto every battler is a substantial amount of extra work on the engine that probably can be done in a much more efficient way. And I haven't ever gotten to that point myself, but I've seen people start support threads because they use a ton of passives and it causes the game to lag.

I have no idea what your states represent or how they're used in your calculations, but it seems like it would probably be simple to do something like assign a numeric value to a variable stored on each battler that represents their row or coordinates or whatever it is you're checking. Then you can just compare that to the attacker's and the range of their weapon.

Something randomly without context would be:
Code:
if (target.row - user.row <= 6)
would return true so it's a valid target for your bow with range 6.
That's definitely not how passives are intended to be used.

...

Something randomly without context would be:
Code:
if (target.row - user.row <= 6)
would return true so it's a valid target for your bow with range 6.

I wish it were that simple, I am using a grid based tactical system and weapon range is based on the weapon equipped as well as if any buffs or debuffs are present. This in addition to 3 separate weapon slots means even checking the range between 3-9 max and 1-5 min + debuff/buff + weapon slot = over 50 possibilities easily.

Anyways, I will consider what you said and do agree it probably isn't the best approach. I think I can solve it using a more direct approach, because having every battler's passive states checked every frame is definitely a no go. I suppose in the meantime I can use this current setup as an interim way to go about it, and to use it to help visualize the correct way to write a complex conditional if that does the same thing without reliance on the editor.

I am using a similar solution for weapon skills and class skills, so I will likely need to change that down the line as well. This doesn't sound correct to me, but I was told that adding traits such as skills and skill types, should be done (or can only be done) through the editor. Surely you can perform the same functions with the correct js I would think. Thinking about it further, it makes no sense that you could not.

Will be reconsidering some of my design decisions.
I am using a grid based tactical system and weapon range is based on the weapon equipped as well as if any buffs or debuffs are present. This in addition to 3 separate weapon slots means even checking the range between 3-9 max and 1-5 min + debuff/buff + weapon slot = over 50 possibilities easily.
Again, not knowing how your mechanics actually work, that just sounds like math. I don't really understand what the "50 possibilities" stuff means, the two battlers are within a certain distance from each other or not. What are other possibilities?

Aside from RPG Maker not being the best choice for making an SRPG, there are a number of existing (and in various degrees of finished) plugins that should already do all of these calculations for you. Are you using one?
Yup, using LeTBS atm, but a lot of the features I am using were custom added as the base plugin leaves a lot to be desired. I just started working with js this last year or so, so I am not surprised really to find that solutions I found viable earlier on in development may not be the ideal approach after all.

Really happy for the help from the friendly posters here. Doubt I could have gotten this far or grasped a much better understanding of js without them.

Original thread: https://forums.rpgmakerweb.com/threads/polishing-my-rudimentary-code-to-take-up-less-space-basic-js-question-solved.167208/