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
56
Reaction score
27
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,724
Reaction score
8,508
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
56
Reaction score
27
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
56
Reaction score
27
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,724
Reaction score
8,508
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
56
Reaction score
27
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
8,947
Reaction score
5,856
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 Profile Posts

Working on a characteeer! (Why is it so hard to color hair?) ;-;
Silly-Sketches.png
Just finished eventing a couple simple puzzles in RMMV, for my very first game "Sample Quest." Just messing around, throwing things together, and calling it a story XD On a non-RM note, got my hair cut today!
I feel the need to inform everyone on the forums NOT to go with Web Hosting Canada for your hosting needs. It is by far the worst customer service and just overall experience I have ever had with hosting and I am Canadian...
BCj
Got another eye surgery monday. Hope I'll recover quick and all's well. A bit tired of surgeries. On a happy note, working on the next chapter of Escalia so hope I can continue soon :)
Celebrating the arrival of autumn in the Northern Hemisphere :)

Forum statistics

Threads
115,368
Messages
1,089,316
Members
150,057
Latest member
kotfwing
Top