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

hi i have question.


Example if i call my function


1:ID 2😮perator


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.
Thank you for your suggestion.


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
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.
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
	if (Operator=='add'){
		//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; }
	}
}
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.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-correctly-use-eval-function-for-passing-operators-to-a-function.72192/