# Variable Experience Gained based on the difference in level

Discussion in 'Javascript/Plugin Support' started by Nevarus, Jun 5, 2018.

1. ### NevarusStruggle-Bug Messages:
18
2
First Language:
English
Primarily Uses:
RMMV
Hey, I'm trying to get Experience that changes based on the difference in level between either the average of the party level or an actor and the enemy. I'm using Yanfly's EnemyLevels and Yanfly's EnemyBaseParamaters.

From what I've tried so far I've got this written out so far:
<Custom Base Param Formula>
if (this.level()-3 = '\$gameActors.actor(1).level')
{exp = Base * 1.5;}
else if (this.level-2() = '\$gameActors.actor(1).level')
{exp = Base * 1.25;}
else if (this.level-1() = '\$gameActors.actor(1).level')
{exp = Base * 1.125;}
else if (this.level() = '\$gameActors.actor(1).level')
{exp = Base;}
else if (this.level() +1 = '\$gameActors.actor(1).level')
{exp = Base * 0.5;}
else if (this.level() +2 = '\$gameActors.actor(1).level')
{exp = Base * 0.3;}
else if (this.level() +3 = '\$gameActors.actor(1).level')
{exp = Base * 0.15;}
else if (this.level() +4 = '\$gameActors.actor(1).level')
{exp = Base * 0.075;}
else if (this.level() +5 = '\$gameActors.actor(1).level')
{exp = Base * 0.015;}
else {exp = Base * 0.005;}
</Custom Base Param Formula>

Basically how i want this to work is like this
if the Enemy Level is greater than the Character's Level by 3 i get +50% XP
else
if the Enemy Level is greater than the Character's Level by 2 i get +25% XP
else
if the Enemy Level is greater than the Character's Level by 1 i get +12.5% XP
else
if the Enemy Level is equal to the Character's Level i get Base XP
else
if the Enemy Level is less than the Character's Level by 1 i get 50% less XP
else
if the Enemy Level is less than the Character's Level by 2 i get 70% less XP
else
if the Enemy Level is less than the Character's Level by 3 i get 85% less XP
else
if the Enemy Level is less than the Character's Level by 4 i get 92.5% less XP
else
if the Enemy Level is less than the Character's Level by 5 i get 98.5% less XP
else i get 99.5% less xp

I'm quite new to using rpg maker mv and also javascript, so any help with this would be appreciated.

2. ### Aloe GuvnerWalrus Messages:
1,562
978
Location:
USA
First Language:
English
Primarily Uses:
RMMV
Formatting suggestion:

If you use the CODE tags (click Insert... Code) it will make reading the code much easier.

________________________________

A few suggestions about the Javascript itself:

1.)
= means assignment
=== means equals
If you are comparing two things, you want to use the equals operator, if you're assigning a value to a variable you want to use the assignment operator.

2.)
In multiple places you have something like this:
this.level-2()

This means that you're trying to call "2" as a function, but 2 is just a number.

3.)
Wherever you have:
'\$gameActors.actor(1).level'

The apostrophe means that it's a string. You don't want a string, you want the value itself, so remove the apostrophes.

4.)
What about the cases where the enemy is more than 3 levels above the player or more than 5 levels below the player?
Consider using the 'greater than' and 'less than' comparisons for these cases.

________________________________

Additional suggestions specific to the Yanfly plugins.

1.)
You need to read the directions more carefully on the Yanfly site.
Make sure you're doing all 3 of those things.

2.)
Regarding the "Base" variable, I don't see where it is defined. Is it defined in the Yanfly plugin?

XIIIthHarbinger likes this.
3. ### NevarusStruggle-Bug Messages:
18
2
First Language:
English
Primarily Uses:
RMMV
Thanks for the Reply.

So to start with by "insert" do you mean this function?
Code:
```<Custom Base exp Formula>
Base=120
if (this.level-3 >= \$gameActors.actor(1).level)
{exp = Base * 1.5;}
else if (level-2 === \$gameActors.actor(1).level)
{exp = Base * 1.25;}
else if (level-1 === \$gameActors.actor(1).level)
{exp = Base * 1.125;}
else if (level === \$gameActors.actor(1).level)
{exp = Base;}
else if (level+1 === \$gameActors.actor(1).level)
{exp = Base * 0.5;}
else if (level+2 === \$gameActors.actor(1).level)
{exp = Base * 0.3;}
else if (level+3 === \$gameActors.actor(1).level)
{exp = Base * 0.15;}
else if (level+4 === \$gameActors.actor(1).level)
{exp = Base * 0.075;}
else if (level+5 === \$gameActors.actor(1).level)
{exp = Base * 0.025;}
else {exp = Base * 0.005;}
</Custom Base exp Formula>```
I honestly am really new to this whole thing, which caused some issues with this code. I had thought that = just means =, but thanks for clearing that up for me. Speaking of =s, Base = 120 (sorry about that). I added Base=120 at the top of the code, not sure if that's the right way to do that tho. I cleared up the apostrophes and the few ()s that were left in the code by accident. Added the greater than than function to the top "level checker", I guess is what i'd call them. The bottom on the other hand I didn't. What i want the bottom end to do is if it's level is lower than mine by more than 5 levels then the Exp is going to be the same even if its 100 levels lower.

I think that it was unnecessary to put this.level instead of level. Because in yanfly's instructions it has just a level variable, that is based off of the enemy's level. I also fixed an error with the numbers that shouldn't have been. for the Level+5 Exp it should be Base * 0.025 not Base * 0.015.

I think..... I fixed everything to get it to work as I intended, but let me know if theres something im mistaking about this thanks.

4. ### Aloe GuvnerWalrus Messages:
1,562
978
Location:
USA
First Language:
English
Primarily Uses:
RMMV
Looks a lot better, you still have "this.level" at the top, change it to "level" .

Also, in JavaScript you should explicitly declare new variables, like this:
Code:
`var Base = 120;`
Other than that, looks fine. I don't use those plugins specifically, so I can't guarantee it will work, but I don't see anything wrong from a general Javascript perspective.
When you test it, you can press F8 during the test play to open the developer console. Yanfly usually will put any error messages there.

5. ### NevarusStruggle-Bug Messages:
18
2
First Language:
English
Primarily Uses:
RMMV
Alright thanks, I tested this several times with different enemy levels and everything seems to check out.

I also changed
Code:
`base = 120`
to
Code:
`var Base = 120;`
p.s. whoops forgot that this.level at the top fixed.

Aloe Guvner likes this.