BaseContainer bc = NULL;



  • Previously (pre R20) I certain occasions, I started my public BaseContainers as NULL's, inside my plugin class, because I needed to start with empty containers.
    So, later, only if the BaseContainer was not NULL (if I could initialize it inside one of the methods), I would perform operations on them.
    But now, I can't seem to be able to declare BaseContainers as NULLs (or nullptr).
    I know I could simply declare them and then only initialise them inside the Init method.
    But, how can I check if a BaseContainer is empty?



  • Hi,

    actually I'm not aware of any change of the Cinema 4D R20 API, which forbids definition of a BaseContainer pointer and assigning nullptr to it.
    On the other hand the code you posted into the headline of this subject (I recommend to write code rather into the actual post, where can also use code tags), doesn't look like you are actually defining a pointer variable.

    BaseContainer bc = nullptr; // This line shouldn't compile
    

    To be honest I'm not sure why this would have compiled in the R19 or any previous SDK.
    It would rather need to look like so:

    BaseContainer *bc = nullptr;
    

    And maybe it's also not the safest approach at all, as it leads to unrecoverable errors (aka crashes) if you forget to implement the nullptr check somewhere. Instead you could unconditionally create the BaseContainer upon initialization and detect the validity of its content by writing a boolean value representing the state into the container itself. Just a suggestion.

    The BaseContainer API does not directly provide any means to detect if the container is empty. But you could for example use BrowseContainer to check, if there's content. See also the BaseContainer manual for a snippet on how to use it.

    And finally as you seem to be porting your code to R20 anyway, you may also want to take a look at the DataDictionaryInterface (and the DataDictionary manual). While not being a 1:1 replacement, it can be a way more powerful solution for most use-cases of a BaseContainer.

    Cheers,
    Andreas



  • Thank you Andres.
    I'm going with the boolean variable to flag if a BaseContainer contains something or not.


Log in to reply