Purpose and Benefits
In the Behavior Editor, the autocomplete feature uses AI to help you
generate code snippets for your scripts.
AI code generation speeds up script creation, reduces manual coding effort, and lets you
explore alternative code implementations.
AI code generation is context-accurate and based on the semantic analysis of your code.
Relying on the Creative Experience SDK documentation (JavaScript API documentation), this feature helps you quickly generate
a comprehensive, coherent, and cohesive script that suits the intent of your experience.
Usage
In the Behavior Editor and from your script, you can:
- Write prompts in natural language to instruct AI to generate code snippets and
complete your scripts.
- Replace selected portions of your code with AI-generated snippets.
- Generate code snippets for your whole script based on an automatic analysis of your
current code.
- Accept or reject the generated code snippets.
AI code generation requires a clear user input to occur, which prevents you from using it
inadvertently. Therefore, several hints appear in your script in the Behavior
Editor and provide you with information on how to start AI code generation. To
start code generation, you can either use the Ask AURA
command or predefined shortcuts (Ctrl +
B, Ctrl + L).
Note:
The AI code generation shortcuts (Ctrl + B, Ctrl +
L) of the Behavior Editor are contextual. Therefore,
they do not conflict with global shortcuts used in the experience.
Prompting Practices
Effective code generation with AI depends on how clearly you craft your prompts. A
well-structured prompt reduces ambiguity and helps the model produce more accurate, usable
code.
- Clearly define the task and required outcome.
- Structure your prompt around explicit sections, categories, or themes when
relevant.
- Provide as much context as possible and examples when possible.
- Specify constraints (language, libraries, performance, style).
- Detail specialized terminology.
- Describe expected behavior and edge cases when relevant.
- Iterate on the prompt based on the AI model results to improve accuracy and
quality.
Note:
AI code generation is inherently nondeterministic. The same prompt can yield different
results. This nondeterminism helps the AI model produce a wider and more diverse range of
suggestions.
Examples: Simple Prompts
The following examples demonstrate the type of simple prompts that you can write to
generate code using AI. They show how you can prompt AI using single sentences to do quick,
simple, or repetitive tasks.
Each example contains the initial prompt and the resulting code produced by AI.
- Example 1: Creating Logs for Called Functions
- Base Code
-
beScript.onStart = function () {
//Will be called on the experience start.
};
beScript.onStop = function () {
//Will be called on experience stop.
};
beScript.execute = function (context) {
//Will be called on each frame.
//To access last frame duration, use 'context.deltaTime'.
//Insert your code here.
};
- Prompt
-
Add logs for each called function.
- Resulting Code by AI
-
beScript.onStart = function () {
console.log("Experience started: " + STU.Experience.getCurrent().getName());
};
beScript.onStop = function () {
console.log("Experience stopped: " + STU.Experience.getCurrent().getName());
};
beScript.execute = function (context) {
console.log("Frame executed with deltaTime: " + context.deltaTime);
};
- Example 2: Hiding Actors from a Collection
- Base Code
-
beScript.onStart = function () {
};
beScript.onStop = function () {
};
beScript.execute = function (context) {
};
- Prompt
-
On start, hide each actor of the collection named ActorsToHide.
- Resulting Code by AI
-
beScript.onStart = function () {
var collection = this.actor.getExperience().getCollectionByName("ActorsToHide");
if (collection) {
for (var i = 0; i < collection.getObjectCount(); i++) {
var actor = collection.getObjectAt(i);
if (actor && actor instanceof STU.Actor3D) {
actor.visible = false;
}
}
}
};
beScript.onStop = function () {
//Will be called on experience stop.
};
beScript.execute = function (context) {
//Will be called on each frame.
//To access last frame duration, use 'context.deltaTime'.
//Insert your code here.
};
- Example 3: Making an Actor Rotate around Its Own Axis
- Base Code
-
beScript.onStart = function () {
//Will be called on the experience start.
};
beScript.onStop = function () {
//Will be called on experience stop.
};
beScript.execute = function (context) {
//Will be called on each frame.
//To access last frame duration, use 'context.deltaTime'.
//Insert your code here.
};
- Prompt
-
Make this actor rotate around itself around its z-axis.
- Resulting Code by AI
-
beScript.onStart = function () {
// Initialize rotation parameters
this.rotationSpeed = 0.5; // radians per second
this.rotationAxis = new DSMath.Vector3D(0, 0, 1); // Z-axis
};
beScript.onStop = function () {
// Cleanup if needed
};
beScript.execute = function (context) {
// Get the actor to rotate
var myActor = this.getActor();
// Calculate rotation angle based on deltaTime for consistent speed
var rotationAngle = this.rotationSpeed * (context.deltaTime / 1000);
// Get the current position to use as rotation origin
var origin = myActor.getPosition();
// Rotate the actor around its Z-axis
myActor.rotateAround(origin, this.rotationAxis, rotationAngle);
};
Example: Advanced Prompt
The following example demonstrates the type of detailed instructions that you can write to
generate code using AI. It shows how you can structure a relatively complex prompt for a
specialized use case and how you can address AI to achieve more efficient results.
The example shows a complex prompt divided into titled blocks. Each block includes a
dedicated description and an analysis from a prompt engineering perspective.
Request
You must implement a behavior attached to an actor that represents a flying drone in a 3D scene.
This behavior must control the drone’s position and motion in response to keyboard input events.
The implementation must be clean, deterministic, and easy to extend.
The Request block defines the global objective: implement a drone behavior
controlled by keyboard inputs in a 3D scene.
This block works as a task anchor. It removes ambiguity about what to build and adds strong
constraints (clean, deterministic, easy to extend)
that guide the model toward structured and reliable code rather than ad hoc
logic.
Actor Context
The drone actor contains four subactors representing its propellers:
- Prop_RearRight
- Prop_RearLeft
- Prop_FrontRight
- Prop_FrontLeft
Retrieve and store the subactors for runtime manipulation (e.g., rotation for visual feedback).
The Actor Context block describes the environment and available entities:
the drone and its propeller subactors.
This block works as a grounding layer for the model. By explicitly listing manipulable
components, it reduces assumptions and helps the model map logic directly to existing scene
objects, improving accuracy and consistency.
Initialization
1. Register keyboard input listeners for real-time control.
2. Retrieve all propeller subactors and store them in clearly named variables.
3. Log the names of all retrieved actors once for debugging purposes.
4. Store the initial position of the drone as its ground resting position.
5. Initialize physics-related values:
- Velocity = 0
- Acceleration = 0
Ensure that all state variables are clearly scoped and persist across updates.
The Initialization block defines setup and persistent state:
- Input listeners
- Actor retrieval
- Logging
- Physics variables
This block enforces a clear state initialization phase. This prevents missing dependencies
and ensures that the model separates one-time setup from runtime logic, which improves code
structure and reliability.
Movement Logic
1. If this.up = true:
- Rotate all propeller subactors around the world z-axis to simulate lift generation.
- Apply a smooth and gradual lift-off behavior with natural-looking inertia.
The altitude must increase progressively rather than instantly.
Altitude is defined as:
"current world y-coordinate position minus initial ground y-coordinate position"
2. If this.up = false:
- Stop or reduce propeller rotation gradually.
- Apply a smooth descent back to the initial ground altitude.
- Ensure that motion decelerates naturally (no abrupt stops).
The motion must feel physically plausible and visually smooth.
The Movement Logic block defines runtime behavior:
- Input handling
- Propeller animation
- Vertical motion physics
This block works as the behavior specification layer. The conditional rules (If
this.up = true, If this.up = false) and qualitative constraints
(smooth, gradual, natural) guide the model toward
realistic motion modeling instead of rigid or binary behavior. This results in more natural
and physically coherent outcomes.
Limitations
- The dialog box for AI prompting does not rely on the conversational features of Virtual Companions (AURA, LEO, MARIE). The dialog box only lets you generate code based on prompts.
- AI relies solely on your script in the Behavior Editor for context.
AI cannot access your whole experience model, editor properties, drivers, sensors, etc.
Ensure that you exhaustively describe the context to AI for more optimal results. For more
information, see Prompting Practices.
- There is no code history management. You cannot navigate between the proposals or
modifications performed by AI. You can only accept and reject generated code, or revert
the changes by performing an undo operation (by default, Ctrl +
Z).
- AI features work only in a cloud environment. If you do not sign in to the 3DEXPERIENCE
Platform or if the system cannot establish a connection to the agent, you cannot use AI
code generation.