Advertisement

Conditional expression

Started by April 01, 2007 11:53 AM
16 comments, last by mono2k 17 years, 7 months ago
I don't know is this a bug or not, just want to report:

string_t offer_text;
offer_text += current_offer.invite.game_type == 0 ? "First game type" : (current_offer.invite.game_type == 1 ? "Second game type" : "Third game type");
Silently fails when game_type == 1. No exception raised, the script just stops executing at this line and ExecuteString returns 0. In case of game_type == 0 or 2, everything works fine.
... and looks like there's another one related to boolean variables handling

in the main script I have a global boolean variable and two little functions

bool showing_offer = false;void show_offer(){        showing_offer = true;        // doing something useful...};void on_offer_cancelled(){	if(showing_offer)	{                // doing some stuff here                // ...		showing_offer = false;	};};


First I call show_offer() via ExecuteString(0, "show_offer()" ...) using my own context.
But after I execute the following code via ExecuteString the variable is not changed!

showing_offer = false; net.decline_offer()


... since on the next ExecuteString(0, "on_offer_cancelled()", ...) I successfully pass inside "if(showing_offer)" block.

I've changed showing_offer to int32 and now I compare it to 0 or 1 and hopefully everything works as expected. So, it looks like this problem is related to boolean variables only.
Advertisement
That ternary conditional operator has given me a lot of grief, with various bugs in the past. You've likely stumbled upon another bug by chaining the operator like this. You should be able to work around it by using common if-else statements instead.

I'll take a look at the problem with boolean variables as well.

What version of AngelScript are you using?

Thanks,
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

2.8.0a

I've stopped to use booleans, ints are ok :-)
That's strange, I thought I had fixed the behaviour of booleans in 2.8.0a. I guess I missed a scenario. I'll try to have it fixed as soon as possible.

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

It looks like something is wrong with ExecuteString, since only ExecuteString does not change global booleans declared in the prebuilt main module. Every function in that module works correctly with booleans then I call it directly or even via ExecuteString.

bool my_var = false;void set_var_to_true(){   my_var = true;};void other_func(){};


AddCodeSection(...);
Build(0);

....

ExecuteString(0, "my_var = true; other_func()") - fails to change my_var

ExecuteString(0, "set_var_to_true(); other_func()") - works!
Advertisement
I do not think it is directly related to ExecuteString itself, since that script is compiled just like any other function. But I'll definitely test it to see if I can reproduce the problem with ExecuteString.

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'm not able to reproduce the problem.

I wrote the following test:

static const char *script2 ="bool gFlag = false;\n""void Set() {gFlag = true;}\n""void DoNothing() {}\n";bool Test(){	bool fail = false;	int r;	COutStream out; 	asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);		engine->SetMessageCallback(asMETHOD(COutStream,Callback), &out, asCALL_THISCALL);	RegisterScriptString(engine);	engine->RegisterGlobalFunction("void Assert(bool)", asFUNCTION(Assert), asCALL_GENERIC);	// TEST 2	engine->AddScriptSection(0, "script", script2, strlen(script2));	r = engine->Build(0);	if( r < 0 ) fail = true;	int id = engine->GetGlobalVarIDByName(0, "gFlag");	bool *flag = (bool*)engine->GetGlobalVarPointer(id);	*(int*)flag = 0xCDCDCDCD;	engine->ExecuteString(0, "Set()");	if( *flag != true )		fail = true;	engine->ExecuteString(0, "Assert(gFlag == true)");	engine->ExecuteString(0, "gFlag = false; DoNothing()");	if( *flag != false )		fail = false;	engine->ExecuteString(0, "Assert(gFlag == false)");	engine->ExecuteString(0, "gFlag = true; DoNothing()");	if( *flag != true )		fail = false;	engine->ExecuteString(0, "Assert(gFlag == true)");	engine->Release();	// Success	return fail;}


The global variable gFlag is being updated correctly.

Of course, I'm currently testing with the version in SVN. And I'm using MSVC6.

What compiler are you using?

Can you send me some sample code that reproduces the problem? Or give the version in SVN a try to see if the problem remains?

Regards,
Andreas

PS. Still haven't looked at the conditional operator ?:

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'm using VS2005 SP1. Will try to reproduce myself tomorrow.
This may be related to the bug fixes I made (and just sent to Andreas tonight). There were aliasing issues that, depending on the compiler and its optimization settings, could cause bugs related to booleans (bytes and words actually). Hopefully my fixes fix the problem you are seeing.

This topic is closed to new replies.

Advertisement