Which is the better practice for long scripts?

Discussion in 'Learning Javascript' started by Nilom, Jul 6, 2019.

  1. Nilom

    Nilom Veteran Veteran

    Messages:
    178
    Likes Received:
    39
    First Language:
    German
    Primarily Uses:
    RMMV
    So, I use a lot of scripts in my game, that have more than the very limited 12 script lines.

    Now I wanted to know which of these two options is the better practice?

    Option A:
    Use a javascript shortener site, shorten the code to a single line and paste it into the script event command. And of course have a readable version in notepad++ or another program.

    Option B:
    Use a function inside my own plugin and call that function with a script event command.


    I have some questions about option B.
    1) Does it hit the performance more if the engine needs to find the function inside the plugins instead of just running a one-liner-script?
    2) Will it be bad if I have about 1000 lines of different functions and constants in my plugin? Or is the engine fast enough to breeze through the functions and effectively skip all functions that are not currently called without a noticable performance loss?
    3) Are functions able to utilize every commands that are also available in script commands or do some of them need parameters passed? Example: event.tileId or something like that.
     
    #1
  2. ??????

    ?????? Diabolical Codemaster Veteran

    Messages:
    6,264
    Likes Received:
    2,312
    Location:
    Your OS
    First Language:
    Binary
    Primarily Uses:
    RMMV
    Option B.

    1 - when you call any script code from any game event it uses 'eval' to evaluate the code and then call it. Either way that eval would take place, but making it shorter could only possibly boost efficiency.

    2 - there would be no performance loss as long as the code is written efficiently. You could have an 18,000 line long script that has 10000 constants/variables and a few thousands functions or whatever, it makes no difference.

    3 - script commands used in the events script command option are evaluated within the scope of an instance of the Game_Interpreter Class. You are able to access any game interpreter function via the 'this' notation. these would not be available unless you passed parameters to your new functions to obtain that information. Things like $gameParty.members() and other global object functions would still be available, cause they are global.

    Hope that helps

    Edit: you should wrap your plugin code in an anonymous function to ensure that your vaariables and constants are only defined within your code and not made global. For example:

    Code:
    // global scope
    const MyPluginClass = {};
    
    (function(){
      // not global scope
      const ATHING = "Wooo!"
      let anumberforstuff = 0;
    
      // explicitly create function on MyPluginClass object
      MyPluginClass.afunk = function(param){
        // do stuff...
      };
    
    })();
    
    Further Edit: note that this does make one global object named 'MyPluginClass', but any constants and variables defined within the wrapper function are not made global.
     
    Last edited: Jul 7, 2019
    #2
    Nilom likes this.
  3. Nilom

    Nilom Veteran Veteran

    Messages:
    178
    Likes Received:
    39
    First Language:
    German
    Primarily Uses:
    RMMV
    Actually that helps a lot, thank you!

    I'm not sure if I understood 3), though. Does it mean that functions called within an event are treated like the function is run inside the event initially? So all context will be based on the event which called the function? Would it be enough to just pass "this" as a function parameter to have all the events parameters like x and y position etc.?

    Is it about reducing occupied memory space to try keeping the own code non global? I would like to have my constants global though, because I like to use $hp and $atk instead of 0 and 2. I can not remember all that numbers, including the numbers of equip slots and movement directions. :LZSjoy:
     
    #3
  4. Kes

    Kes Global Moderators Global Mod

    Messages:
    20,806
    Likes Received:
    10,587
    First Language:
    English
    Primarily Uses:
    RMVXA

    I've moved this thread to Learning Javascript. Please be sure to post your threads in the correct forum next time. Thank you.

     
    #4
  5. Nilom

    Nilom Veteran Veteran

    Messages:
    178
    Likes Received:
    39
    First Language:
    German
    Primarily Uses:
    RMMV
    I think it is not nececcarily a javascript question as my main questions are about the RPG Maker MV engine, it's limitations and how effectively it handles the code. But thanks for moving anyhow.
     
    #5
  6. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,581
    Likes Received:
    1,361
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    Use are a or b does not really matter, rmmv use `eval("script")` thats have very poor performance.
    Sure B are just little more fast, because js have less code to parse and eval.
    If you use only scripts commands in loop you will destroy cpu of your user :troll:.

    You should avoid use command script to eval scripts.
    Is better to create your plugin with listeners and events.

    all dev will tell you.
    Don’t use ‘eval’
    Although the “eval” function is a good method to run arbitrary code, each string that is passed to the eval function has to be parsed and executed on-the-fly. This cost has to be paid every time the execution reaches an eval function call.
     
    #6
    Nilom likes this.
  7. Nilom

    Nilom Veteran Veteran

    Messages:
    178
    Likes Received:
    39
    First Language:
    German
    Primarily Uses:
    RMMV
    Ok thank you. It is good to know. Unfortunately my javascript skills are not good enough yet to create my own plugins with listeners. I'll stick to calling functions for now. :LZScheeze:
     
    #7
  8. MushroomCake28

    MushroomCake28 KAMO Studio Veteran

    Messages:
    1,714
    Likes Received:
    3,065
    Location:
    Montreal, Canada
    First Language:
    English
    Primarily Uses:
    RMMV
    Like everyone said, option B is better. Eval is not very efficient.
     
    #8
    Nilom likes this.

Share This Page