The Dink Network

Reply to Re: Scripting problem

If you don't have an account, just leave the password field blank.
Username:
Password:
Subject:
Antispam: Enter Dink Smallwood's last name (surname) below.
Formatting: :) :( ;( :P ;) :D >( : :s :O evil cat blood
Bold font Italic font hyperlink Code tags
Message:
 
 
October 3rd 2010, 11:36 PM
custom_magicman.gif
magicman
Peasant They/Them Netherlands duck
Mmmm, pizza. 
This... is complicated hairy internal stuff. What I'm going to say now probably only applies to 1.08. No clue about earlier versions (some of the comments suggest it used to be different), nor about freedink.

Anyway, procedure calls, external(), spawn()... they all actually create a new script. It uses up a script slot, and they have a different value of &current_script.
Using run_script_by_number doesn't create a new script. Think of it as a goto-by-proxy. script_attach also doesn't create a new script.

Those freshly spawned scripts (at least those created by external() and procedure calls) will be killed off as expected. Unlike what the previous version of this post said, you don't need to kill_this_task() them. You still need to do that for spawn(), though!

Here's the catch: local variables are stored by script-number. Anything that creates a new script (with a new number) won't be able to read local variables. Globals work, of course. Pseudoglobals (&current_sprite, &arg1 to &arg9, etc.) are probably different in every case.

So... how do you know if a different script number is used? Use &current_script. To see if it survives, periodically check scripts_used():

// test.c
void main( void )
{
  int &x = 1234;
  sp_custom("myscript",&current_sprite,&current_script);
}

void talk( void )
{
  custom();
  // Didn't know about &x, but at least we got back.
  external("test","custom");
  // Also didn't know about &x, but we're still here.
  goto label;
  // Yay, it knows about &x. Sadly, we're stuck there.
  say_stop("You'll never see this. Muhahaha!");
}

void custom( void )
{
label:
  int &scripts = scripts_used();
  say_stop("Now using: &scripts scripts",1);
  int &oldme = sp_custom("myscript",&current_sprite,-1);
  if (&oldme == &current_script)
  {
    say_stop("I know about &x and I am happy about that!",1);
  }
  else
  {
    say_stop("I don't know what &x is",1);
  }
}