Object Instances
Goog
Posts: 65
I've been looking for information on how objects are shared between other objects - is this possible?
For example:
Object1 creates ObjectA.
Object1 creates ObjectB.
ObjectB would like to use the same instance of ObjectA created in Object1, without creating a new object and thus using more memory.
I just can't find how, but I think it might be possible with pointers?
A good example would be where the parent object uses the graphics object, but a child would also like to use the same graphics object.
Thanks,
Goog
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Doing my part to keep the Commodore 64 alive!
http://www.commodorestuff.com
·
For example:
Object1 creates ObjectA.
Object1 creates ObjectB.
ObjectB would like to use the same instance of ObjectA created in Object1, without creating a new object and thus using more memory.
I just can't find how, but I think it might be possible with pointers?
A good example would be where the parent object uses the graphics object, but a child would also like to use the same graphics object.
Thanks,
Goog
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Doing my part to keep the Commodore 64 alive!
http://www.commodorestuff.com
·
Comments
·· An object can launch a part of itself into other COGs using the COGNEW command.· If you have a Method called "BLINK" in your object, you can launch multiple instances of it by doing something like this:
Of course you have to have defined stack space for it.· You can call this as many times as there are cogs available, but each will need stack space for itself.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Think of this possibility: My parent object does all of the graphics display, but the child object wants to do some drawing as well.· Without sharing the graphics object, the compiler allocates enough memory for the parent and the child's graphics objects, basically doubling the amount of space used.· What I would like to do is something like this:
Any thoughts?
Thanks,
Goog
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Doing my part to keep the Commodore 64 alive!
http://www.commodorestuff.com
·
I.e cog0 and cog4 both "run" the same code. It is fine as long as both dont try to do stuff at the same time, and you have to pay attention to the multi-cog pin control rules. Basically always release the pins to Input mode in the objects.
Have a look at the i2cObject in the library.
James
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Doing my part to keep the Commodore 64 alive!
http://www.commodorestuff.com
·
·· I still don't think I know exactly what you mean, but when you launch multiple copies of the same object, they don't use more memory for each instance...The only extra space used is the Stack and Variable space.· The actual Object only exists once in main memory for SPIN objects.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Doing my part to keep the Commodore 64 alive!
http://www.commodorestuff.com
·
Basically, in the end, ChildB should display the same LEDs as the parent did, but instead they are all off (because ChildB reads the value and gets a return of zero).· You'll understand when you see/run it.·
So this is what I'm trying to accomplish - using a child object that references another object created by its parent.
Goog
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Doing my part to keep the Commodore 64 alive!
http://www.commodorestuff.com
VARs are duplicated for each instance of the object. If you want instances to share the same data, put it in a DAT section, like this:
-Phil
So basically, the VAR section has "private" variables that are only accessible by the parent object? And the DAT section has data/variables that are global to the object - the values stay the same until changed by any object anywhere in code? (it seems so, but I would like solid confirmation)
Goog
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Doing my part to keep the Commodore 64 alive!
http://www.commodorestuff.com
·
Close, but not quite. The VAR section consists of variables that are duplicated for, and unique to, each "instance" of the object. (I use the word "instance" loosely, since the object itself is not duplicated — only its environment data are, including its VARs.) VARs are not shared with other instances of the same object or with any other object, including the parent. Variables in the DAT section, like the object they're contained in, have only a single copy in hub RAM. You can think of them as part of the object's code (that doesn't get duplicated), if you like. These data are therefore shared by all "instances" of the object, since they're part of the object itself.
-Phil
Hopefully this will be explained more in detail in the final version of the manual - I only found limited information regarding object instances, which is why I started this whole topic.
Thanks!
Goog
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Doing my part to keep the Commodore 64 alive!
http://www.commodorestuff.com
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Who says you have to have knowledge to use it?
I've killed a fly with my bare mind.
-Phil