🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

push / pop bytecodes, variable names, pointers

Started by
9 comments, last by Gyrbo 17 years, 2 months ago
Goal: I want to know the name, type, and pointer to each variable that is pushed onto the stack in AS, and know when those variables get popped. The switch statement inside of asCContext::ExecuteNext looks like where I need to hook into AS for this, but I also need to know the string names for the variables. It looks like asCVariableScope is where this information is kept. The problem is that the variable lists are destroyed after the scripts are compiled. Am I wrong or overlooking anything? Am I able to retrieve the string name of a variable while it is executing? If this is not possible, then I am thinking I need to build a master list of variables at compile time, and insert an additional bytecode next to each push/pop so that I can index against and get the name of the variable. Thats a bit of hacking, and I wanted to get this question out of the way before I start molesting Angelscript... Thanks Joe
Advertisement
Variables are not pushed onto the stack one by one, so doing it they way you're thinking won't work. However, take a look at test_debug.cpp in test_features. I think it does pretty much what you want.

Especially this part may be interesting to you:

void PrintVariables(asIScriptContext *ctx, int stackLevel){	int numVars = ctx->GetVarCount(stackLevel);	asIScriptEngine *engine = ctx->GetEngine();	for( int n = 0; n < numVars; n++ )	{		int typeId = ctx->GetVarTypeId(n, stackLevel); 		void *varPointer = ctx->GetVarPointer(n, stackLevel);		if( typeId == engine->GetTypeIdByDecl(0, "int") )		{			print(" %s = %d\n", ctx->GetVarDeclaration(n, 0, stackLevel), *(int*)varPointer);		}		else if( typeId == engine->GetTypeIdByDecl(0, "string") )		{			asCScriptString *str = *(asCScriptString**)varPointer;			if( str )				print(" %s = '%s'\n", ctx->GetVarDeclaration(n, 0, stackLevel), str->buffer.c_str());			else				print(" %s = <null>\n", ctx->GetVarDeclaration(n, 0, stackLevel));		}	}}


There's one catch though (that I'll fix in a future version). Variables may occupy the same stack space in the function, if they are declared in different scopes. Currently AngelScript doesn't maintain information about the scope of the variables at runtime, so there's currently no easy way of knowing that unless you parse the script yourself.

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

I had a chance to play around with your suggestion, and I think it is what I needed. Thanks for the advice, that saved me a lot of work!

As far as the scope problem you described, I had thought about that too when I was thinking about my hack solution. But to be honest, having those other scopes visible is actually desirable in my situation. For the most part anyways. ;p

My current project makes extensive use of AS, and it is really working out well. I'll give you a link after I post some materials online.

Thanks,
Joe
Cool! I look forward to seeing what you're doing. :D

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

Quote: Original post by WitchLord
Variables are not pushed onto the stack one by one, so doing it they way you're thinking won't work. However, take a look at test_debug.cpp in test_features. I think it does pretty much what you want.

Especially this part may be interesting to you:

void PrintVariables(asIScriptContext *ctx, int stackLevel){	int numVars = ctx->GetVarCount(stackLevel);	asIScriptEngine *engine = ctx->GetEngine();	for( int n = 0; n < numVars; n++ )	{		int typeId = ctx->GetVarTypeId(n, stackLevel); 		void *varPointer = ctx->GetVarPointer(n, stackLevel);		if( typeId == engine->GetTypeIdByDecl(0, "int") )		{			print(" %s = %d\n", ctx->GetVarDeclaration(n, 0, stackLevel), *(int*)varPointer);		}		else if( typeId == engine->GetTypeIdByDecl(0, "string") )		{			asCScriptString *str = *(asCScriptString**)varPointer;			if( str )				print(" %s = '%s'\n", ctx->GetVarDeclaration(n, 0, stackLevel), str->buffer.c_str());			else				print(" %s = <null>\n", ctx->GetVarDeclaration(n, 0, stackLevel));		}	}}


There's one catch though (that I'll fix in a future version). Variables may occupy the same stack space in the function, if they are declared in different scopes. Currently AngelScript doesn't maintain information about the scope of the variables at runtime, so there's currently no easy way of knowing that unless you parse the script yourself.

Regards,
Andreas


for example, I have a problem about signals and cars. i constructed an algorithm as follows:

if (carPassed[n]
{
if n > 0
carPassed[n-1] = false;
signal[n] = 0;
if n > 0
signal[n-1] = 1;
if n > 1
signal[n-2] = 2;
}
for example, I have a problem about signals and cars. i constructed an algorithm as follows:

if (carPassed[n]
{
if n > 0
carPassed[n-1] = false;
signal[n] = 0;
if n > 0
signal[n-1] = 1;
if n > 1
signal[n-2] = 2;
}
for example, I have a problem about signals and cars. i constructed an algorithm as follows:

if (carPassed[n]
{
if n > 0
carPassed[n-1] = false;
signal[n] = 0;
if n > 0
signal[n-1] = 1;
if n > 1
signal[n-2] = 2;
}
for example, I have a problem about signals and cars. i constructed an algorithm as follows:

if (carPassed[n]
{
if n > 0
carPassed[n-1] = false;
signal[n] = 0;
if n > 0
signal[n-1] = 1;
if n > 1
signal[n-2] = 2;
}
for example, I have a problem about signals and cars. i constructed an algorithm as follows:

if (carPassed[n]
{
if n > 0
carPassed[n-1] = false;
signal[n] = 0;
if n > 0
signal[n-1] = 1;
if n > 1
signal[n-2] = 2;
}

...No, I'm just kidding. QUADRUPLE POST!

This topic is closed to new replies.

Advertisement