Global variables
July 25th 2004, 10:04 AM

Vedran


When I want one script to set something which is recognized by another script, do I absolutely need to set a global?
Also, how do I keep an action from repeating? EG: There is an item on a shelf, and after it is picked up, I don't want to be able to pick it up again. Normally in C, I would use static int. Do I need global again?
Also, how do I keep an action from repeating? EG: There is an item on a shelf, and after it is picked up, I don't want to be able to pick it up again. Normally in C, I would use static int. Do I need global again?
July 25th 2004, 10:30 AM

MiloBones


Look at the stuff in the DinkC reference about death. I think if you put in something like:
void die( void )
{
editor_type(¤t_sprite, 1);
}
...in the script, it should stay dead, i.e. not reappear.
void die( void )
{
editor_type(¤t_sprite, 1);
}
...in the script, it should stay dead, i.e. not reappear.
July 25th 2004, 11:46 AM

Vedran


Perhaps, but that helps only partially. What if I don't want to kill the script completely, but need another effect to happen instead?
July 25th 2004, 12:10 PM

MiloBones


Well, you could use the "limited memory" (redink's words) given by editor_seq and editor_frame. In other words, you would have another (invisible) sprite on the screen which changed seq when the first event happened. I don't quite know whether this would work, and it'd be easier to just use a global.
Coming from a C background, I'm sure you're going to hate DinkC. As you are limited to only ~230 active variables at one time (including globals and all currently used locals attached to scripts), its generally a good idea to use editor_seq and editor_frame when dealing with a sprite remembering something.
//Bookshelf
void main(void)
{
int &hold = sp_editor_num(¤t_sprite);
int &myvar = editor_seq(&hold, -1);
}
void talk(void)
{
if (&myvar == 0)
{
say("I found the Magic Thing of Thingy!", 1);
add_item("item-fst", 438, 1);
&myvar = 1;
}
else
{
say_stop("I already got the thing.", 1);
}
editor_seq(&hold, &myvar);
}
You can do this with every sprite, as each sprite has a limited memory of a sequence and frame that you can store and retrieve arbitrary values from. Retrieving that value from another sprite (on another screen) is a bit more tricky, so for this instances global variables are recommended.
//Bookshelf
void main(void)
{
int &hold = sp_editor_num(¤t_sprite);
int &myvar = editor_seq(&hold, -1);
}
void talk(void)
{
if (&myvar == 0)
{
say("I found the Magic Thing of Thingy!", 1);
add_item("item-fst", 438, 1);
&myvar = 1;
}
else
{
say_stop("I already got the thing.", 1);
}
editor_seq(&hold, &myvar);
}
You can do this with every sprite, as each sprite has a limited memory of a sequence and frame that you can store and retrieve arbitrary values from. Retrieving that value from another sprite (on another screen) is a bit more tricky, so for this instances global variables are recommended.
Gyah! An error in the DinkC reference? Could it be?
int &hold = sp_editor_num(¤t_sprite);
if (&hold != 0)
{
this was placed by the editor, lets make the barrel stay flat
editor_type(¤t_sprite, 3);
editor_seq(¤t_sprite, 173);
editor_frame(¤t_sprite, 4);
type means show this seq/frame combo as background in the future
}
Notice Seth has editor_seq(¤t_sprite, #), i.e., the sprite number, and Redink (correctly) has editor_seq(&hold, #), i.e., the editor number.
Not that anyone cares...
int &hold = sp_editor_num(¤t_sprite);
if (&hold != 0)
{
this was placed by the editor, lets make the barrel stay flat
editor_type(¤t_sprite, 3);
editor_seq(¤t_sprite, 173);
editor_frame(¤t_sprite, 4);
type means show this seq/frame combo as background in the future
}
Notice Seth has editor_seq(¤t_sprite, #), i.e., the sprite number, and Redink (correctly) has editor_seq(&hold, #), i.e., the editor number.
Not that anyone cares...
That is why the updated DinkC Reference rocks.