RMMZ [Solved] How to call a JS-file function from an event?

Status
Not open for further replies.

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
57
Reaction score
28
First Language
English
Primarily Uses
RMMZ
How can I call a functions stored within a JS file from an event? Example of the JS file is at the bottom of this post.

I do successfully use ExecScript by Jeremy Kahn, but this script runs entire file and does not allow picking a function from within the file. Hence, I am looking for a solution that allows to call a function from within the file.

Here is an example of what I would like to call -- the function hello_world:

JavaScript:
//=======================
// my_tests.js                                                          
//=======================

function hello_world(){
    console.log('Hello World!');
};

Thank you.
 
Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
33,869
Reaction score
8,583
First Language
German
Primarily Uses
RMMV
that is what the script command of the events is for - IF the function has been declared that way.

if the function has been declared private, then nothing short of rewriting the js file will get you access, but if the programmer intended for that function to be used simply call it in an event command: script
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
57
Reaction score
28
First Language
English
Primarily Uses
RMMZ
Andar, if you do not know how to specifically answer my question, please don't send me on a false lead.
There is a certain context - I have a function (listed above, clearly I can rewrite it as long as I know how to). If you know how this function has to be structured, please let me know. Otherwise I think you don't know how to either.

Now, I am aware of the plugin architecture, there is a whole manual on it that I studied back and forth and am very familiar with. But I need to find out, if there is an even simplier way, out of the two that I know of right now - if I can call a function within a JS file directly.
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
57
Reaction score
28
First Language
English
Primarily Uses
RMMZ
Solved.

Apparently, there is no 3rd way of calling a function from a file. Two options are: i) ExecScript (this executes entire JS file as a function) or, ii) the function you want to call has to be inside a file that is registered as a plugin.

For the second way, the JS file's function has to be written as:

JavaScript:
//=======================
// my_tests.js                                                       
//=======================
(function hello_world(){
    console.log('Hello World!'); }; 
})();

function hello_world2(){
    console.log('Hello World - 2'); };
})();

function hello_world3(){
    console.log('Hello World - 3'); };
})();

Then in event, under "script", you call either one of these functions, or all 3 at once:

JavaScript:
hello_world3()

BUT ! The file containing these functions has to be registered as plugin and to be ON for these functions to work. Else it will result in error.
 
Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
33,869
Reaction score
8,583
First Language
German
Primarily Uses
RMMV
Andar, if you do not know how to specifically answer my question, please don't send me on a false lead.
that was no false lead - that was to tell you that you have to call the function by a script command, and that this only works for functions inside plugins (which are nothing but javascript files).
there is no other way to access a function
 

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
57
Reaction score
28
First Language
English
Primarily Uses
RMMZ
Andar, aight, aight, peace and love brother :kaoluv:
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
9,000
Reaction score
5,888
First Language
Dutch
Primarily Uses
RMXP

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.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

new mechanic in battle where you can mentally erase enemies when you deplete all their MP instead HP. this gives you a special item of their "memory". it's pretty to do in battle so far.
Apparently MZ lets you preview move routes in the move route editor without having to start up test game. That could save a lot of time during development compared to MV, especially for a long game that uses a lot of event movement for cutscenes.
A repost of an old meme I had made, now with music

I have a really hard time keeping all these goodies secret til December... but I swear, it is worth the wait!

Forum statistics

Threads
115,859
Messages
1,093,529
Members
151,081
Latest member
MOLLARTA
Top