Working with AI
Authoring scripts with AI
Let a model write .tgscript visual scripts in their text form, then turn them into node graphs.
The Script Editor's Text tab accepts the same graph as a JavaScript-like text. A model can write that text, and you get a real, editable node graph.
Workflow
- Copy the system prompt from the prompt library into your AI chat.
- Describe the behaviour: "when the player is within 3 m, move this object to (2, 5, 0)".
- Script Editor > New, switch to Text, select all, paste the answer.
- Compile. If the status isn't green, read the error (it has a line number) and give it back to the model.
- Save, and attach the script in the Inspector.
An agent can do steps 3–5 itself:
js
tgDebug.scriptEditorShow(true);
tgDebug.scriptEditorCreate('Bounce');
const r = tgDebug.scriptEditorCommit(text); // r.errors → [{line, message}]
tgDebug.scriptEditorCompile();
const st = tgDebug.scriptEditorState(); // st.compileOk, st.refusals
tgDebug.scriptEditorSave();Rules models get wrong
Put these in your prompt. They come from testing the parser:
- The file starts with
//!tgscript v1and//! script name=<Name>, and the graph sits between//! region graphand//! endregion. - Every statement is preceded by its own
//! node <id> <Type> pos=(x,y)line. Ids are unique integers. - Variables are named
n<id>_value(sources) orn<id>_out(operators). Using a variable creates the wire. - Attribute values are unquoted for simple words:
key=Space,component=DistanceToPlayerSquare. - Only nodes from the node reference exist. There are no loops, no variables that persist between frames, no functions, no strings beyond attributes, and no access to other entities.
if (...) { } else { }is a Branch node, and statements run in order.- Constants are written with two decimals (
4.00) and vectors asctx.vec3(x, y, z).
Example
A correct answer (tested in the editor):
tgscript
//!tgscript v1
//! script name=Bob
//! region graph
//! node 1 OnUpdate pos=(60,80)
export function onUpdate(ctx) {
//! node 2 KeyDown pos=(260,80) key=Space
const n2_value = ctx.keyDown("Space");
//! node 3 Branch pos=(460,80)
if (n2_value) {
//! node 4 ConstVec3 pos=(460,240)
const n4_value = ctx.vec3(0.00, 2.00, 0.00);
//! node 5 SetVelocity pos=(660,40)
ctx.setVelocity(n4_value);
} else {
//! node 6 ConstVec3 pos=(460,360)
const n6_value = ctx.vec3(0.00, 0.00, 0.00);
//! node 7 SetVelocity pos=(660,200)
ctx.setVelocity(n6_value);
}
}
//! endregionThe scene needs MoveByVelocity in update: for velocity to move anything.