The Dink Network

Custom procedures, return, script_used() etc.

January 15th 2008, 12:59 PM
dinkdead.gif
I was experimenting with custom procedures and I have a few questions...

According to the DinkC reference (v3.1), external() creates a new instance of the script and using my_proc(); for example just calls that procedure in the same script. When playing around with this I noticed that while running a called procedure it counts as a seperate script (checked with script_used()). There's no difference if I use just my_proc() or external("cur_script", "my_proc"); .
IS this any different from external() when used for a proc. in the same script, and does it actually create another instance of the script (as it would seem, especially as local variables can't be referenced) or is it just the way script_used() works that makes it appear like that?
And does it make any difference?

return; - DinkC ref. v3.1 says this will exit the script but it actually exits the current procedure (as stated in DinkC ref. v4) rather than the whole script, is it wrong in v3.1 or have I not understood something?

v1.08 adds the ability to give and return values from procedures - what can be done with this? Not knowing any other languages other than DinkC I can't see a use for it... can someone give me an example of how this could be helpful?

If at all useful and/or interesting, here's the script I was using for procedure stuff:

//key-71.c (G)
void main (void)
{
say_xy("1", 0, 100);
wait(750);
test();
say_xy("4", 0, 250);
wait(750);
kill_this_task();
}
void test (void)
{
say_xy("2", 0, 150);
wait(750);
banana();
say_xy("3", 0, 200);
wait(750);
}
void banana (void)
{
say_xy("BANANA!!", 0, 300);
wait(750);
}

and I was testing it by tapping this:

//key-57.c (9)
void main (void)
{
int &cur_scrp = scripts_used();
say("There are &cur_scrp scripts being used right now.", 1);
kill_this_task();
}
which is key 9 in my Keys
January 26th 2008, 05:46 PM
dinkdead.gif
Anyone?
January 26th 2008, 07:35 PM
fairy.gif
Someone
Peasant He/Him Australia
 
I'm not sure, but I'm led to believe 1.08 modified local function calls to act like external(). It does make a difference because local variables can't be accessed, &current_sprite may not work as desired, and there if you were to have a function call itself you may run out of script numbers. It might be possible to call a function locally using set_callback_random, but I haven't tried it.

I'm not sure what you mean by "exit the script". Does it not exit the script when it exits the procedure? Exit does not mean kill. Unless you mean return is causing it to exit a locally called function and return to the calling function. But as you've found they are on different script numbers even if it's called "locally"..

Functions that take variables are very useful. Of course the same effect could be done with global variables in v1.07 but arguments make it easier. It's useful for when you want to do multiple things that are almost identical.

E.g. if you wanted to make 3 bombs, but in different positions.

//bomb.c
void make_bomb(void)
{
create_sprite(&arg1,&arg2,17,?,1);
//sp_strength etc
}

then call
external("bomb","make_bomb",100,200);
external("bomb","make_bomb",300,200);
external("bomb","make_bomb",500,200);

MouseDink uses functions with arguments if you still doubt how useful it is. See the globalX.c files. For example I have code to determine which direction (out of the standard 8 directions) one sprite is relative to another. You can imagine that whenever I want to find this the code is identical except for the sprite numbers for the two sprites, so I can pass them in an argument and use the same code for any use. I also used it determine the gradient of the line between two sprites, as well as to calculate hit damages..

Another use is to pass an indicator of the calling script (sender). E.g. in MouseDink I've got a function that I want to be run slightly different if it's called when the player first starts the game and when he loads a new game. So I sent a 0 for new game and 1 for load game (or something like that) and I can have the code execute slightly differently depending on what script calls it.
January 27th 2008, 06:15 PM
dinkdead.gif
Thank you!
I've looked at the MouseDink scripts but they're a bit beyond me still... though I can see what you mean about using &arg1, &arg2 etc.

Can you tell me what exactly an argument is? I'm not sure of the right terminology for things. Is it a number that defines something, for example an x/y position or the 1 in say("baa", 1); ?

Also, I noticed that external is normally used: external("bomb","make_bomb"); but you added numbers afterwards to pass on: external("bomb","make_bomb",100,200);. Can that be done with any command - adding in otherwise meaningless numbers - for example create_sprite(200, 200, 3, 23, 1, 54, 75, 999);?
January 29th 2008, 03:08 AM
fairy.gif
Someone
Peasant He/Him Australia
 
Arguments are the stuff between the parentheses when you call a function, and each argument is separated by commas. So for say("baa",1); the first argument is "baa" and the second argument is 1.

I have no idea why you would want to pass frivolous arguments to create_sprite. At best it won't have any effect.

external() and custom global functions are the only function in DinkC that doesn't always require a certain number of arguments (e.g. create_sprite requires 5 arguments). External can take 2-10 arguments and custom globals 0-8. Note that external("bomb","make_bomb") is effectively the same as external("bomb","make_bomb",0,0,0,0,0,0,0,0);
January 29th 2008, 12:39 PM
dinkdead.gif
Ok then, thanks (again!).
I have no reason to add anything to create_sprite, it was just the first thing that came to mind to use as an example.