Require/Include type functionality

Started by
4 comments, last by WitchLord 17 years, 1 month ago
I want to write a function very similar to PHP's require/include functions. Assuming your not familiar with those, they work differently than a preprocessor as they are included during execution. Basically I need to be able to load a script, execute it, and then return back to the original script. One catch, they need to share stacks, all variables, functions, etc need to be accessible from the included code, just as if it was actually in the original script. Is this possible with AngelScript? -- Dave Krusu
Advertisement
Just a quick update:

One reason why a preprocessor type include won't work for what I want is if you had the following:

if(whatever == value)    #include <script>...


Obviously it would execute the first line of the included script and think it hit the end of the if statement.
AngelScript doesn't have direct support for this, but you might be able to use the import feature to do what you want. Basically it would work something like this:

// AngelScriptimport void doSomething() from "another module";void myFunction(){   LoadAndBindScript("another script");      // Call the recently bound function   doSomething();}


// C++void LoadAndBindScript(string &str){   // Remove current bindings, so that the current module can be released   engine->UnbindAllImportedFunctions("main module");   // Load and compile the new script   string script = LoadScript(str.c_str());   engine->AddScriptSection("another module", script.c_str());   engine->Build("another module");   // Bind the new functions   engine->BindAllImportedFunctions("main module");}



The dynamically loaded script won't be able to access variables directly from the static script, but you can bind functions in both directions so a two-way interface can be established to exchange data between the modules.

If you don't need to dynamically load these modules then you could use a slightly different #include directive (perhaps call it #embed, instead?), one that would include the script content and wrap it inside a statement block. Of course the script content included this way cannot declare new functions nor global variables.

Help me understand this feature better, so that I can decide whether or not it is something that might be interesting to add to AngelScript.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Documentation on the functions:
require
include

They are basically the same function, except one requires that the file exists or it'll stop execution, that part I can of course handle myself. I'm not positive how it actually works internally, but from what I understand it basically runs an evaluation function it to execute the code. Very similar to your ExecuteString function except it can access all variables, functions, etc in the main script.
I would just wrap the included script in a statement block, except PHP's version allows you to declare functions. After some more investigation it looks like this is tied in with ZendEngine (the scripting library) as it uses tokens for include/require.

I could probably parse the script and do the same effect(by placing the code directly in the main one, and putting functions and things at the top), but of course that would be painfully slow.
Sounds like a handy feature for scripts that output text (or HTML in the case of PHP). Especially if you take into account that the included script can be generated differently based on input parameters, defined at runtime.

I think I'll add this as a low priority entry in my to-do list. At least until I can figure out how it could be implemented in AngelScript.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement