Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

VCFUD Chapter 3 - Pre-optimizing Code for AI

VCFUD Chapter 3 - Pre-optimizing Code for AI

simon_sdev
simon_sdev
Vibe Coding For Unity Devs · · 5 min read
1,269 0

A continuation of Chapter 2 - Leveraging Sequence Systems for Enhanced Vibe Coding

Before we apply the sequence structured discussed in previous chapters - it is keen to pre-optimize the code for AI input.

Preoptimizing code for AI is a practice that bridges that gap between human intent and machine understanding. In many ways, it's like writing pseudocode for a fifth grader - but with the assumption that the fifth grader has a full grasp on all syntax. The core idea is simple enough: LLM's don't need you to tell them how to do everything, that's kind of their entire point. They already know the details of syntax, structure, and most APIs you'll encounter on your journey with Unity (especially with the help of web search features). What they need is your specific context, your intention, and a scaffold to build on.

AI doesn't infer project goals or high-level systems behind the scenes in your project unless you tell it. That means the most effective way to write code that AI can work with is to leave detailed logic to comments and focus instead on laying out the purpose of variables, function headers, and the structural flow of behaviors.

The general process looks like this:

  1. Define Variables – Lay out the key fields you'll need: references, values, and configuration options.
  2. Stub Functions – Clearly name methods that describe what each behavior should do, even if the bodies are empty or only contain comments.
  3. List States and Behaviors – Identify key flow changes or events and where they should occur.
  4. Leave the Nitty Gritties to Comments – Place brief instructions inside method bodies describing the intended behavior. Let the AI fill in the implementation.

Here is an example of a pre-optimized script that clearly outlines user intent:

public class DoorController : MonoBehaviour
{
    // Variables
    public Animator doorAnimator;
    public bool isLocked = false;

    // Functions
    public void OpenDoor()
    {
        // If door is not locked, play open animation
    }

    public void CloseDoor()
    {
        // Play close animation
    }

    public void LockDoor()
    {
        // Prevent door from being opened
    }

    public void UnlockDoor()
    {
        // Allow door to be opened
    }
}

This script contains no functional logic, but it communicates everything the LLM needs to build a functional implementation. Once structured like this, the file can be passed to Copilot or ChatGPT with a simple plain text prompt like: “Fill in the methods so this door plays animations correcctly and won't open when locked.”

The strength of this approach lies in its clarity and compartmentalization. The goal is not to avoid writing code, but to scaffold it in such a way that AI tools can meaningfully collaborate. When the logic of the script is already partitioned into named components and behaviors, LLMs no longer have to guess at structure, naming conventions, or even your intent. This increases reliability, reduces hallucinations, and provides a better launch point for iterative work.

When paired with the sequence architecture described previously in this guide, this technique becomes even more powerful. By wrapping your preoptimized scripts inside GameSequence-based components, you ensure that AI-written logic stays scoped and doesn't tumble into overly large or unfocused monobehaviors. It also guarantees that lifecycle and event flow are predictable, giving the AI a more consistent environment to reason within.

using UnityEngine;

public class DoorSequence : GameSequence
{
    // === CONFIGURATION ===
    public Animator doorAnimator;
    public bool isLocked = false;

    // === STATE ===
    private bool isOpen = false;

    // === ON BEGIN ===
    public override void OnBegin()
    {
        // Trigger opening or closing depending on current state
        if (isOpen)
            CloseDoor();
        else
            OpenDoor();
    }

    // === ON UPDATE ===
    public override void OnUpdate()
    {
        // Optionally listen for input or animation state to complete
    }

    // === ON END ===
    public override void OnEnd()
    {
        // Optional: cleanup or transition behavior
    }

    // === PUBLIC BEHAVIORS ===
    public void OpenDoor()
    {
        // If not locked, set open state and trigger animation
    }

    public void CloseDoor()
    {
        // Set closed state and trigger close animation
    }

    public void LockDoor()
    {
        // Set isLocked = true and prevent open interaction
    }

    public void UnlockDoor()
    {
        // Set isLocked = false and allow open interaction
    }
}

To extend this further, think of scaffolding as a design contract between yourself and the AI. The clearer the contract, the better the AI will perform. Instead of writing out a full class and asking the AI to refactor it, you're writing a description of the job with slots ready to be filled. Just like you'd hand off a detailed spec to a new developer, you're giving the AI a constrained space to operate in, one where it is hard to get off track.

Scaffolding should also reflect modular design. Each class should serve a single purpose. Each function should describe exactly one action or behavior. Avoid nested or overloaded responsibilities unless you know you'll handle the details yourself. What's helpful to you visually - compactness, brevity, smart chaining - is often a barrier for LLM's that rely on sequential inference.

To take advantage of vibe coding workflows, a properly scaffolded file should be readable in a single glance. This means you want minimal dependencies, explicit names, no hidden complexity.

In short, pre-optimizing is about helping the AI help you. Give it what it lacks, structure, context, and intention, and let it handle what it excels at: generating clean, functioning logic. A good scaffold gives the AI just enough shape to be useful, without boxing it into solutions you didn't intend. Acquiring this skill, makes AI a true collaborator, not just a code generator.

Discussion

Loading comments...