variable inheritance when using import

raorn

Member
Does ``import'' just "paste" other script, or it does something "clever"?
Let's say i have three scripts:

script1.ash
Code:
int var1;

int do_something1(...)
{
...
}

script2.ash
Code:
int var2;

int do_something2(...)
{
...
}

mainscript.ash
Code:
int mainvar1;
import <script1.ash>;
import <script2.ash>;
int mainvar2;

void main(...)
{
...
}

Can I:

  • [li]access var1 and var2 from mainscript?[/li]
    [li]access mainvar1 from script1 and script2?[/li]
    [li]access mainvar2 from script1 and script2?[/li]
    [li]access var1 from script2?[/li]
    [li]access var2 from script1?[/li]

I'm asking this because I have two useful relay override scripts (Disco Combos and Fight Buttons). For now I've just copypasted two scripts together, but it's not easy to support it and I want to use another script for Shimmering Portal punchcards action (not written yet, just planning). So I want to do something like:

fight.ash:
Code:
buffer page;

import <script1.ash>;
import <script2.ash>;
...
import <scriptN.ash>;

void main()
{
  page = visit_url();

  page = do_something1(page);
  page = do_something2(page);
  ...
  page = do_somethingN(page);

  write(page);
}
 
Can I:

1 * access var1 and var2 from mainscript?
2 * access mainvar1 from script1 and script2?
3 * access mainvar2 from script1 and script2?
4 * access var1 from script2?
5 * access var2 from script1?

The answer is no across the board. What you have done will create an error. The import statements must be first in the script otherwise it will not work.

That aside:

1 yes
2/3 no---remember import must come before the variable declaration.
4/5 I believe it is possible by importing each script into the script which you want variable access, but I do not know what happens when scripts recursively import each other. I do know that if script 1 is imported by script 2, and the main script, kolmafia will pass over the import statement which imports it a second time ignoring it, but the functions and variables in script 1 will be available to script 2 as well as the main script.

Code:
buffer page;

import ;
import ;
...
import ;

void main()
{
  page = visit_url();

  page = do_something1(page);
  page = do_something2(page);
  ...
  page = do_somethingN(page);

  write(page);
}

since you pass page to the functions and set page with the return results, the functions need not be able to see the declaration.

valid kolmafia code:

Code:
import ;
import ;
...
import ;

buffer page;

void main()
{
  page = visit_url();

  page = do_something1(page);
  page = do_something2(page);
  ...
  page = do_somethingN(page);

  write(page);
}

Now the key is you will have to modify the original scripts that you want to make work together because they cannot call write() otherwise the next part will not run.
 

raorn

Member
Thanks. Now things a bit more clear for me.

Only two things have meaning for fight.php relay override - page itself and encountered monster. These two can be passed as function arguments. I'll post my results when it's ready.

It could be great if one can have two functions with the same name in two different scripts and do something like:

Code:
import <script1.ash>;
import <script2.ash>;

void main()
{
  ...
  script1::do_something(...);
  script2::do_something(...);
  ...
}

In this case it would be possible to create "framework" for fight overrides. However, this would break existing scripts. Cosider this as a long-term feature request ;-)
 
Top