Performance questions with plugins and script calls

Discussion in 'Learning Javascript' started by Eliaquim, Sep 11, 2019.

Thread Status:
Not open for further replies.
  1. Eliaquim

    Eliaquim Raze: The Rakuen Zero's Guardian! Veteran

    Messages:
    798
    Likes Received:
    310
    Location:
    Brazil - Rio de Janeiro
    First Language:
    Portuguese - Br
    Primarily Uses:
    RMMV
    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?

    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! ^^
     
    #1
  2. shockra

    shockra Slightly Crazy Programmer Veteran

    Messages:
    406
    Likes Received:
    177
    Location:
    US
    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".
     
    #2
    Eliaquim likes this.
  3. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,261
    Likes Received:
    784
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    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:
     
    #3
  4. Aloe Guvner

    Aloe Guvner Walrus Veteran

    Messages:
    1,626
    Likes Received:
    1,047
    Location:
    USA
    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.

    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.
     
    #4
  5. Eliaquim

    Eliaquim Raze: The Rakuen Zero's Guardian! Veteran

    Messages:
    798
    Likes Received:
    310
    Location:
    Brazil - Rio de Janeiro
    First Language:
    Portuguese - Br
    Primarily Uses:
    RMMV
    @caethyril yes! It's for mobile xD
    Thanks, people!!
    Have a lot of work to do now!
    ^^
    :kaothx:
     
    #5
  6. bazrat

    bazrat Veteran Veteran

    Messages:
    237
    Likes Received:
    161
    Location:
    Washington State
    First Language:
    English
    Primarily Uses:
    RMMV
    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:
    i didnt realize plugin commands were so good...
     
    #6
    Eliaquim likes this.
  7. Kes

    Kes Global Moderators Global Mod

    Messages:
    20,945
    Likes Received:
    10,652
    First Language:
    English
    Primarily Uses:
    RMVXA

    This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

     
    #7
Thread Status:
Not open for further replies.

Share This Page