Hi guys ;)
I wonder if any coders out there could help me finally understand stateful nodes.
I don’t really know much about code, but I love to explore and create nodes to learn to code.
I guess I understand the main basic concepts, but I’m definitely stuck.
I’ve been watching YouTube videos about pointers, pointer to pointers and reading some articles about it, but no matter how long I look at the examples and the codes of the stateful stock nodes, I’m having a hard time.
I know what a variable is, I know a pointer stores the address of a variable.
That you can declare and initialize a pointer using datatype1 *myPointer
.
That one can access (dereference) the value a pointer points to using *myPointer
or **myPointer2
to access the value of a pointer to pointer.
But still
If someone could help me comment and change my comments and questions on this simplified Count
stock node code, and understand the phases of it and how data travels where to do what, that would be much appreciated !
I think I get the main idea of memory being freed, and then setting values at those memory locations, but the exact steps and the syntax being used and why I fail.
Thanks ;)
/* @copyright Copyright © 2012–2022 Kosada Incorporated. This code may be modified and distributed under the terms of the MIT License. For more information, see https://vuo.org/license.*/
VuoGenericType1 * nodeInstanceInit // Are we declaring a pointer named "nodeInstanceInit" of type VuoGenericType1 ?
(
VuoInputData(VuoGenericType1) setCount // Declare a variable named setCount ?
)
{
VuoGenericType1 *countState = (VuoGenericType1 *) malloc(sizeof(VuoGenericType1)); // Declare and initialize a pointer named counState to what ? To the memory addresses of setCount ?
VuoRegister(countState, free); // Free and allocate memory for the pointer counState ?
*countState = setCount; // On node launch, the value the pointer points to is set to the value of "setCount" ?
return countState; // Return the pointer
}
void nodeInstanceEvent
(
VuoInstanceData(VuoGenericType1 *) countState, // Get/pass the data of countState ? And what is (VuoGenericType1 *) ? I thought nodeInstanceInit was the pointer ?
VuoInputData(VuoGenericType1, {"defaults":{"VuoInteger":1, "VuoReal":1.0}}) increment, // Ok I get this, we declare a Vuo Port
VuoInputEvent({"eventBlocking":"none","data":"increment"}) incrementEvent, // Here we declare event blocking, ok
VuoInputData(VuoGenericType1, {"defaults":{"VuoInteger":0, "VuoReal":0.0}}) setCount, // Another port, ok
VuoInputEvent({"eventBlocking":"none","data":"setCount"}) setCountEvent, // Ok
VuoOutputData(VuoGenericType1) count // Ok, this is the ouput port of the node
)
{
if (incrementEvent) // Ok, each time the node receives an event do ...
**countState += increment; // ? If *countState is the value at the address of setCount, what is **countState ? We set 2** because it's a pointer inside of "VuoGenericType1 *" ? Or because single * are fort output ports ?
if (setCountEvent) // when this receives en event, do ...
**countState = setCount; // ___________ ? will become the value inserted at the setCount input port
*count = **countState; // The output port "count" (is it somehow a pointer too ?) becomes ___________ ?
}