Advertisement

Issue with Add-on "Any": Retrieve of float fails

Started by January 08, 2017 12:35 AM
1 comment, last by calzone 7 years, 10 months ago

I'm working with the CScriptAny class and have run into behavior I can't understand.

Two problems:

  • When a float is stored in an any, any.GetTypeId() returns a different result than GetTypeIdByDecl(). 11 (double) and 10 (float) respectively.
  • When the correct type (10 for float) is used in any.Retrieve, an assert fails. When the incorrect type is used (11 for double) the result is garbage

My goal is to (in script) store a float into an any object, then pass it to c++ via a function call.

What am I missing here? Code below.

Script Code:


void main()
{        
    float f1, f2;    
    any a1;
    
    f1 = 11.11;
    f2 = 22.22;
    
    a1.store(f1);
    takeany(a1);         
}

Registration of takeany in c++:


RegisterGlobalFunction("void takeany(any &)", asFUNCTION(takeany), asCALL_CDECL);

function takeany() in c++: (please forgive my use of printf, i find it easier to use when looking at pointers)


void takeany(CScriptAny &a)
{
    float value;
    int type_id;
    void *val_ptr = operator new(4); //4 bytes for float
    
    asIScriptContext *ctx = asGetActiveContext();
    asIScriptEngine* engine = ctx->GetEngine();
    
    cout<<"in takeany\n";
    printf("value of val_ptr: %i \n", *static_cast<int*>(val_ptr));
    
    type_id = a.GetTypeId();
    cout<<"typeid is: "<<type_id<<endl;
    type_id = engine->GetTypeIdByDecl("float");
    cout<<"typeid is: "<<type_id<<endl;
    
    //method 1
    a.Retrieve(val_ptr, type_id);
    printf("value at val_ptr: %f \n", *static_cast<float*>(val_ptr));
    
    //method 2
    a.Retrieve(static_cast<void*>(&value),type_id);
    printf("value of value: %f \n", value);
        
    value = *static_cast<float*>(val_ptr);
    
    cout<<value<<endl;
}

This produces the following on the terminal:

in takeany
value of val_ptr: 438831032
typeid is: 11
typeid is: 10
better_print_2: ../../sdk/add_on/scriptany/scriptany.cpp:329: bool CScriptAny::Retrieve(void*, int) const: Assertion `refTypeId > asTYPEID_DOUBLE || refTypeId == asTYPEID_BOOL || refTypeId == asTYPEID_INT64 || refTypeId == asTYPEID_DOUBLE' failed.
Aborted

Note that type 10 corresponds to a float, type 11 corresponds to a double.

The CScriptAny class is designed to always store floats and doubles as double, and integers as int64.

When you retrieve the value using the correct type id of 11, you're getting a double, however in your code you're treating it as a float, which is the reason you see garbage.

To retrieve the double value do the following:

void takeany(CScriptAny &a)
{
    double value = 0;
 
    cout<<"in takeany\n";
    
    if( a.GetTypeId() == asTYPEID_DOUBLE )
    {
        a.Retrieve(value);  
        printf("value: %f \n", value);
   }
}

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

Advertisement

That was it. Thanks for your help.

This topic is closed to new replies.

Advertisement