How to "Include" a file from a string?

Blitz3D Forums/Blitz3D Programming/How to "Include" a file from a string?

FearedMachina(Posted 2011) [#1]
Okay, so the problem I'm having is that I'm essentailly trying to let the user select a .bb file (with a prompt) for inclusion in the main script.

However, I can't figure out how to get blitz3d to do this. I've got the file path saved as a string, thats no problem. The problem is this

"Include inputstring$"

returns an error. I can't put it in quotes either, because I'm guessing blitz3d verifies all Include file paths before program execution. So, is there any way to allow someone to SELECT a file to include? or is the command always this linear?

Your help would be appreciated.

Last edited 2011


GfK(Posted 2011) [#2]
Can't be done, I'm afraid. The "Include" part is done at compile time and you cannot add any more code to it afterwards.


Yasha(Posted 2011) [#3]
the main script

verifies all Include file paths before program execution


Sounds like you've completely misunderstood both the Include directive, and how Blitz3D runs.

Blitz3D is a compiled language. What you've written is not a script, but is deployed as an executable binary - you don't ever need to distribute the source. (If you see people talking about scripts on this forum, they're referring to addons in Lua or some other language, which is an unrelated issue having nothing to do with B3D's core.)

Because of this, everything that will be a part of the program has to be found, processed and linked at compile-time. This means that Included files have to be pasted together into a single long piece of source before the compiler processes them. As a result, the executable never actually sees the Include statement, because it's been replaced with the complete text in the named file (this is why it's a "directive" rather than a "command" - it gives an instruction to the compiler, not to the runtime).

This in turn means that Include quite simply does not exist at runtime, and isn't a procedure that can be executed by a running program (if you think about it, there's no way this could even work logically). Since variables and program logic do not exist at compile-time, there's no way to use Include with them (even then, this wouldn't help your users).

If you simply want your users to choose between functionalities that you've written and were intending to distribute with the main program, the simplest thing is to include them all into one big exe and use a Select structure to dispatch to the right functions - within the relevant files - at runtime (there are no namespaces in B3D so anything defined in any Included file is visible throughout the program).

If you want to either distribute addon modules later, or let your users write mods, you'll need an actual scripting language, or possibly something more complicated. There are several scripting languages floating about the forum, although none of the ones available for B3D are perfect sadly. Other options include TCC (see my signature link, makes use of scripts written in the C programming language that compile to binaries at runtime), or using DLLs linked with the CallDLL function - but B3D doesn't let you make DLLs so again you'd need to use another language (C is good!).