Advertisement

calling c++ function in my own script

Started by February 21, 2003 07:14 AM
2 comments, last by ddungum 21 years, 9 months ago
Hello there. I''m creating my own C-like script and its virtual machine. Currently, it lacks the most important feature, communication with c++ native code. But I have totally no concept about it. I read the article about this issue in GPG 1. Inline assembly was used to call C function In that article, but accornding to MSDN, inline assembly can''t call c++ function. And if possible, I want my virtual machine to be executed on any platform by not using inline assembly. I heard that UnrealScript is able to call its underlying c++ native function, and Unreal engine is running on not-pc platform like x-box. So i believe that there is not-platform-specific way to achieve it. Can somebody give me any infomation? I can''t find any good infomation in internet.
Well, you would have a set of predefined functions, each one based on a byte value (or wider if you have that many functions). Then there are a few ways to go about this... you can either make a function pointer map, or use an if/switch statement. (function pointer map is MUCH faster).

Say you want the first character (0x01) to be a clear screen call, then followed by the color to clear in r,g,b (all unsigned chars), we''ll use black for now, then we have another character, 0x2 (made up for no apparent reason) for an unconditional jump, followed by a location (32-bit location).


  //So your byte code would look like this:unsigned char *ByteCode = {0x01,0x00,0x00,0x00,0x2,0x00,0x00,0x00,0x00};//Used to store our positionunsigned long ProgramCounter=0;//Start from the beginning./*Our function prototypes, would be in a .h file somewhere, and the functions in another .cpp file... but i''m putting them here for simplicity sake*/void cls(void){ unsigned char r,g,b; ++ProgramCounter; r = ByteCode[ProgramCounter]; ++ProgramCounter; g = ByteCode[ProgramCounter]; ++ProgramCounter; b = ByteCode[ProgramCounter];//Our clear screen code would go here. ++ProgramCounter;}void jump(void){ unsigned long *jumploc = &ByteCode[ProgramCounter+1]; ProgramCounter = *jumploc; //Jumps to our new location!}//Stores our function pointersvoid (*CallFunctions[2])(void) = {NULL, cls, jump};//Our function to do process..void ProcessNext(void){ CallFunctions[ByteCode[ProgramCounter]]();}  


Hope this helps a Bit .
Advertisement
The way most game engines handle scripting is by running the virtual machine within the actual game. Then, at startup time, the engine "registers" C/C++ functions with the virtual machine that coincide with functions that can be called in a script.

So, when a user writes a script, they have a bunch of predefined functions they can use. When the virtual machine interprets the script, it calls the registered C/C++ function whenever it encounters the script function of the same name.

Not sure if that''s what you''re trying to do, or if you want to run the virtual machine in its own process. In that case, you might want to do a similar thing, but have the virtual machine load up a DLL written in C++ that has the functions you need, and then things would progress similarly to how they do in the game engines...
Check out this link:

http://www.gamedev.net/reference/list.asp?categoryid=45#118

The article "Creating a Scripting System in C++ Part V" discusses calling internal functions from scripts.

This topic is closed to new replies.

Advertisement