🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

AngelScript 2.5.0c released

Started by
44 comments, last by WitchLord 18 years, 4 months ago

meaning option 1 is the only better way?

so to be safe, all classes should have the AddRef, DecRef ?
Advertisement
got wierd result after first run, seems like the original data get deleted.

the original data stays after i changed the C++ code to:
void DecRef() {--refCount;}

there is only 1 function EntityGetData at the moment, so i do not know why the refCount is decreased.

anything wrong ?



//--------------------- C++ ------------------------------------
class Entity_Data
{
public:
long Lifespan;
long LightNumber;
int refCount;
Entity_Data() {refCount = 1;}
void AddRef() {refCount++;}
void DecRef() {if( --refCount == 0 ) delete this;}

Entity_Data &operator=(const Entity_Data &other)
{ //... copy everything except the refCount ...
Lifespan = other.Lifespan;
LightNumber = other.LightNumber;
return *this;
}

Entity_Data *Vars[5];

Vars = new Entity_Data();
Vars->Lifespan = i;
Vars->LightNumber = 1111 * i;
...
...


r = engine->RegisterGlobalFunction("int EntityGetData (int id, Entity_Data@ &out)", asFUNCTION(EntityGetData), asCALL_CDECL);



//--------------------- C++ ------------------------------------
long EntityGetData (long id, Entity_Data **entity)
{
*entity = Vars[id];
return (1);
}


//--------------------- angelscript------------------------------
Entity_Data@ vars;

int r = EntityGetData (id, @vars);

[Edited by - iram on February 5, 2006 2:27:36 AM]
Ok. You're almost there. :)

The problem is in the C++ function EntityGetData(). Since the application will keep a reference, and pass a reference to the script engine, you'll have to change your function to call AddRef() on the object before returning from the function. This was why the object got deleted, AngelScript called DecRef() on the reference it had.

long EntityGetData (long id, Entity_Data **entity){  *entity = Vars[id];  Vars[id]->AddRef(); // Count the reference passed to the script engine  return (1);}




AngelScript works best with objects that are properly registered with the AddRef and Release behaviours. This allows for proper memory management, giving a secure environment.

After seeing a little more of your implementation, you could probably get away with registering dummy functions for AddRef and Release that don't do anything. That would allow you to pass the Entity_Data around by handle, but without the need for changing the actual C++ structure to count references. The drawback of that would be that if you're not careful, the scripts might have stored a pointer to the object somewhere that it will use later on, even though the application has deleted the original object. Though, now that you have implemented the reference counter I don't see the reason to remove it again. :)



AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game


thanks for the enlightenment.

but i am still quite confused with & and @ in the script.
are they supposed to be the "same" ??

for example, i tried both and it works the same.
void Run (string @text)
void Run (string ∈ text)

so it is possible for other classes ?

how to handle int& ?

They are not the same, just as & and * are not the same in C++, even though they are very similar.

@ passes the object by pointer to the function, increasing the reference counter first. The pointer may be NULL, just as for C++ pointers.

∈ also passes a pointer to the value, although to guarantee correctness, the pointer is actually to a copy of the original value. Any changes to the value in the function, is not reflected on the original value.

&out passes a pointer to an object, meant for output values. After the function returns the value is copied to the original value.

&inout passes a pointer to the true object. But to guarantee safety this is only supported for objects that support object handles.

Parameter references for primitive types and object types that do not support object handles are only allowed as either ∈, or &out. Both are compatible with C++'s normal parameter references, except in the case where the function wants an input value, and alters it.

If you really need AngelScript to handle parameter references like C++ do, then you may compile the library with the flag AS_ALLOW_UNSAFE_REFERENCES. When that is done, all types may be passed by reference by declaring the parameter with & (without any extra keyword). However, just as the flag might suggest, this may not always be secure, just as it is not always secure in C++.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game


what about getting "Invalid configuration" error during Build ?

i noticed some classes have this problem.
If you get that, one of the registration functions failed. Verify the return value to see what it is.

You can also set the message stream before calling any of the registration functions, in which case the engine will write out the errors.

I recommend that you validate the return value with an assert after each call. This will catch the errors in debug mode and doesn't impact the neither code readability, nor performance in release mode.

Example:

r = RegisterGlobalFunction(...); assert( r >= 0 );

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game


one of the new class is returning a -5 during registration.
it is a code for "invalid" but my class is usable.

what could be the possible issue?
That is the error code for asINVALID_ARG (see angelscript.h). One of the arguments to the register function is not allowed.

You need to show me the line that you're calling for me to tell you what's wrong.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

actually it is -1 after i changed some of the codes.
still it will fail for some classes, not sure why.

a)
r = PR.Script.ScriptEngine->RegisterObjectType("srGraphics", sizeof(Graphics), asOBJ_CLASS);

b)
r = PR.Script.ScriptEngine->RegisterObjectType("srGraphics", sizeof(Entity), asOBJ_CLASS);

if i register with a), i get -1.
so if changing to class to an okay one, no errors given.

wondering what could have affected this? some restrictions on the classes?
btw, i am using a third party library so i cannot do anything about it. only to get support from the owner if i can know the problems.

This topic is closed to new replies.

Advertisement