Performance questions with plugins and script calls

Status
Not open for further replies.

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,695
Reaction score
1,113
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi there!

I have some doubts while making my plugins and the game. And it is about performance.
For example, I know that it depends on the code of the plugin for it to lag the game.
But, what is the most thing that can lag a game in the plugins?

1 - A lot of global variables, instead of local ones?
9. Prioritize access to local variables
JavaScript first searches to see if a variable exists locally, then searches progressively in higher levels of scope until global variables. Saving variables in a local scope allows JavaScript to access them much faster.

Local variables are found based on the most specific scope and can pass through multiple levels of scope, the look-ups can result in generic queries. When defining the function scope, within a local variable without a preceding variable declaration, it is important to precede each variable with let or const in order to define the current scope in order to prevent the look-up and to speed up the code.

10. Avoid using global variables
Because the scripting engine needs to look through the scope when referencing global variables from within function or another scope, the variable will be destroyed when the local scope is lost. If variables in the global scope can not persist through the lifetime of the script, the performance will be improved.

2 - Also, many aliases of the same function, in different plugins, (like one alias "Scene_ItemBase.prototype.createActorWindow = function()" and other aliases that too)?

3 - Another one is to make a script call like a plugin( I don't know if I use the right expression here...). I mean, for example, at the beginning of the game, in a script call:
Code:
If(Utils.isMobileDevice()) { $gameSwitches.setValue(1, true)};
Or

Another code, more complex, utilizing "var banana = something"

These things is saved in the save file of the game, right? And the "var" that I create on script calls, will be global? I mean, I can use that in another event? Or do I have to assign a value to it again?

Thanks a lot! ^^
 

shockra

Slightly Crazy Programmer
Veteran
Joined
Feb 16, 2016
Messages
444
Reaction score
208
First Language
English
Primarily Uses
RMMV
When using "var" to designate a variable, you're making it a local variable. Local variables are ideal if you're only using that variable for the function or script it's created in. If it is needed elsewhere, don't use "var".
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,087
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
I feel poor structure is often one of the largest impacts on performance, both in eventing and in scripting: unnecessarily frequent loops, redundant calculations, etc. E.g. if you need something calculated that won't change value on the given map, don't calculate it every frame; calculate it once, when you enter a new map, and store the result for later reference. Basically, make sure the game's not doing stuff it doesn't have to do.

RMMV's script calls are evals, which among other things are notably more CPU-intensive to run than "normal" JS. For various reasons, it's better to put chunky code in a plugin and invoke it using a function call (via Script command), rather than put all the code directly into the Script command. I'd suggest using plugin commands where possible! :kaothx:

var scopes a variable to the enclosing block. If you use it in a Script command, the enclosing block is the eval, a context that gets cleared/deleted when the script call is complete. In other words, your example "banana" var is not global, and is lost when the Script command ends. Global variables are characterised by being defined on the global object, which in NW.js is window; e.g. this defines a new global variable:
Code:
window.banana = 5;
Game saves only contain specific data, as defined in the makeSavefileInfo and makeSaveContents methods on the DataManager (rpg_managers.js).

I don't imagine the alias thing is worth bothering about unless you're trying to get every last bit of performance (including refactoring all your project code)...maybe for mobile deployments? :kaoswt:
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
First a disclaimer:
Performance is a fickle topic, and the only way to get a true answer is to profile the performance and identify the parts which are slow. The chrome devtools have tools for that, if you're interested more there are plenty of tutorials out there.

My opinion:
#1 and #2 won't affect performance in any noticeable way. The difference between accessing a global variable and local variable is so tiny that you won't notice, and function calls are very fast too. Maybe if there were 100+ alias on a single function you might see something. The V8 Javascript engine in chrome-based browsers is insanely optimized for these kinds of things.

#3 I'm not sure what you're saying. Do you mean the Script call from an Event Command? That uses "eval", which is probably the slowest possible thing you can do and it's still probably so fast that you don't notice it.

eval is slow because it has to call the JS compiler to compile the plain text into JS and then execute it. There's a certain plugin library that makes heavy heavy use of eval rather than building & caching function calls, and I've worked with people before who's games were heavily slowed down by that.

All that said, the biggest hits to performance are not in one-time things, it's when you execute something more often than is needed.

For example, let's say I want to check if Harold is standing on Region 4. Do I want to check every frame for Harold's location? Probably not! I don't need to check 60x per second.
Instead, execute when you need to and not more. In this example, I only need to check every time Harold moves to see if he moves to a Region 4.

Another code, more complex, utilizing "var banana = something"

These things is saved in the save file of the game, right?
In that case, no, "banana" is not saved to the save file. Only certain things are saved like $gameSystem and $gameActors, you have to check the code for the full list.
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,695
Reaction score
1,113
First Language
Portuguese - Br
Primarily Uses
RMMZ
@caethyril yes! It's for mobile xD
Thanks, people!!
Have a lot of work to do now!
^^
:kaothx:
 

bazrat

Pixel Game Maker MV
Veteran
Joined
Oct 15, 2018
Messages
241
Reaction score
176
First Language
English
Primarily Uses
Other
@caethyril yes! It's for mobile xD
Thanks, people!!
Have a lot of work to do now!
^^
:kaothx:
also yanflys script call optimizer is coming out sometime this month. he made it sound like the script command will run as a function now. im sure exactly what that means but i cant wait for it:
Optimize Script Calls

Script Calls, whether they're for setting variables, conditional branches, or actual script calls themselves, can run into performance issues when a lot of them are ran in succession. This plugin will optimize script call usage found inside common events and make them dedicated functions instead for better performance.
i didnt realize plugin commands were so good...
 
Status
Not open for further replies.

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

Latest Threads

Latest Posts

Latest Profile Posts

Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:

Forum statistics

Threads
105,854
Messages
1,016,998
Members
137,562
Latest member
tamedeathman
Top