Load external files

Forst of all, props to the great work on the framework!!!

Second, I’m using the Script Editor to run a couple of different bots. Now, they are using a couple of functions that I have to repeat on each file. Is there a way to load an external file with these functions? The #include is only for old-man-C and I can’t use namespaces on scripts. Is there a way to do this?

Thank you for the feedback.

You can use the following script code to read an external file:

var fileAsText = System.IO.File.ReadAllText(@"C:\Users\John\Desktop\test-file.txt");

But that will only load the file into the fileAsText variable, right? It won’t be parsed and whatever functions are inside it won’t be available to be used.

I mean, the Script Interface shows an error and won’t even let the script start.

Yes.

What error do you see and for which code?

This module file will keep all my generic/global functions to reuse in all my scripts. So for example, I have a function that outputs a message, sleeps a bit and then refreshes the memory variable. Store it in “c:\functions.cs” as:

void sleep()
{
	Host.Log("sleeping for a bit then reading values after I've slept");
	Host.Delay(1000);
	Sanderling.InvalidateMeasurement();
	Measurement = Sanderling?.MemoryMeasurementParsed?.Value;
}

Withing the main script inside the Script Engine Interface I will have the code that loads this file alongside the normal bot code. For example:

var fileAsText = System.IO.File.ReadAllText(@"C:\test.cs");
while (true)
{
	Host.Log("I'm pre-sleep");
	sleep();
	Host.Log("I'm post-sleep");
}

The issue is that the Script Engine will not recognize that the “sleep” function exists because it is loaded in the fileAsText variable and not actually parsed to the engine and applied to any context so the Script Interface will trigger an error like:

[
  {
    "TimeDateTimeIntraDayCal": "13.13.34",
    "TimeDateTimeIntraSecMilliString": "021",
    "CaptionString": "System.Exception: compilation error: (3,1): error CS0103: The name 'sleep' does not exist in the current context\r\n   at BotSharp.ScriptRun.ScriptRun.<>c__DisplayClass61_0.<Start>b__0()",
    "LineIndex": 2,
    "CharacterIndexInLine": 0,
    "LineIndexInAvalonEdit": 3,
    "CharacterIndexInLineInAvalonEdit": 1
  }
]

For this case, I would suggest another approach:
Instead of loading test.cs while the script is running, decide what should be in there before starting the script.
You can then remove the statement to load the file from the script and instead drop the functions.cs from windows explorer into the script editor on that location.

This way, the sleep function will become available for use in other parts of the script.

Hummm… maybe I should try a different approach. What about adding these generic functions and then compiling the interface with them embedded?

Since I’m compiling the Sanderling.exe in Visual Studio I could add them directly to the code. Is there any specific spot in the code that you would suggest adding these types of functions?

At the moment, I do not see any problem with this.

I am a little bit confused. Does this not also require to decide the content of the functions before running the script?