• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Weird continual crash, maybe a problem with multiple objects having same script

3 posts in this topic

I have the task of incorporating AngelScript into our game engine and I'm having some problems.  The engine uses the concept of a GameObject, which can have a script attached to it.  When I have the same script running on different GameObjects, things seem to get strange with seemingly random crashes.



I keep getting the following crash in the function asCContext::ExecuteNext():


case asBC_RDR4:

*(asDWORD*)(l_fp - asBC_SWORDARG0(l_bc)) = **(asDWORD**)&m_regs.valueRegister; <---EXC_BAD_ACCESS( code=1, address=0xfffffff1 )




The following crash always happens when the same script is compiled for multiple GameObjects of the same type, and I examined the m_currentFunction variable in the context and it was calling a function that was defined in the script file and not a globally registered function.


case asBC_WRTV4:

**(asDWORD**)&m_regs.valueRegister = *(l_fp - asBC_SWORDARG0(l_bc));<---EXC_BAD_ACCESS( code=2, address=0xfffffff1 )






What are some general things to check for with theses particular crashes happening? 


Share this post

Link to post
Share on other sites

My first thought is that you're doing something wrong when reusing the context or script modules. Can you show us how you've integrated the script engine? 


A second thought is that perhaps you've encountered some obscure bug in the script compiler that makes it generate invalid bytecode. You've already found m_currentFunction. Can you use it to identify the script code that is being executed and show the script code here, so I can have a look at it?



Does the crash always occur on the same place in the script? You say the crash happens when running the same script on different GameObjects. Does this mean the script works as expected when you only have one GameObject? That would pretty much rule out a problem with the script it self, or a bug in the script compiler.


You also say you compile the same script for multiple GameObjects. Are you reusing the same script module when you do this? Did you cache the asIScriptFunctions before you reused the module (it would invalidate the script function unless you call AddRef on it). 


Share this post

Link to post
Share on other sites

Hi! Thanks for getting back to me and sorry for my delay in responding.


Script engine integration:


I use the scriptbuilder add on to compile the script code:


CScriptBuilder builder;


Currently, each script compiled gets it's own module, but I thought I saw that you have a module only for each different type of object, so there's a difference.


Then, I cache the function's to be called like so:


UpdateFunction = Type->GetMethodByDecl("void Update( float )");


After this I get the factory function that is used to create the script object like so:


std::string s = std::string( Type->GetName() ) + "@ " + std::string( Type->GetName() ) + "( GAMEOBJECT@ )";

FactoryFunc = Type->GetFactoryByDecl( s.c_str() );


and from the c++ side I call this to create the class along with all of it's variables:


Context->Prepare( FactoryFunc );

Context->SetArgObject( 0, GameObject ); //the c++ gameobject.



next, I cache the newly created script object in the GameObject:


GameObject->ScriptFactory = *( (asIScriptObject**)Context->GetAddressOfReturnValue() );





Here is what my script's look like:


class MainPlayer : IGameObjectScript { //IGameObjectScript is an interface with no methods defined.

#include "SharedCode.as"




//the App will pass in the newly created GameObject.

MainPlayer( GAMEOBJECT@ Object ) {

@GameObject = @Object;






void Update( float DeltaTime ) {

Thing1 = ComputeSomethingReallyFast( @GameObject );




//variable list.

float ScaleFactor = 1.13f;

float Thing1 = 0;

float Thing2 = ( 20.f / 60.f );

vector4 ComputedPosition;



the SharedCode.as file has various functions defined that are common to a set of GameObject's that share the same script file.


float ComputeSomethingReallyFast( GAMEOBJECT@ GameObject ) {

return 9000.f;




I register the GAMEOBJECT like so:

RegisterObjectType( "GAMEOBJECT", 0, asOBJ_REF | asOBJ_NOCOUNT );


RegisterGlobalFunction( "void GameObject_GetPosition( GAMEOBJECT@ GameObject, vector4& Position )", asFUNCTIONPR( GameObject_GetPosition, ( GAMEOBJECT* GameObject, vector4* Position ), void ), asCALL_CDECL );

RegisterGlobalFunction( "void GameObject_SetPosition( GAMEOBJECT@ GameObject, vector4& Position )", asFUNCTIONPR( GameObject_SetPosition, ( GAMEOBJECT* GameObject, vector4* Position ), void ), asCALL_CDECL );



I register the vector4 class like so:

RegisterObjectType( "vector4", sizeof( vector4 ), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA ); assert( r >= 0 );


RegisterObjectBehaviour( "vector4", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR( ConstructVector4x, (vector4*), void), asCALL_CDECL_OBJLAST );

RegisterObjectBehaviour( "vector4", asBEHAVE_CONSTRUCT"void f( float, float, float, float )", asFUNCTIONPR( ConstructVector4, ( float, float, float, float, vector4* ), void ), asCALL_CDECL_OBJLAST );


RegisterObjectMethod( "vector4", "vector4 &opAssign(const vector4 &in)", asFUNCTIONPR( Vector4Assignment,(const vector4&, vector4*), vector4& ), asCALL_CDECL_OBJLAST);


RegisterObjectProperty( "vector4", "float x", offsetof( vector4, x ) );

RegisterObjectProperty( "vector4", "float y", offsetof( vector4, y ) );

RegisterObjectProperty( "vector4", "float z", offsetof( vector4, z ) );

RegisterObjectProperty( "vector4", "float w", offsetof( vector4, w ) );


RegisterGlobalFunction( "const vector4& vector4_scale( vector4& result , const vector4& v , float scale )", asFUNCTIONPR( vector4_scale, ( vector4* result , const vector4* v , float scale ), const vector4* ), asCALL_CDECL );



Can functions that are registered like this maybe cause problems in the script compiler if called like this:

vector4 Temp;

vector4_scale( Temp, ComputedPosition, ScaleFactor );



instead of like this where I actually use the return value?


ComputedPosition = vector4_scale( Temp, ComputedPosition, ScaleFactor );



Now, each game object is updated like so:


Context->Prepare( FactoryFunc );

Context->SetObject( GameObject->ScriptFactory );

Context->SetArgFloat( 0, FloatValue ); //FloatValue is just some number that get's passed to the script function every frame from the App or c++ side.




Doing it this way keeps all script data tied to the one owning gameobject and there are no internal conflicts with the variables correct?


This seems like the right way to instantiate all script objects but when I have several objects all sharing the same script file things seem to go awry or maybe I'm just doing something stupid.


There are times when a crash occurs and I inspect the m_currentFunction and it's pointing to a function defined in the script file SharedCode.as and I was wondering why this could possibly be a problem because the code appears to be correct.


If I'm not being clear just let me know and I will try to be more specific.


Thanks for your help.


Share this post

Link to post
Share on other sites
I'll need to do a more detailed review of the info you posted, but after a quick readthrough I see nothing wrong with what you're doing.

The script you're using is also quite trivial so I find it hard to believe the you've stumbled on a bug in the compiler. Though, it can't be ruled out.

Yet, I can't help but think that you might be mixing the function pointers and object instances. Even though the modules are compiled from the same source code each object type will be unique and you must use the correct function pointer with the matching object instance.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0