Which is the better practice for long scripts?

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
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.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,271
Reaction score
2,329
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:

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
39
First Language
German
Primarily Uses
RMMV
Hope that helps
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:
 

Kes

Global Moderators
Global Mod
Joined
Aug 3, 2012
Messages
21,388
Reaction score
10,825
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.

 

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
39
First Language
German
Primarily Uses
RMMV
[move]Learning Javascript[/move]
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.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,604
Reaction score
1,397
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.
 

Nilom

Veteran
Veteran
Joined
Sep 9, 2013
Messages
178
Reaction score
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:
 

MushroomCake28

KAMO Studio
Moderator
Joined
Nov 18, 2015
Messages
1,886
Reaction score
3,360
First Language
English
Primarily Uses
RMMV
Like everyone said, option B is better. Eval is not very efficient.
 

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

Latest Threads

Latest Posts

Latest Profile Posts

TFW: You get a text from someone and a long conversation--but you have no idea who this person is and don't wanna be that guy that asks "Who is this by the way?" I have no clue who this person is... but they seem to know me quite well. :rswt
You know, after all these years of using RM I've never played To The Moon. With being busy with life and then after my mom died I was too emotionally fragile to play something that heavy. Has anyone played the Switch version? Is there any big differences between that and the RM version?
Today I finished my Lets Play. Gonna have the videos scheduled for daily uploads now. Woo I finished something ^^
*tries to study Emergence of Bangladesh: Ancient Bengal* Matsyanyayam- When the law of punishment is kept in abeyance... okay what? *searches dictionary* oh! Ok. In abeyance, it gives rise to such disorders as is implied in the proverb of fishes i.e the larger fish swallows a small one- wait fishes eat each other!? Is that like... fishy cannibalism!?...Oh! It's 8:30! Time for dinner! *throws book on floor*
Babbaditch Finster and the Seven-Sided Cube has got to be one of my favorite classic games that didn't exist when I was kid, or ever

Forum statistics

Threads
93,395
Messages
911,975
Members
122,902
Latest member
Tiregs
Top