Jump to content
  • Advertisement
Sign in to follow this  
B_old

LineCallback for ExecuteString()

This topic is 3678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!