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
49
Reaction score
21
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,695
Reaction score
8,496
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
49
Reaction score
21
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
49
Reaction score
21
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,695
Reaction score
8,496
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
49
Reaction score
21
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,926
Reaction score
5,849
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

Doing RPG Maker News for 21st September 2021


Malak is still waiting for Dark Deception Chapter 4... This was him yesterday after another sugar crash.
Again, he's fine. He's just bored at this point, considering he's exhausted his entire vacation bucket list.
Am I the only one who files custom resources under specific folders to remember who to credit? :kaoswt2:
1632263519730.png
I always smile to myself when I see someone do the "immediate halt and spin around" from forgetting something. I find it sort of endearing how we all do it without ever being taught to.

Forum statistics

Threads
115,260
Messages
1,088,430
Members
149,862
Latest member
raeellee
Top