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

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,437
First Language
French
Primarily Uses
RMMV
hi i have question.


Example if i call my function


1:ID 2:operator


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
 
Last edited by a moderator:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
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
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,437
First Language
French
Primarily Uses
RMMV
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; }
	}
}
 
Last edited by a moderator:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People1.png & SF_People1.png added!!!!

Adventurer:
"Why your body looks like a bird?"

Stranger:
"Because I'm harpy"

Adventurer:
"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!
Pootscooter wrote on Pharonix's profile.
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!

Forum statistics

Threads
105,506
Messages
1,014,261
Members
137,173
Latest member
zoetsoi
Top