mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
1d4a3c700b
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@985 0fc631ac-6414-0410-93d0-97cfa31319b6
50 lines
1.7 KiB
Plaintext
50 lines
1.7 KiB
Plaintext
Variables
|
|
|
|
MSE script has the notion of ''variables''.
|
|
A variable is a name holding a value, assigned using the @:=@ operator:
|
|
> variable := 1 + 1
|
|
From now on (until another value is assigned) @variable@ evaluates to @2@ in the rest of the script.
|
|
|
|
--Scope--
|
|
|
|
Assignments to variables are ''local'' to the current function.
|
|
Consider:
|
|
> function := {
|
|
> x := "something else"
|
|
> # here x is something else
|
|
> }
|
|
> x := 1
|
|
> function()
|
|
> # here x is still 1
|
|
|
|
Unlike most programming languages MSE script uses [[http://en.wikipedia.org/wiki/Dynamic_scoping#Dynamic_scoping|dynamic scoping]].
|
|
This means that assignments done in the calling function are visible in the called function:
|
|
> fun := { "xyz is {xyz}" }
|
|
> one := {
|
|
> xyz := 1
|
|
> fun()
|
|
> }
|
|
> two := {
|
|
> xyz := "two"
|
|
> fun()
|
|
> }
|
|
> one() == "xyz is 1"
|
|
> two() == "xyz is two"
|
|
|
|
This can be very useful for variables like @card@, which are used by many functions.
|
|
|
|
--Overriding functions--
|
|
Like custom functions, the [[fun:index|built in functions]] are also stored in variables.
|
|
It is possible to overwrite them:
|
|
> to_upper := { input }
|
|
> to_upper("xyz") == "xyz" # Not what it used to do
|
|
|
|
A neat trick is adding 'extra' behaviour to functions.
|
|
This can be done by first making a copy, and calling that:
|
|
> real_to_upper := to_upper
|
|
> to_upper := { "upper case: " + real_to_upper() }
|
|
> to_upper("xyz") == "upper case: XYZ"
|
|
Note that @real_to_upper@ is called without extra parameters, the @input@ variable is still set from the outer call to the new @to_upper@ itself.
|
|
|
|
<div style="text-align:right;">previous: <a href="operators">← Operators</a> | next: <a href="functions">Functions →</a></div>
|