View Full Version : Containers: What's their official proper usage?
Scott Ayers 08-08-2010, 09:20 PM Hey guys,
Up until now, I've only been using containers when I absolutely have to. Because I've been trying to keep my scripting learning path as generic as possible and not too C4D specific.
But now I'd like to learn more about them.
The SDK says things like "you should always store values in containers". But when I look through other people's code. I don't see people using containers very often for common tasks like moving or rotating objects. The only time I see containers being used are when dealing with the SendModeling() functions. Or most often...when using the frames option for the time line.
Since we can create empty base containers and put things inside of them like object positions and rotations. I'm wondering if we're all breaking the rules a bit by not using them for things like this?
So I'm a little bit confused about when, and why, we should use containers.
-ScottA
|
|
Darter
08-09-2010, 11:56 PM
The SDK says things like "you should always store values in containers".
I can't see this anywhere in the SDK. The overhead involved in storing and retrieving values when using containers means that it's faster to create variables for storing your values. Use global variables for values that need to be retained between calls.
Per-Anders
08-10-2010, 12:21 AM
Actually containers are not particularly slow unless you're dealing with many thousands of value get/sets or are in a particularly speed critical section of code.
A container is a construct that's the equivalent in many ways to a dictionary in languages other than C++ and COFFEE. It can contain any datatype that C4D supports, so you can extend that range by building new customdatatype plugins if required. Each entry comprises of a key (a unique integer ID) and the data stored as a GeData value. Additionally you can access data by index and treat it as a list style object appending data with non-unique id's, however this is generally only of use when you're constructing Menus, which use BaseContainers to represent their contents as it makes it impossible to key-search the container.
All BaseList2D objects and their derivatives have a BaseContainer which stores their settings (interface values and so on), this container is copied when the object is copied, for example during undo operations, it's saved with the object and loaded with the object. If you need any data to be persistent on a node you can use it's container, though make sure it has a unique id if you don't own the object as you might accidentally be writing over a value the object itself is using.
BaseContainers are also used to transfer data around between functions where a rigid structure may not be suitable, for instance SendModelingCommand, most Input Event and GUI generic calls.
You don't have to use containers for any of these things, however they are very useful and convenient and you shouldn't really be scared of using them, they're quite simple and easy.
Scott Ayers
08-10-2010, 02:19 PM
You don't have to use containers for any of these things, however they are very useful and convenient and you shouldn't really be scared of using them, they're quite simple and easy.
So suppose I just wanted to do something very simple like move an object from one position to another. Can I use a container to do that?
And I guess I should also ask...Should I be using a container to do that kind of thing in C4D. Rather than using the plain old standard method of using global variables and functions?
-ScottA
Cairyn
08-10-2010, 07:58 PM
So suppose I just wanted to do something very simple like move an object from one position to another. Can I use a container to do that?
And I guess I should also ask...Should I be using a container to do that kind of thing in C4D. Rather than using the plain old standard method of using global variables and functions?
No, you shouldn't. Putting things into a container class for no reason at all makes no sense.
You use a container when you want to store/load something, or when you want to pass an arbitrary number of values to a method, or when you need to pack several values into a... well, container. Everything else is plain and simple variables and parameters.
Scott Ayers
08-10-2010, 10:15 PM
Ok. I think I understand.
I should think of containers as C4D's preferred method for storing menu and tool attributes.
And although you do also "store" values in variables in normal scripting situations that can effect an object in many other ways(position,scale, etc.). The only reason to choose to use a container is if you want to set up some sort of user interface where the user would select from a set list of options.
Or if you need to access the already built-in containers that can already control certain parts of the UI like the base container class can access the time line and many of the tools.
-ScottA
CGTalk Moderation
08-10-2010, 10:15 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.