Calling a variable from another js file

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
woddy
Junior Member
Posts: 13
Joined: Mon Mar 05, 2018 10:44 pm

Calling a variable from another js file

Post by woddy » Fri Oct 26, 2018 12:52 pm

Greetings again,

During scripting i noticed that my 1.js script is quite long and since its hard to follow, i want to do some calculations that i currently do on 1.js in 2.js
I used export/import from ' ' statement but couldnt manage to send the variable 'first' under prototype.beginEvent in 1.js to 2.js
I wonder if there is a way to send a non global variable to another file do some computations and call it back to the original file.
I hope i made myself clear. Thanks a lot

Code: Select all

 
 // 1.js
1.js.prototype.beginEvent = function() {
var first = 10;
import {second} from './2.js'
}

Code: Select all

 
 //2.js
var second = 5 + first;
export { second };
}

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: Calling a variable from another js file

Post by andrew » Fri Oct 26, 2018 1:04 pm

To include a script, use:

Code: Select all

include("scriptfile.js");
usually at the beginning of your script, see examples at:

https://github.com/qcad/qcad/tree/maste ... c/Examples

To pass variables around, you'd typically use functions:

Code: Select all

function myFunction(myVariable) {
   // do something with myVariable
   ...
   // return some result:
   return 7;
};
Again, please refer to example scripts.

If you are creating an interactive script, please start with this tutorial:
https://qcad.org/en/tutorial-interactive-script-actions

Post Reply

Return to “QCAD 'How Do I' Questions”