Variable scope and set_callback_random()
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.
It seems to me that it is... but I could be wrong.
Looking at redink1's notes about 1.08 variable scope suggest it should be fine, as no new script is created in the process.
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".
&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".
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.
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.
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.