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,339
Reaction score
2,502
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

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,009
Reaction score
11,347
First Language
English
Primarily Uses
RMVXA
[move]Learning Javascript[/move]
 

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,616
Reaction score
1,422
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
Global Mod
Joined
Nov 18, 2015
Messages
3,314
Reaction score
4,419
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 Profile Posts

Why you do this? Now I have to buy the new RPG Maker xD
I've always dreaded planning for my games so I just didn't plan at all. That... didn't work out after all. :kaosigh:
Turns out it can be kind of fun if you keep the scope small and don't take it way too seriously.
well... for the first time in months... I feel like my game is headed towards something again. I finally have it where I won't be drawing battle assets for the rest of the year...
What I would give to actually get some feedback on my game...

Forum statistics

Threads
99,568
Messages
966,845
Members
131,215
Latest member
SDragon64
Top