When researching information for my recent scripting experiments one of the posts I came across highlighted an issues that when adding the script code using the Script Control addcode method the script would execute. The post cited was a simple script:
sub main MsgBox("main executed") end sub main
I had also observed a few cases myself but it appeared inconsistent. Now I’ve come back to look at the problem, by just looking at the code with fresh eyes the solution was obvious, wasn’t it?
If I step back to the early 1970’s using basic interpreters, I entered the subroutines/functions then added a line to specify what routine to call. At which point the code would run. So if we look at the code example we see the subroutine plus a line “main” which I can only imagine is saying run “main”. A quick check with my EA Script Tester AddIn, and yes this appears to solve the issue. I removed the last line i.e. “main” and the AddCode just adds the code without running, and the run does as we expect. And to check, putting “main” at the end and the script runs immediately on adding the code as well as when requested to run. BTW: I’m not sure that is what was referred to as the solution, which referenced needing to strip out the calls; however I felt it was worth checking out so I understand the rules.
Now just as I am cautious and curious I ran a few more tests. I started with a script that included a few subroutines. Ignoring setting parameters and any required objects the basic code consists of:
myScriptControl.AddCode(myScriptAsAString) result = myScriptControl.Run("NAME OF SUBROUTINE TO RUN")
So if I create a new script, as below, which has several subroutines I performed a few tests.
sub main msgbox("Hello") end sub sub A msgbox("A") end sub sub B msgbox ("B") end sub main
- Running the code as illustrated it ran twice.
- I then removed the last line (“main”) and it only runs with the .run method is set. Worth noting that you can specify any subroutine name and it will run accordingly. But don’t forget that if the subroutine has parameters you need to configure and set the parameters object as the second argument of the run call as well as set up any dependent objects.
- I added calls to the subroutines between them and yes it does execute on loading . In testing it looks like all the code is loaded and then executed.
- Finally, I added one or more of the subroutine names at the end of the script and as I would expect it would run them in the defined sequence.
In practice, when running EA scripts either in an AddIn or external program, to ensure the code isn’t executed when added we need to strip out any lines containing a reference to a subroutine in our script that are NOT contained within a function or subroutine otherwise they will instruct the script control to execute upon adding code. This was included in the initial post and although it may be a challenge not sure there is anything else – it’s the way the “interpreter” works and I guess for backwards compatibility will always work.
Of course, we could want this behaviour – in fact I can see a case where some subroutines run to initialise “stuff” before a call is made to a specified subroutine – up to the designer/user.
Having looked at a lot of documents on scripting in windows during my recent experiments I can’t recall any reference to the behaviour outlined above, although there are references to a distinct lack of documentation about the script control in general and perhaps this is the real issue!!
I hope this is useful, and if there are other “script funnies” I’d be interested to here.
PS: This was the issue highlight in my test Case #6 if you are interested.