When a value needs arithmetic
On this page
OPTIONAL SCRIPTING
Use this only when the built-in operations cannot express what should happen to the old value.
Say an early release measured a relationship on a 0–100 scale. The update calls it trust and uses 0–50. Someone with 40 points should start with 20. A straight rename would leave them with 40. In this example, older saves have no independent trust score.
1
Define trust in the updated project.#
Give it a numeric default for a new player. In the migration designer, select the transition for this release, choose Run Migration Script, then choose GDScript under Script language. The code goes in the script box below the ID fields.
Select the operation on the left and the language on the right. The code below goes into the large text box that opens beneath them.
if variables.has("relationship_score"): variables["trust"] = variables["relationship_score"] * 0.5 variables.erase("relationship_score")2
Read the three lines in order.#
Line 1 checks whether the slot even has the old score. Line 2 halves it and writes the result to trust. Line 3 removes the old name. Indent the last two lines so they only run when the old value exists.
3
Add the operation and check both cases.#
Test a save with 40 relationship points. Look for trust = 20 in Restored progress. Test another slot without the old score. It should keep the normal default for trust. Apply the rule and save the project after both behave as intended.
What code can read here
Migration scripts work with variables and customData. They support small data changes such as arithmetic and existence checks. Scene changes, speech and disk access belong in ordinary game scripts. The designer also offers PCA Script, Lua, BASIC and Visual.
STUDIO 0.86.3.8 / 12
