Skip to content

MSC Tutorial Part 8

jam1garner edited this page Dec 27, 2017 · 2 revisions

Calling other scripts

For characters you need a lot of code to make all the mechanics come together and having them all in one file isn't very helpful. This is where scripts come in. Scripts are separate sections of code which can be run (or "called") by one another in order to do things such as reuse common bits of code, separate code into easier to understand chunks or simply to allow certain parts of code to be dynamically called. Now how does this work in MSC?

In MSC you have 2 commands that are most important to calling scripts, pushInt and callFunc. If we want to call script_0 we do the following:

pushInt. script_0
callFunc 0

Now how does this work? First we have pushInt. script_0. This is pushing a reference to script 0 onto the stack. A reference to a script is a name that represents it and, in this case, it represents the location, which is used by some commands as the input. At assembly, pymsc assembler takes the reference script_0 and turns it into the location of the script in the file. The next thing that happens is callFunc. What callFunc does is it gets 1 item off the stack and calls the script at that location. Now what does the 0 mean? The parameter for callFunc is the number of arguments. If we do pass arguments they go before the script reference like so:

try. label
pushInt. 1
pushInt. 2
pushInt. 3
pushInt. script_200
callFunc 3
label:

In this case we pass 3 arguments: 1, 2 and 3 (in that order). Now you may also notice that this time we have a push indicator on the try command (the . after the command). Using this you can get a return value from a script. This means a script can be set up to spit out a value which, when the script is run, is pushed onto the stack. Lets look at how a script is set up.

begin 1,1 
pushVar. 0,0
pushInt. 2
multi.
return_6
end

Breakdown:

begin X,Y - there are Y local variables, X of which are arguments that are passed in
return_6 - take the top value off the stack and return it
end - always include this at the end of your scripts

The rest is explained by previous tutorials.

Bonus tips:

  • MSC scripts can be named anything, for example if a script is named wavedash.txt you can push the script reference with pushInt. wavedash (After you've added it to your Scripts file)
  • MSC scripts are, by default when disassembled with references in the format script_[number]. If script_[number] is referenced but script_[number].txt does not exist it will actually use the script of index [number]. This means you can rename disassembled scripts without replacing all the references to it, however a full folder find/replace through a text editor will do the job if you'd prefer to keep the MSC scripts linked by reference instead of by index.