# How to correctly use EVAL function? For passing operators to a function

#### Jonforum

##### Veteran
hi i have question.

Example if i call my function

1:ID 2perator

StatsControl(322,'-=');  // Is do a decrease

if i do this

StatsControl(322,'+=');  // Is do a increase

i read Eval can make me do this easy,

I know well the long method, but I would like to find a shortcut to call this function and be, Decrement or increase, without duplicating my code once.

if i try this i get Uncaught ReferenceError: Invalid left-hand side in assignment

op='+=';
eval(\$dataActors[1].meta.MaxBag + op + 10);

Here my function. Existe a very fast way without duplicate the code for assigned dinamyque operator (+=) or (-=)

function StatsControl(PID,Operator) {
//MaxBag // add point MaxBag (10) // Store MaxBag Adder (/10) pour le reset
if (PID==322) { \$dataActors[1].meta.MaxBag+=10, StoreMaxBag+=10; } else
//HP // add point HP (5) // Store HP Adder (/5) pour le reset
if (PID==326) { \$gameActors.actor(1).mhp+=5, StoreHP+=5; } else
//MP // add point MP (5) // Store MP Adder (/5) pour le reset
if (PID==330) { \$gameActors.actor(1).mmp+=5, StoreMP+=5; } else
//ATT // add point ATT (5) // Store ATT Adder (1) pour le reset
if (PID==334) { \$gameActors.actor(1).atk+=1, StoreATT+=1; } else
//Def // add point Def (5) // Store StoreDef Adder (1) pour le reset
if (PID==338) { \$gameActors.actor(1).def+=1, StoreDef+=1; } else
//LUK // add point LUK (5) // Store StoreLUK Adder (1) pour le reset
if (PID==342) { \$gameActors.actor(1).luk+=1, StoreLUK+=1; } else
//STA // add point STA (5) // Store StoreSTA Adder (1) pour le reset
if (PID==346) { \$gameActors.actor(1).agi+=1, StoreSTA+=1; } else
//INT // add point INT (5) // Store StoreINT Adder (1) pour le reset
if (PID==350) { \$gameActors.actor(1).mat+=1, StoreINT+=1; }

}

If someone knows a trick, thanks

Last edited by a moderator:

#### Zalerinian

##### Jack of all Errors
It doesn't work because eval takes in a string that represents JavaScript code, but you really should avoid using eval if possible. You could easily use a switch statement to see what the operator is and then execute code based off that.

#### Jonforum

##### Veteran
It doesn't work because eval takes in a string that represents JavaScript code, but you really should avoid using eval if possible. You could easily use a switch statement to see what the operator is and then execute code based off that.

So I am obliged to do as his.
Would not there be a short way?

Code:
``````function StatsControl(PID,Operator) {
\$dataActors[1].meta.MaxBag = Number(\$dataActors[1].meta.MaxBag); // permanent Number convert the Meta of heroes 1
//MaxBag  // add  point MaxBag (10)  // Store MaxBag Adder (/10) pour le reset
if (PID==322) { \$dataActors[1].meta.MaxBag+=10, StoreMaxBag+=10; } else
//HP  // add  point HP (5)  // Store HP Adder (/5) pour le reset
if (PID==326) { \$gameActors.actor(1).mhp+=5, StoreHP+=5; } else
//MP  // add  point MP (5)  // Store MP Adder (/5) pour le reset
if (PID==330) { \$gameActors.actor(1).mmp+=5, StoreMP+=5; } else
//ATT  // add  point ATT (5)  // Store ATT Adder (1) pour le reset
if (PID==334) { \$gameActors.actor(1).atk+=1, StoreATT+=1; } else
//Def  // add  point Def (5)  // Store StoreDef Adder (1) pour le reset
if (PID==338) { \$gameActors.actor(1).def+=1, StoreDef+=1; } else
//LUK  // add  point LUK (5)  // Store StoreLUK Adder (1) pour le reset
if (PID==342) { \$gameActors.actor(1).luk+=1, StoreLUK+=1; } else
//STA  // add  point STA (5)  // Store StoreSTA Adder (1) pour le reset
if (PID==346) { \$gameActors.actor(1).agi+=1, StoreSTA+=1; } else
//INT  // add  point INT (5)  // Store StoreINT Adder (1) pour le reset
if (PID==350) { \$gameActors.actor(1).mat+=1, StoreINT+=1; }
}else if (Operator=='rem'){
//MaxBag  // add  point MaxBag (10)  // Store MaxBag Adder (/10) pour le reset
if (PID==322) { \$dataActors[1].meta.MaxBag-=10, StoreMaxBag-=10; } else
//HP  // add  point HP (5)  // Store HP Adder (/5) pour le reset
if (PID==326) { \$gameActors.actor(1).mhp-=5, StoreHP-=5; } else
//MP  // add  point MP (5)  // Store MP Adder (/5) pour le reset
if (PID==330) { \$gameActors.actor(1).mmp-=5, StoreMP-=5; } else
//ATT  // add  point ATT (5)  // Store ATT Adder (1) pour le reset
if (PID==334) { \$gameActors.actor(1).atk-=1, StoreATT-=1; } else
//Def  // add  point Def (5)  // Store StoreDef Adder (1) pour le reset
if (PID==338) { \$gameActors.actor(1).def-=1, StoreDef-=1; } else
//LUK  // add  point LUK (5)  // Store StoreLUK Adder (1) pour le reset
if (PID==342) { \$gameActors.actor(1).luk-=1, StoreLUK-=1; } else
//STA  // add  point STA (5)  // Store StoreSTA Adder (1) pour le reset
if (PID==346) { \$gameActors.actor(1).agi-=1, StoreSTA-=1; } else
//INT  // add  point INT (5)  // Store StoreINT Adder (1) pour le reset
if (PID==350) { \$gameActors.actor(1).mat-=1, StoreINT-=1; }
}
}``````

Last edited by a moderator:

#### Zalerinian

##### Jack of all Errors
Yes, that's one way of doing it. You could also look into how the Game_Interpreter deals with values. It has a function called operateValue that would probably interest you in this case.

### Latest Profile Posts

"Why your body looks like a bird?"

Stranger:
"Because I'm harpy"

"Clap along if you feel like a room without a roof"

*Both dancing to Pharrell Williams song*
Stream will be live shortly with a make-up session of the Interactive Text Adventure! Feel free to drop by!
Hey @Pharonix, just wondering what you're up to lately and if you've had an opportunity to check out the LTBS thread in the past few days...(a couple of us could really use your expertise). Hope you are well and Happy Thanksgiving!