Pass a STRUCTURE into a module?
dthorson
Posts: 103
Anyone know if this is possible?
I'd like to pass a DEFINE_TYPE STRUCTURE[] into a Module that I've completed.
I'd like to pass a DEFINE_TYPE STRUCTURE[] into a Module that I've completed.
0
Comments
I'm off down another coding path.
you do
DEFINE VAR
_SatRadio SatRadio[5]; (I now have five arrays in the structure)
That way I can change the layout/order and graphics on the fly in code.
I was hoping to pass the structure array to the module that runs the TP, but passing the values is working well too.
Don't you mean you have an array of structures with 5 elements?
Paul
then in your module you have to decode it using STRING_TO_VARIABLE passing the info into your structure.
I have something similar to this working fine.
But as you can see its a hassle to implement but does allow you the option of passing a heap of data to a module on the fly.
credits to Jeff Coffler and his SlimServerMod program which is where I found this technique.
I think of structures like a java/class (I know they aren't), once you build one with all your arrays inside, you can duplicate them. When you change the main structure, it changes all of the copies you made.
I've found the best way to get around modules and structures is to save all data to an XML file. The advantage to this is, if the master ever goes down you just upload the XML files and all the user data is back.
My resi program lets the user store/change all event timers which include lights/thermo/sprinklers/shades/music events/etc - yes, I have these XML files emailed to me monthly just to have a recovery point.
the best way to use a structure in a module, is to create the type on the main program, and on the module. Then you just have to passe every array or variable into your module declaration. It's working fine, and you can use structure on you module...
Will that work? I would think that every time a variable in the structure changes you would need to manually update the other structure by doing the variable to string thing. Is this not the case? If it is that adds a bunch of extra processing that needs to be kept track of in case it gets out of sync.
Paul
I think the structure has to be defined identically in the main program and the module for the to work ... which, of course, eliminates the ability to change the structure on the fly.
I prefer to send components of the structure as module parameters, then populate them in the module DEFINE_START. It's not ideal, but seems to me to fit the bill best in terms of readability and ease of use.
That's what I thought. Any changes on the fly will not be reflected in the other structures until you update them. I figure all the structure does in encapsulate a few variables anyway, so why not just use arrays? Writing variables to a file sounds like a good idea for some infrequently changing data, but I would think the constant I/O would slow things down if the data changed frequently like volume. I think I will just stick to arrays for now until they allow passing a structure to a module.
Paul
You can also update the information by passing it into the module. Outside the module just keep the passed in char array up to date using VARIABLE_TO_STRING. Inside the module just do a STRING_TO_VARIABLE before you use and trust the data.
MODULE_NAME='Structure Test" (CHAR cEncodedData[])
There is absolutly no reason why STRING_TO_VARIABLE/VARIABLE_TO_STRING is not the solution to the problem... It works great, end of soap box....
Actually, there is one very big reason why it doesn't always provide a good solution: you still must define the structure identically in both the calling program and the module being called. The original poster refers to exactly that; he wanted the ability to change the structure members in the main code and propagate it to the module; if your module is pre-compiled, you cannot in any way, shape or form do this ... both the calling program and the module must be recompiled.
Now if the marshaling protocols passed the metadata necessary to actually create the structure and define it in the module, it would work, but they do not.
Even though that is pain, it is still better then passing all that information into the module as seperate arrays parameters. Small price to be pay for the correct solution.
Sure it will. As AMXJeff stated earlier:
It works perfectly! Here?s an example where the main program or the module can alter the data and they will both stay in sync. I chose to pass the encoded string in as a parameter to the module instead of the SEND_COMMAND method that filpee demonstrated. I believe SEND_COMMAND is limited to about 128 bytes (and SEND_STRING is somewhere around 2000 bytes) so if you want to pass heaps of data back and forth at one time I don?t think the SEND_COMMAND route will suffice. If I?m wrong someone please correct me. Anyway here?s the code:
After a Button 1 Push:
After a Button 2 Push:
After a Button 3 Push
After a Button 1 Push:
After a Button 2 Push:
After a Button 4 Push
After a Button 1 Push:
After a Button 2 Push:
Thanks for your insight filpee and AMXJeff! Your posts opened new doors for me. It?s a beautiful thing!
If anyone wants to try the code without cutting and pasting, I attached the project file.
FYI: Send_Commands to Virtual Devices is at 2000 bytes as well. But I always pass the Encoded Array into the module, it is just cleaner. Good Job!!!!!!!!!!!!!
The reason your structure is staying in sync is because you are continually syncing them in the code after any modification. That's cheating!
What myself and I think others were hoping for is a method where there is no manual synchronization required. If you could pass a structure to a module, so that the main code and module code had the same copy of the structure, simply assigning a value to a structures members would keep them in sync because there is only one copy. In your code you have two copies and are continually syncing them after any modification. No doubt this works, but I would rather not have to try and sync everything up like that. What if you had 100 structures? Then it starts to become rather tedious to keep them synchronized manually.
In most other programming languages like C for instance, you can pass a pointer to a structure to a function so that both the calling code and client code are looking at the same memory and no synchronization is required. I think that is the functionality that Netlinx is sorely missing.
Paul
A parameter passed by reference is a pointer essentially. All variables in Netlinx are passed by reference, with the odd exception of passing structures to modules, which you just can't do no way no how.
Paul
So how does that relate to what you posted above about modules not having that capability because they are pre-compiled?
Paul
Sorry to be dense but I don't understand how both these things can be true. If you can pass a DEV structure to a module then you can pass a structure to a module. Netlinx just doesn't allow user defined structures to be passed to modules. This would seem to indicate that it isn't impossible to pass user defined structures due to the precompilation of modules, it just hasn't been implemented.
Paul
DEV, DEVCHAN and DEVLEV are not user defined Structures, they are Native Structures to the NetLinx language. The Module knows about them Long before Line 1 of the NetLinx Module code.