LineCallback for ExecuteString()

Started by
5 comments, last by B_old 15 years, 4 months ago
Hi, I wanted to add a LineCallback as seen in one of the samples that aborts ExecuteString after a certain time. And to test it, I set up something like this:

bool ScriptEngine::init()
{
	m_engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
		
	if (!m_engine) return false;

	m_engine->SetMessageCallback(asMETHOD(ScriptEngine, messageCallback), this, asCALL_THISCALL);

	m_defaultContext = m_engine->CreateContext();

	return true;
}


void ScriptEngine::release()
{
	if (m_defaultContext) m_defaultContext->Release();

	if (m_engine) 
	{
		m_engine->Release();
		m_engine = 0;
	}
}


bool ScriptEngine::executeString(const char *script)
{
        if (!m_engine) return false;

	return m_engine->ExecuteString(0, script, &m_defaultContext) == 0;
}


I planned to add the LineCallback to the context later and just tested the above first. The script was executed, but when exiting the application Visual Leak Detector alerted me that it found a leak. This does not happen when I call ExecuteString() without the context. Any idea what I am doing wrong? I seem to be releasing the context correctly. Any idea what I am doing wrong?
Advertisement
http://www.angelcode.com/angelscript/sdk/docs/manual/classas_i_script_engine.html#00870375f64d9c99725d342432308676

You need to pass the flag asEXECSTRING_USE_MY_CONTEXT to ExecuteString to have it use the context you created. Otherwise it will create it's own context that it returns to you.

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 see. I never used the contexts before. Is it ok to use the same context for several ExecuteString() calls?

A stupid question maybe, but if I pass the pointer to my own context, why don't you just assume I also want to use it?
Because you could have forgotten to initialize the pointer. Now a question for you, if you pass a pointer to a pointer to a context to a function, why didn't you suspect it would be a return value? ;)

Yes, you can use the same context for several ExecuteString calls, but the script function must finish before you reuse the context for the next ExecuteString call, otherwise it will fail.

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

Heh, perfectly valid question. It seems I even misread your first answer. It seems a bit clearer now (thanks!), so I am going to proceed with adding that line callback. What I am doing is the normal way to let ExecuteString() time out, isn't it?
There are only 2 ways of letting contexts time out:


1. through the line callback, where the callback will be called for each script statement, letting the callback decide whether to abort the execution or not.

2. through a secondary thread, that sleeps until the time out periods ends, and when it wakes up it calls the contexts's abort method (if it's still running, of course).


Performance wise the second option is better, but if you don't want to hazzle with multiple threads the first option is probably easier.

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 see. Thanks for the info!

This topic is closed to new replies.

Advertisement