[Closed] Weird struct + callback behaviour
heh, It’s funny – i had written a reply to you earlier with the brackets taken out and before i tested it, i couldn’t get the callback to unregister so I didn’t post! duh!
Ok seems like i’m wrong afterall…
man…
newStruct = theStruct()
is the right way to instanciate a struct
and calling the function without the struct name also works… very confusing all of this, It has to be a scope issue… I don’t understand how the callback knows wich function to get because i’m not feeding the struct… is the struct automaticly “assigned” to the callback because I created the callback from the struct!?
ahwell it’s weekend…
-Johan
Hi Johan,
Since i’ve been using structs for a while i’ve come to the conclusion i’ve been using them badly. From the help it does indeed say the same. without the brackets, the data declared in the struct is not initialised. Suffice to say, in the past I’ve just used them as a neat container for all my function calls. But i’ve definately has issues with scope of the variables contained within them. I’m currently writing something that i want to use a struct to store an array to a struct member, and I’ve been getting errors. Until you posted this i was unsure why.
I haven’t been able to get your code working, even outside the structure def and once registered, i cant get it to unregister.
the brackets appear to initialize any variable within to the struct instance value – hence
struct doinStuff (
foo = 2
)
DoIt = doinStuff
DoIt.foo
will throw an error, and
struct doinStuff (
foo = 2
)
DoIt = doinStuff()
DoIt.foo
will yield 2
I feel kind of foolish since i had been doing this differently, and it is the brackets that make the scope of the struct ‘global’.
edit – just found this in another post –
” if your struct contains local variables, you cannot access them unless you
create an instance of the struct.
If the struct contains only functions delimited by commas, you can directly
[b]call these functions without creating an instance.
[/b]anyone want to chime in as i fumble my way through the help please feel welcome.
Ok, your code Johan works fine – my bad. Im still not exactly sure what it is doing when I run the whole thing with the toggle function changed to false, but just executing the function on its own works fine.
Thanks to re-reading a few other posts too I have got it clear about handling data as well as functions within the structure. Previously, i was using an separate file on an include with the struct instance which i guess is okay but didn’t know how best to define it within one script. I was previously getting an error the first time I ran the script, then it was fine after that. I was declaring globals for things i didnt need to because i thought my struct function variables were causing the problems. I realise now what i did wrong, and I can just have the one global. Much cleaner! As my scripts were getting longer i was concerned that i would have problems in the future.
sorry if my ramblings have taken this off topic to what you previously asked.
Oh no, It’s cool
It’s best for everybody if anybody can post their troubles and solutions to related threads, that’s how i’ve learned much!
Cheers,
-Johan