Book a Demo

Author Topic: Include during runtime in Scripts  (Read 4134 times)

SvenK

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Include during runtime in Scripts
« 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")



Paulus

  • EA User
  • **
  • Posts: 152
  • Karma: +0/-0
    • View Profile
Re: Include during runtime in Scripts
« Reply #1 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?
« Last Edit: April 30, 2013, 03:16:47 am by pmaessen »

SvenK

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Include during runtime in Scripts
« Reply #2 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.

Paulus

  • EA User
  • **
  • Posts: 152
  • Karma: +0/-0
    • View Profile
Re: Include during runtime in Scripts
« Reply #3 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
...



« Last Edit: April 30, 2013, 06:08:23 pm by pmaessen »

SvenK

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Include during runtime in Scripts
« Reply #4 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.