Followers 0

# How to...

## 6 posts in this topic

my problem is described in the generic handle documentation, but i don't know how to implement it.

this is basically what i do, each object is given an local pool of memory, the script can say how big that pool is. what i want to do, is create a struct, or class, with each script, get the size of the class, and tell my object to create a pool of memory for this class to occupy, then the script can request the pool, typecast it to the local class, and modify objects/memory in that pool directly.

it'd look like this:

c++ code:

class Obj{
private:
char *m_Buffer;
public:
void CreateBuffer(int size){
m_Buffer = new char[size];
}

char *GetBuffer(void){ //i tried registering this as an ref object, with the scripthandler add-on.
return m_Buffer;
}

//construct/destructors.
};

as script:

class Data{
int x;
};

void Update(Obj @A){
Data @D = cast<Data@>(A.GetBuffer());
print("x: "+D.x);
}

void Initialize(Obj @A){
A.CreateBuffer(sizeof(Data)); //is there anyway to get the size of an class?
Data @D = cast<Data@>(A.GetBuffer());
D.x = 0;
};

anywho, this causes angelscript to crash. any pointers on how to achieve this? Edited by slicer4ever
0

##### Share on other sites

I have a few questions :)

on which line does it crash? you can use the angelScript debugger to step through code and figure out exactly where it's crashing.

As for the sizeof() operator, I'm not (entirely) sure, but why not overload a function in the C++ side like so?


template <typename T>
unsigned int SizeOf(T *x){
return sizeof(T);
}

//register the function for different types. I haven't used AS in a while, so I'm
//not really sure about the parameters :)
ASengine->RegisterGlobalFunction("unsigned int sizeof(int @T)", asFUNCTION(SizeOf<int>), asCALL_CDECL);

ASengine->RegisterGlobalFunction("unsigned int sizeof(float @T)", asFUNCTION(SizeOf<float>), asCALL_CDECL);

//.
//.
//.
//and so on for all other types


0

##### Share on other sites

I have a few questions

on which line does it crash? you can use the angelScript debugger to step through code and figure out exactly where it's crashing.

I'll step-through it, and get back to you on this. fairly certain it's:
Data @D = cast<Data@>(A.GetBuffer());

though.

As for the sizeof() operator, I'm not (entirely) sure, but why not overload a function in the C++ side like so?

This won't work, as the class is dependent on the script. what i want to achieve is to allow each script to need only the external memory it needs to work. in the past i've built a storage container for this, but it meant doing look-ups to map variables/pointers. i'm hoping to find a way that would allow me to map a point in memory directly to a class.

edit: It's happening with this line:
A.GetBuffer();

i'm registering it like so:
ScriptEngine->RegisterObjectMethod("Obj", "ref GetBuffer()", asMETHOD(Obj, GetBuffer), asCALL_THISCALL);

edit2:
this is the crash dialog i get:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

line 174: as_callfunc_x86.cpp Edited by slicer4ever
0

##### Share on other sites

The memory for the script class is not meant to be allocated manually by the application. The script engine must allocate and initialize the memory. A script class doesn't just contain the members declared by the script, there are also hidden data (object type, ref counter, etc) that the application shouldn't manipulate directly.

May I ask why you want to do something like this? Perhaps there is another way.

0

##### Share on other sites

The memory for the script class is not meant to be allocated manually by the application. The script engine must allocate and initialize the memory. A script class doesn't just contain the members declared by the script, there are also hidden data (object type, ref counter, etc) that the application shouldn't manipulate directly.

May I ask why you want to do something like this? Perhaps there is another way.

thanks for the response Andreas=-)
this is basically what i'm attempting to achieve. my game has a generic "Ability" class, what i'd like to do is use angelscript to manage what an ability does, this means different scripts, have different needs. in the past, i wrote a generic container(essentially a dictionary add-on), that got attached to each instance of the class, and the script could customize that container as it needed. the problem with this approach was doing the look-ups into the container to get the appropriate information the script needs.

I was hoping that their would be a way to directly map memory that the script needs to some class, so that i could store a pointer to the scripts unique class in the c++ ability object.

basically, i want to create an instance of a class(this class is unique to each script), attach it to the ability object, and then when the ability is passed to a generic "update" function in the script, it can pull the custom class pointer from the ability object, and i don't have to do any ugly look-ups to continue working/evaluating what an ability is doing.

i think it'd look like this in angelscript:

class Data{
int y;
};

void Initialize(Ablity @Ab){
Data @x = Data();
Ab.SetDataObject(x);
};

void Update(Ability @Ab){
Data @x = @Ab.GetDataObject();
x.y = 100;
};

obviously not exact, but it's what i'd like to achieve. (essentially script creates class, hands it to the engine to store, and then pulls it out as it needs it. but the engine has zero idea what the class looks like.) I'm fairly certain the generic handler can achieve this, i just don't know how to register my engine to support this setup.

edit:

I've been working on this a bit more, and think i almost have it. I switched my c++ script to hold a pointer to the CScriptHandle object, like so:


class Obj{
private:
CScriptHandle *m_Handle;
public
void SetScriptHandle(CScriptHandle *Handle){
m_Handle = Handle;
}

CScriptHandle *GetScriptHandle(void){
return m_Handle;
}
};

//Registered like so:
ScriptEngine->RegisterObjectMethod("Obj", "void SetScriptHandle(ref &in)", asMETHOD(Obj, SetScriptHandle), asCALL_THISCALL);
ScriptEngine->RegisterObjectMethod("Obj", "ref &GetScriptHandle()", asMETHOD(Obj, GetScriptHandle), asCALL_THISCALL);


I then use it like so:

class Data{
int x;
};

void Func(Obj @A){
Data @x = Data();
x.x = 0;
A.SetScriptHandle(x);
A.GetScriptHandle();
};

the above code sets the script handle into the function correctly, and has an appropiate ref object inside the CScriptHandle class. but when i call GetScriptHandle, the CScriptHandle class suddenly no longer has it's ref object. i'm assuming angelscript is releasing x from the ref object, but i have no idea how to keep it in the engine. Edited by slicer4ever
0

##### Share on other sites

The CScriptHandle is meant to be used as a value type, so you should preferably store the CScriptHandle member by value rather than by pointer. Like this:

class Obj{
private:
CScriptHandle m_Handle;
public
void SetScriptHandle(CScriptHandle &Handle){
m_Handle = Handle;
}

CScriptHandle &GetScriptHandle(void){
return m_Handle;
}
};

//Registered like so:
ScriptEngine->RegisterObjectMethod("Obj", "void SetScriptHandle(const ref &in)", asMETHOD(Obj, SetScriptHandle), asCALL_THISCALL);
ScriptEngine->RegisterObjectMethod("Obj", "ref &GetScriptHandle()", asMETHOD(Obj, GetScriptHandle), asCALL_THISCALL);


In fact, you can expose the m_handle member directly as an object property, without the need for the Setter and Getter methods if you'd like:

ScriptEngine->RegistetObjectProperty("Obj", "ref @m_handle", asOFFSET(Obj, m_handle));


That would allow the script to manipulate it directly:

// AngelScript
class Data{
int x;
};

void Func(Obj @A){
Data @x = Data();
x.x = 0;
@A.m_handle = x;
@x = cast<Data>(A.m_handle);
};


Regards,

Andreas

1

##### Share on other sites
woot, thanks a ton Andreas, that's exactly what i need=-)
0

## Create an account

Register a new account