Jump to content

  • Log In with Google      Sign In   
  • Create Account


justin12343

Member Since 19 Jan 2010
Offline Last Active Sep 18 2013 01:32 PM
-----

Topics I've Started

Null pointer returned from constructor?

02 August 2013 - 07:22 PM

This code that used to work in the previous version of AngelScript now returns a null pointer since updating to version 2.27.0. It's pretty much identical to what's in the manual, so I'm wondering if the method for getting the object pointer has changed since the update.

void Object::CallConstructor()
{
	if (constructorCalled)
		return;

	asIScriptContext *context = scriptManager->GetContext();

	if (context == NULL)
		return;

	if (context->Prepare( funcConstructor) < 0)
	{
		printf( "There was an error preparing the context.\n");
		return;
	}

	*((Object**)context->GetAddressOfArg(0)) = this;
	//AddRef();

	int r = context->Execute();

	if (r != asEXECUTION_FINISHED)
	{
		if (r == asEXECUTION_EXCEPTION)
		{
			const char *exception = context->GetExceptionString();
			printf( "An Exception, '%s', Occurred In The Constructor.\n", exception);
			return;
		}

		if (r == asERROR)
		{
			printf( "An Unexpected Error Occurred In The Constructor.\n");
			return;
		}
	}
	else
	{
		pScrObj = *((asIScriptObject**)context->GetAddressOfReturnValue());// <---------------------
		pScrObj->AddRef(); //Must Be Called or else Pure Virtual Error!
		
		context->Unprepare();
		constructorCalled = true;
	}
}

Multithreaded script execution

20 July 2013 - 11:53 AM

I have been using AngelScript in my engine for a while and now I'm trying out its multithreading capabilities. There's not much documentation on this subject, so I'm struggling a bit. I'm trying to acheive a variable amount of threads, maybe 2 or 3, to be used to execute multiple scripts at a given time. The documentation says more than one engine can be used right? Can I only use one? Has anybody had any luck with multithreading with angelscript and does anyone have any sample code available?

 

I'm trying to keep it as simple as possible:

 

This is my execution function that my threads call:

int threadExecuteContext( void *data)
{
	asIScriptContext *ctx = (asIScriptContext*)data;

	int r = ctx->Execute();

	if (r != asEXECUTION_FINISHED)
	{
		if (r == asEXECUTION_EXCEPTION)
			std::cout << "An exception occurred while executing function on thread id " << SDL_ThreadID() << "." << std::endl;

		if (r == asEXECUTION_ERROR)
			std::cout << "An error occurred while executing function on thread id " << SDL_ThreadID() << "." << std::endl;

		return asERROR;
	}
	else
	{
		ctx->Unprepare();
	}

	return 0;	
}

This one prepares a function to be called and waits for a available thread:

int ScriptManager::ExecuteFunction( asIScriptFunction *func, asIScriptObject *obj)
{
	asIScriptContext *ctx = GetContext();

	if (ctx == NULL)
		return asERROR;

	if (ctx->Prepare( func) < 0)
	{
		std::cout << "Could not prepare script function" << std::endl;
		return asERROR;
	}

	ctx->SetObject( obj);

	SDL_Thread *th = NULL;

	while (th == NULL)
		th = GetThread( ctx);

	return asSUCCESS;
}

And lastly here is my GetThread function. It maintains the maximum thread limit by waiting for one to finish:

SDL_Thread *ScriptManager::GetThread( asIScriptContext *ctx)
{
	SDL_Thread *th = NULL;

	if (threads.size() < MAX_THREADS)
	{
		th = SDL_CreateThread( threadExecuteContext, "Script Execution Thread", ctx);
		threads.push_back( th);
	}
	else
	{
		int returned;
		thread_iterator it = threads.begin();

		SDL_WaitThread( *it, &returned);
		threads.erase( it);

		th = SDL_CreateThread( threadExecuteContext, "Script Execution Thread", ctx);
		threads.push_back( th);
	}

	return th;
}

This method has problems. The context always has an unintialized state and does not execute anything.


Converting from frame based movement to frame rate independent movement

08 June 2013 - 04:33 PM

I'm trying to convert my engine to use frame rate independent movement. Wouldn't that require that I simply multiply by the amount of time passed?

 

I'm calculating the frame time like so:

double FrameTime = (CurrentTime - LastTime)/1000;
double dt = 1.0/fps;

And my previous movement code is this:

//X Movement
x += cos(degtorad(angle)) * x_speed;
y -= sin(degtorad(angle)) * x_speed;

//Y Movement
y += y_speed;

 

 

So, would my movement code then change to look more like this:

//X Movement
x += cos(degtorad(angle)) * (x_speed * FrameTime/dt);
y -= sin(degtorad(angle)) * (x_speed * FrameTime/dt);

//Y Movement
y += (y_speed * FrameTime/dt);

 

This doesn't produce accurate movements. Rather than this method, should I be considering time when I change speed values, sort of like this:

if (left is down)
     x_speed -= x_acceleration*(FrameTime/dt);

if (player not on solid ground)
     y_speed += gravity*(FrameTime/dt);

 


Pass an asIScriptObject to a script?

22 May 2013 - 07:53 AM

I'm trying to pass a handle of a game object to a script function, but its not working properly.

 

CScriptAny *Object::GetScriptObject()
{
	CScriptAny *n = new CScriptAny( scriptManager->GetEngine());
	int typeId = scriptManager->GetEngine()->GetTypeIdByDecl("Base_Object_IMP@");
	n->Store( pScrObj, typeId);

	return n;
}

Base_Object_IMP is the interface that the parent class of all scripted game objects, Base_Object inherit. Each instance store a asIScriptObject returned from the constructor. The function above fails with an access violation and it doesn't seem to be able to find the type id for 'Base_Object'.

 

I'm trying to cast from the base type to a another child type, like so:

Base_Object @base = null;
Object @obj = find_instance( objPlayer, Player);

if (obj != null)
	obj.GetScriptObject().retrieve( @base);

objPlayer_controller @PlayerHandle = cast<objPlayer_controller>( @base);
 

 

This is what I'm trying to do:

asIScriptObject -> Base_Object_IMP@ -> Base_Object@ -> objPlayer_controller@ (or any other child type).

 

Is there a proper way to do this?


Porting point sprites from DirectX

21 December 2012 - 03:09 PM

I've hit a road block in the development of my OpenGL renderer regarding the size of point sprites. I have been writing the features of my renderers for DirectX first and then I immediately port them to OpenGL after I get them working, since Windows is my primary platform and DirectX seems to be easier to write for (come get it haters lol). I noticed that you can't set the point size for each point sprite individually in OpenGL as you can in DirectX (and OpenGLES), unless you use a shader. My readings tell me that I can cheat using the Z value and distance attenuation to achieve the same effect. My question is  how do I use this to make my point sprites look exactly the same in OpenGL as they would in DirectX? I trying to avoid taking the shader route for this because I want my engine to be able to run on older hardware.


PARTNERS