# Variable Paremeter with value type bug

## Recommended Posts

Perhaps there is something I am missing, but I cannot get variable parameters with out reference to work. I have stripped out the function to the bare minimum to reproduce. The following causes a call to NxVec3 operator= with null pointers: Script: NxVec3 value; varParam(value); NxVec3 is registered as : RegisterObjectType("NxVec3", sizeof(NxVec3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CA); varParam is registered as: RegisterGlobalFunction("bool varParam(?&out)", asFUNCTION(varParam), asCALL_CDECL); varParam is implemented as: bool varParam(void *ref, int typeId) { (*(NxVec3*)ref) = NxVec3(20.0f,10.0f,5.0f); return true; } Stepping through the debugger, when it reaches varParam, a call to NxVec3 operator= with the value being assigned to the cast ref pointer. After the function leaves, I can't quite follow what it is doing on the ascript side, until shortly after another call is made to the NxVec3 operator = method, except this time with invalid pointers. The operator registration and implementation is provided below: RegisterObjectBehaviour("NxVec3", asBEHAVE_ASSIGNMENT, "NxVec3 &f(const NxVec3 &in)", asMETHODPR(NxVec3, operator=, (const NxVec3&),const NxVec3&), asCALL_THISCALL); NX_INLINE const NxVec3& NxVec3::operator=(const NxVec3& v) { x = v.x; y = v.y; z = v.z; return *this; } Also note, I have tried this with primitive types and it worked fine, but have not tried with any handles as of yet. Does this even support handles? Thanks for your time.

##### Share on other sites
I believe you're actually getting a pointer to a pointer when it is a registered type, so you code should be written as:

bool varParam(void *ref, int typeId){  (**(NxVec3**)ref) = NxVec3(20.0f,10.0f,5.0f);  return true;}

This would be because of the way objects are passed internally in AngelScript, but I realize that it would be better for the application if it was just a pointer to the object so I'll look into changing this in a future release.

##### Share on other sites
That code change worked. Thank you!
Though the syntax does seem rather silly :P

Also, why does asIScriptModule have GetObjectTypeByIndex() but no GetObjectTypeById()?
If using asIScriptEngine->GetObjectTypeById(), does this include object types defined in that particular modules in script? Also, if this function returns null when using a typeId passed from a variable parameter, is it safe to assume the passed variable must be a primative?

On a semi unrelated note, I was comparing library sizes from my previous version 2.15.1 to 2.16.2 built with the settings provided in the included msvc2009 projects

2.15.1 release = 749kb
2.15.1 debug = 2632kb
2.16.2 release = 6969kb
2.16.2 debug = 2777kb

I assume alot of inlining is going on but thats almost a 10 fold increase in file size for release mode! What gives?

##### Share on other sites
GetTypeIdByIndex() is used to enumerate the types that were declared in the script, classes, interfaces, enums, etc.

The GetObjectTypeById() in the engine will return the object type for types declared in script modules too.

No, if GetObjectTypeById() returns null it can mean, either that it is primitive, or that it is an invalid type id.

To determine if the type id is for a primitive type you should check the bits in asTYPEID_MASK_OBJECT, if none of them are set, then the type id is for a primitive type.

You may want to check the CScriptAny add-on for examples on how the variable argument works.

About the binary size. I'm not sure, but I think it is because I turned on full optimization for speed in order to reproduce and fix some bugs that only appeared like that. Try turning down the optimization again, and you'll probably get a smaller binary size.

##### Share on other sites
Wow, you are awesome!
I did not know about AddRefScriptObject, or CopyScriptObject. I assumed the only way to deal with that was to have a giant switch for all possible registered data types to cast them proper. This is most excellent.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627744
• Total Posts
2978901

• 10
• 10
• 21
• 14
• 14