REBUILD_EVENTS()
dtucker
Posts: 7
I am having some difficulty with REBUILD_EVENTS(). I want to have a CHANNEL_EVENT that operates on an array of devchan elements, like so:
And I would like to make changes to that array at runtime:
Does anybody see what I might be doing wrong? Is it clear what I am trying to accomplish?
DEFINE_VARIABLE devchan devchanElements[10]; integer count; DEFINE_EVENTS CHANNEL_EVENT [devchanElements] { ON: { send_string 0, "'Channel ',itoa(channel.channel),' was turned on.'" } }
And I would like to make changes to that array at runtime:
DEFINE_FUNCTION AddDevChan(dev d, integer c) { count = count + 1; devchanElements[count].DEVICE = d; devchanElements[count].CHANNEL = c; REBUILD_EVENT(); }I would expect that when I call the AddDevChan that channel events that occur on that device and channel will be raised. But that is not happening. It seems my REBUILD_EVENT() method is not updating the event as needed.
Does anybody see what I might be doing wrong? Is it clear what I am trying to accomplish?
0
Comments
Try changing this:
To this:
And you should be good to go.
HTH
Consider, that function REBUILD_EVENT in each module operates independently... And in occasion of a code I would write so:
1) As a typing shortcut for large button arrays as shown in example 1.
2) As a way to pass one button array into a module and then split the array into multiple button arrays as shown in example 2. I could just as easily pass in multiple button arrays to begin with and many times I do but sometimes I think the one array method is cleaner.
I?m sure there are much better uses for REBUILD_EVENT() and I wish I knew what they are because I feel like I?m missing out on something. REBUID_EVENT() users please chime in.
Example 1: Typing shortcut.
Instead of doing something like this:
I use REBUILD_EVENT() like this:
Example 2: Split button array passed into a module.
Include file:
Module file with REBUILD_EVENT():
Joe this example is virtually identical to the example in the NS2 help files but I still don't get why you need: and what this does.
When the button array is initialize during compiling the array is filled w/ zeros and in define_start you call the function fnInitLights () which loads your values so what does the set_lenght_array and rebuild_event actually do for you. I am so not getting this!
Let?s start backwards and begin with REBUILD_EVENT(). First off, the event table is built at compile time. Let?s say we have the following code:
When buttons 1, 2, or 3 are pushed for dvTP1, dvTP2, or dvTP3, the BUTTON_EVENT fires. Fair enough.
Now let?s say during runtime we want to change the nSomeButtons array to something like this:
nSomeButtons = ?4,5,6?
If we want the BUTTON_EVENT to fire with these new button values then we have to call REBUILD_EVENT() so that the event table is reconstructed. Probably a more practical example is subtracting or adding back into the DEV array and then calling REBUILD_EVENT(). I?m looking forward to hearing from others to learn how and when this rebuilding technique is used.
Regarding SET_LENGTH_ARRAY():
When you declare an array without initializing it,
VOLATILE INTEGER nLightBtns[nMaxLights]
you?re only telling the system that you want to reserve X amount of memory. My understanding of arrays that haven?t been initialized is that the value of each element is undetermined (it will be whatever that chunk of memory happens to be.) I think Netlinx may automatically set each element to 0 for us but the length of that array is 0 and therefore ?doesn?t really exist.?
If you do direct assignment like this:
nLightBtns = ?2,4,6?
then the length is automatically set to 3.
If you then did:
nLightBtns[4] = ?8?
then the value of the 4th element will indeed be 8 but the length will still be 3 because altering individual elements of an array doesn?t alter the length of the array. If we want to assign values to individual elements of an array and have the length altered then we need to do it manually by calling SET_LENGTH_ARRAY()
And that?s the problem that the original poster had. The individual elements were getting set but the length of the array was still at 0 so when REBUILD_EVENT() was called nothing was added to the event table.
Reading over what I wrote about SET_LENGTH_ARRAY() sounds wordy to me and probably didn?t help clear up any confusion. Maybe someone else can put it in more succinct terms.
http://amxforums.com/showthread.php?p=25830#post25830
where;
VOLATILE INTEGER nEPCII_ActiveTPArry[EPCII_NUM_TPs] ;
was declared in the variable section and no initial values were set at in the declaration on in start up. First I think your saying that when the elements are not initialized w/ a value they may have the value of what was in that memory location or be set to zero. This could use some clarification cuz if it's not initially set to zero I have some code that needs changing, from what I've seen it appears to be. Anyway if I go into debug and look at the array I see zeros in each element but I see the entire array initialized w/ zeros. When I set the values of these elements and run for loops to check the values of these elements it all works fine. So to me the array lenght is as declared even though it wasn't initilized w/ values.
So this shouldn't be working for me? And I should be using Rebuild_Event.
REBUILD_EVENT only has something to do with arrays that you use in the DEFINE_EVENT section.
the only reason i could find to do this is the following:
For example, you have a very large project with alot of buttons on a lot of touchpanels. You use a FOR() loop to fill an array (because you are too lazy to type 300+ numbers in Netlinx Studio), and then use REBUILD_EVENT to have that array triggered in DEFINE_EVENT. right?
What I would like to see is:
When building a module - say for thermostats and you set the max number of stats to 8.
You now have your structures/arrays set to 8 which are locked in the module.
I would like to be able to input 3 stats and than REBUILD the structures/arrays to only include 3 stats. This would help cut down on all the request being sent to the stats and maintain a smaller file size.
Be default the debugger shows the total length and not the current length. I?m pretty sure it used to be the other way around in older version of NS2. Anyway, right click on the variable and uncheck ?total length? and you?ll see that your array has 0 length.
I looked at your code and you should be just fine because you?re using the array as a set of flags and you manually peek and poke individual elements. If you have a FOR loop that was based on the LENGTH_ARRAY as shown in the code below for button 1 then you?ll have problems if you don?t set the length someplace.
I guess I do have vague recollections from previous post that declared variables that aren't initialized w/ values may hold the garbage held over from that locatons previous usage so as a precautionary discipline they should be initialized with a value of zero if other values aren't required at start up.
I'll have to play around with this when I get some time and make sense of it all. If I had used length_array in my for loops instead of the constant I would have seen this and its odd cuz that's normally what I do. I almost always initialize w/ values and use length_array in my loops.
Thanks Joe!
The original code:
I needed to add more arrays and since my number of T-Stat will never be 64 I figured this would be a good place to use this command. I've used it in several module but must admit I still don't fully grasp the concept.
So I replaced the above w/:
From what I gather this should be correct since Rebuild_Event will affect all changed variables above the point where it's declares in it's block of code.
For those that know is this application correct?
Ok, I understand what you're saying. This command only applies to vars or var arrays that deal w/ event tables and since none of these arrays are BtnArrys, VTArrys or LVLArrys which are used wtih a DEV to create a button_event, level_event, channel_event or timeline_event its use is not required. Basical just as the the instructions say. Maybe if I read it a few more times it will sink in!
That makes it a little clearer. I guess the whole event table thing and how it works or what it actually is since we can't look at it, is kind of a mystery and a difficult concept to grasp since we rarely deal with it directly. If it wasn't for the few times the limitations of the event tables screwed me or shoulod I say I screwed myself because I wasn't aware of their limitations I wouldn't even know they exist or pay them any mind.
Another question. In this instance should I even bother with the SET_LENGHT_ARRAY since it's declared with the same lenght in DEFINE_VARIABLE but not populated with initial values? This stuff still boggles my mind.
Normally I would always initiate the array with zero's or actual values when defining it so this has never really been an issue for me but in reading recent post I've been trynig new ideas, second guessing myself and trying understand things that were always sort of fuzzy. Actually most things are still fuzzy!
Whenever you do a string operation on an array that changes it, the length is also set. So your LEFT_STRING, MID_STRING, etc., will set the length of the array they change. This is also true of assignments - any time you assign a value to an entire array (not just a single element), the length is set then too. It's possible that any internal NetLinx function that operates on an array as a whole and changes it will set the length, but these are the cases it does I am sure about.