Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: SvenK on April 29, 2013, 10:25:34 pm

Title: Include during runtime in Scripts
Post by: SvenK on April 29, 2013, 10:25:34 pm
Hello,

is it possible to include scripts during runtime? In JScript you have to do
Code: [Select]
!INC foo.bar
However, neither of the following works:
Code: [Select]
if (something)
{
  !INC foo.bar
}

Code: [Select]
eval("!INC foo.bar")


Title: Re: Include during runtime in Scripts
Post by: Paulus on April 30, 2013, 03:15:42 am
AFAIK EA uses '!INC' to include the actual script -prior to feeding it to a scripting enigne-, so language constructs like 'eval' wouldn't work.

But what are you trying to achieve by this?
Title: Re: Include during runtime in Scripts
Post by: SvenK on April 30, 2013, 04:25:33 pm
Quote
But what are you trying to achieve by this?

I want to include some files only once.

Assume a script A creates a variable myvariable= new Object().

Another script B adds something to this variable: myvariable.number=10

If at another place script A is again included, myvariable is overwritten...

While writing this, I think I may introduce some kind of include-guard to solve the problem.
Title: Re: Include during runtime in Scripts
Post by: Paulus on April 30, 2013, 06:07:13 pm
EA will not include the same script twice, so in your example if you include B in a script X, and subsequently A in the same script X, than this second include of A is ignored and the changes made in B will be visible to X.

If you have several scripts that share 'myvariable' as a common global variable only declare the global variable in A and either create/initialize the variable in the script you know will run first, or have each script that uses it check for existence first, like so:

Code: [Select]
!inc A

...
if (myvariable == undefined){
   myvariable= new Object()
}
myvariable.number=10
...



Title: Re: Include during runtime in Scripts
Post by: SvenK on May 02, 2013, 07:05:27 pm
Quote
EA will not include the same script twice, so in your example if you include B in a script X, and subsequently A in the same script X, than this second include of A is ignored and the changes made in B will be visible to X.

I wasn't aware of that. So my concerns were if superfluous. Thanks for the clarification.