Skip to content
On this page

Custom User Data

All API objects can have custom user data associated. Data is indexed by a name string, associating new data with an existing name will overwrite the old data. The user data is copied and persisted in the API objects through serialization and distribution. When querying user data from an object the returned data is a new copy of the data buffer to maintain integrity of the API object store.

Here is an example:

cpp
void UserDataExample()
{
    // The data
    uint32_t buffer[64];
    // Populate data ...
    // buffer[] = ...

    // Create an object and set custom user data
    spSceneNode node = sg->CreateSceneNode();
    node->SetUserData( "myData", buffer, sizeof( buffer ) );

    // ...

    // Access the user data
    spUnsignedCharArray myDataBuffer = node->GetUserData( "myData" );

    // Size, will be sizeof(buffer)
    unsigned int myDataSize = myDataBuffer.GetItemCount();

    // Raw data
    unsigned char* myData = myDataBuffer.Data();
}
csharp
void UserDataExample()
{
    // Create an object and set custom user data
    using (spSceneNode node = sg.CreateSceneNode())
    {
        node.SetUserData( "myData", "some data", 9 );

        // ...

        // Access the user data
        spUnsignedCharArray myDataBuffer = node.GetUserData( "myData" );
    }
}
python
def user_data_example(sg):
    # Create an object and set custom user data
    node = sg.CreateSceneNode()
    node.SetUserData( "myData", "some data", 9 )
    
    # ...
    
    # Access the user data
    myDataBuffer = node.GetUserData( "myData" )