The Dink Network

Variable scope and set_callback_random()

February 14th, 12:22 AM
custom_simon.gif
SimonK
Peasant He/Him Australia
 
Just wondering if a local variable declared in the main() procedure of a script is accessible/read by a custom procedure that is called via the set_callback_random(); function.

It seems to me that it is... but I could be wrong.
February 14th, 10:06 AM
peasantmb.gif
yeoldetoast
Peasant They/Them Australia
Oh, NOW YOU'VE DONE IT! 
Looking at redink1's notes about 1.08 variable scope suggest it should be fine, as no new script is created in the process.
February 14th, 10:46 PM
custom_simon.gif
SimonK
Peasant He/Him Australia
 
Hmmm, I tried that example of code and maybe something has changed between Dink 1.08 and DinkHD, as if I assign that code to a key script, I get Dink saying

&x, &y

then

4, 6

rather than the "4, 8" and then "2, 6" mentioned

========================

This was what Redink1 posted about 1.08 and scope

* Variables are now dynamicly scoped.

It basically allows you to reference local variables defined in previous functions, so you wouldn't have to resort to global variables like &save_x and &save_y (most of the time).

You can do something like this:

void main(void)
{
int &x = 4;
int &y = 6;
bob();
say_stop("&x, &y", 1);
}

void bob()
{
int &y = 8;
function();
}

void function(void)
{
say_stop("&x, &y", 1);
&x / 2;
}

This would result in Dink saying "4, 8" and then "2, 6".
February 15th, 08:06 AM
peasantmb.gif
yeoldetoast
Peasant They/Them Australia
Oh, NOW YOU'VE DONE IT! 
It does the same thing in 1.08, as it creates 3 separate script instances. There's just seemingly very little documentation on variable scope out there other than that thread plus magicman's rant.
February 15th, 10:56 PM
custom_simon.gif
SimonK
Peasant He/Him Australia
 
Wow, this helps me a lot towards getting a grasp on variables and DinkC. I haven't digested it all, but it is one of the best explanations I've seen for script instancing etc.

The forever/persistent loot thing is something I wanted for SOB with the skulls and the graves in Story 3, I think in the Redux versions I ended up using global variables, but this whole multiple scripts/library.c etc is very intriguing, and is something I really should adopt for Necromancer once I can get my head around it all.
February 16th, 02:59 AM
peasantmp.gif
Skurn
Peasant He/Him Equatorial Guinea duck bloop
can't flim flam the glim glam 
persistent loot should be in every dmod ever.

blasting open a chest you cant see on part of the screen you can't get to without taking a detour and losing whatever was in it is bullshit.