Let the AI write the code
AIs have been all the rage for a while now. And rightfully so. Language AIs like ChatGPT are powerful tools and they are here to stay.
The advantages of AI-generated code in game development
Bootstrapping and Prototyping
AI-generated code can automate repetitive coding tasks. Especially the boring bootstrap code that many of us now generate with snippets, templates, macros, ... . Why not ask the AI to create a quick hello world for us. Even if the code does not work right out of the box, it usually is a fine starting point. Personally, I am looking forward to many hours saved :-)
Optimization and Bugfixing
Like any machine, AIs have the advantage that they do not mind performing repetitive tasks which require the utmost amount of pedantry (aka programming). They simply don't make silly mistakes like missing a semicolon. Once they have integrated with most modern compilers I would expect that they will never again produce broken code. It may not quite do what you want, but at least it will compile.
Consistency
No more bickering about coding styles. The AI will do it however you tell it to. It simply follows the rules and does not forget to apply them.
Translation
Code is logic and computers (and thus AIs) are good with logic. Therefore it's not a big leap to predict that soon it won't matter much in which programming language you are proficient. If the code is needed in another language we will simply ask the AI to translate it for our colleagues.
Disadvantages
Lost in translation
These AIs are still in their infancy and sometimes it is hard to make them understand what you want. Especially if the tasks are complex or require a bit of reasoning. Then it is still much faster to simply whip up the code yourself. However, once you have it, it surely does not hurt to ask the AI for improvements.
If memory serves well
Future programming will be more like searching the web nowadays. Instead of doing the work from the ground up, we will be more concerned with filtering the garbage from the rest. In this case, finding the working code that does exactly what we want.
Though herein lies a problem. While playing with these AIs I noticed that quite often I had to already know the answer to get the right result. In short, they served well as a reminder to fill in the gaps of how exactly the code was spelled, but they could not extrapolate. They had no idea what my goal was.
Taking leaps of imagination is still a problem. Creativity especially. They are trained on existing data. They can create combinations that did not exist before. Just like randomizing each color in an image can, at one point, result in a Mona Lisa. AIs will get there much quicker than a random approach thanks to their training data but once you are outside the known data, even the best AIs generate a lot of garbage. Weeding through that will become a problem once we have pushed them to the limit of what can be extracted and recombined from existing data (which still is a lot).
Money makes the world go round
One thing I like is the premium (or open-source) model of current AIs. That way maybe we can avoid diluting results with advertisements. A world where SEO is not a thing anymore would be great.
For me, it's easy to say that paying for AIs is a good thing. I can afford $1 per 1000 words. Others might struggle and we will have to find ways to ensure everyone can access this technology.
How to use AIs like ChatGPT in Unity
TLDR: Get your ChatGPT API key, buy SPARK AI and start exploring.
There are many packages offering AI support in the Unity Asset Store. We have purchased what seem to be the three most popular ones and put them to the test. We used Unity 2021 and Unity 2022 since at the time of writing those were the most used LTS versions with about 80% of all users in total [1]
It needs to be said that all the solutions in the store are actually just API wrappers for the real AI service. So they mainly differ in how comfortable they are and what boundaries they set for the prompts.
Preparations
They all use the ChatGPT API to generate their scripts and therefore none of these work offline. You will need to register an account with OpenAI and get an API key.
Here is a step-by-step guide:
- Create an account with OpenAI.
Here is the link to the OpenAI registration form. The account is free but you will need an email address and a phone number for verification.
If you want to use the AI a lot you will have to purchase additional processing power (called tokens). Four tokens are about three words and 1000 tokens cost about $0.1. This means that for $1 you will get about 7500 words. - Request an API key from the API Keys page.
At the time of writing OpenAI offers about $5 in free API calls (about 37.000 words). This may change in the future but for evaluating the product it is sufficient. You can check how much you have used on the Account Usage page.
AI Toolbox (formerly "ChatGPT Script Generator")
Our first candidate is the "AI Toolbox", formerly known as "ChatGPT Script Generator" (closed-source).
First, you will have to add your API key. You can find the input field in the project settings:
A button for testing the key would have been nice.
A nice addition is the "Generate Component" button (though you can disable it if needed).
Simple query test
In their demo video, they used this prompt to create a controller for an airplane.
"Add 3D airplane controller that accelerates automatically up to max speed. Player controls pitch and yaw."
This seems like a good use case, so let's give it a try. Here is our very simple demo scene:
Once we have hit the "Generate Component" button we are greeted with a dialog to enter our request.
Result:
The resulting script took about a minute and did cost about $ 0.001 (~10 tokens).
It does work if you chose the right values. By default, the plane will never decelerate since the acceleration is always applied and the deceleration speed is lower than the acceleration. However, that's not part of this test since what is generated is mostly relying on ChatGPT and not the tool itself.
The query is stored in the history. Though there is no real iteration.
Querying for "Make it turn slower" did not yield an altered plane script. Instead, it returned a new script that had no connection to the plane.
Complex query test
With this query, we will test if the AI actually knows a bit about Unity.
"Add a controller that changes the mouse sensitivity of the scroll wheel using the old input system."
Sadly this resulted in a broken script since it uses the new input system instead of the old one.
Verdict
The "ChatGPT Script Generator" asset gives you a nice quick way of trying out ChatGPT. Sadly it's closed-source and it does not support iterating on existing scripts which makes using it a bit tedious.
Positive:
- Streaming Response: The response from the API is streamed, meaning you can watch it as it goes, just like on the website.
- Well-chosen guiding prompts: The preset "building prompts" in the settings ensure that namespaces and [RequireComponent()] attributes are always added.
- History: The tool offers to save the history of your queries. Just like the ChatGPT website does. A handy feature.
- Encrypted Key storage: A nice positive thing is that the key seems to be stored in an encrypted way (though we can't easily check since the code is not visible). Of course, this is no real secure storage but it's better than saving it as plain text. However, saving the key inside the project is risky since it's easy to forget to ignore this file in the VCS.
- Well-known developer: It's by a developer with a long history of supporting their assets.
- Price: With only about $15 it is the cheapest asset we have tested.
Neutral:
- Support via Discord Chat. While Discord is a great tool it still is proprietary software and not as accessible as email or the Unity forum.
- Documentation: The offline documentation is the bare minimum. It tells you how to enter the API key and that's it. Though to be honest, there isn't much more you'll need to know to use it. The online documentation however is nicely done and offers some tips and tricks.
Negative:
- Closed-Source (DLL): The assets code is closed-source in a DLL file. Something that can give you trouble if things don't work or the developer drops support.
- No testing for the API key. We have to waste our precious tokens to find out whether or not it works.
- No real iteration: There is history but it lacks context. You can not iterate on your scripts.
- API Key storage: The api key is stored inside the project. This risks that it is committed to the VCS by accident, not great.
GPT AI Integration
Our second candidate is the "GPT AI Integration" (open-source).
Right after importing into Unity 2022, it shows an auto-upgrade dialog for deprecated scripts. Nothing too serious, though it signals that the author has not yet adapted it fully to 2022.
Like all other assets, you must add your API key first. But how? There seem to be no preferences and clicking ChatGPT in the menu throws some errors.
The answer can be found in the manual:
It seems we have to enter the API key in some ScriptableObject file which can be found here:
Okay and that file points to some other file on my hard drive.
I did not feel like creating that file so I chose the other "string" option.
I don't think "organization" is required, so I just left it empty. Sadly the inputs have no tooltips.
I have also noticed that if the Auth Type is set to "String" then the API key will be stored as plain text in the Scriptable Object. Users should be warned about that. I am pretty sure some will commit their API key accidentally to their VCS.
Simple query test
Okay, now let's try to generate a controller for our airplane demo.
"Create a 3D airplane controller that accelerates automatically up to max speed. Player controls pitch and yaw."
The result is the full reply from ChartGPT. In order to save the script we have to delete the plain text and hit the "Save as" button.
The script did not compile right away.
It turns out ChatGPT has to be asked to import the proper namespaces.
"Create a 3D airplane controller that accelerates automatically up to max speed. Player controls pitch and yaw. Import all required namespaces. Add RequireComponent attributes for any component used in the script."
Unfortunately, this error kept popping up afterward. Not even removing and reinstalling the asset could fix it. A look into the code reveals that even the code throwing the error is faulty (using an index out of range for reporting the element of an array). It seems someone has tried to write his/her own JSON parser and did not quite succeed. Only after fixing some scripts by hand I got it to work again.
Result:
The final script worked and did make the plane fly. So, if it works then it's a valuable tool.
Complex query test
With this query, we tested if the AI knows a bit about Unity.
"Add a controller that changes the mouse sensitivity of the scroll wheel using the old input system."
Just like the other assets this resulted in a broken script since it uses the new input system instead of the old one. Seems like a ChatGPT error.
Verdict
Sadly the script errors in the "GPT AI Integration" asset itself and the not-so-great UX make it a hard-to-recommend tool, despite the many features it has. But it definitely is an asset that should be revisited once it has matured a bit. If you get it now and the author keeps improving it then I think you won't regret it. And it's open-source, which is something that can not be said about the other tested assets.
Positive:
- Open-Source: The source code is included so we can take a look at how it is done and fix stuff if needed.
- Token and cost estimation (in USD). The dialog shows you a handy estimate of how much the prompt will cost.
- Streaming Response: The response from the API is streamed, meaning you can watch it as it goes, just like on the website.
- Supports multiple use cases: This one is not just a script generator. It is a ChatGPT toolkit and you can use it to send search queries, do text translations, and generate shaders.
- Script Iteration: You can hand in an existing script and ask for changes based on it. Very handy for iteration.
Neutral:
- Support via Discord Chat.
- Relatively new developer: This developer has not yet had the chance to prove himself in the long run. Let's give him the benefit of the doubt.
Negative:
- Deprecated Script APIs: With Unity 2022 we got some auto-upgrade dialogs at the start. Nothing to be too worried about.
- Complicated Setup: It may be a good setup for production in a team and I understand the value of keeping the API key out of the project and harms way, but this could be much more streamlined.
- Security: If the Auth Type is set to "String" then the API key will be stored as plain text in the Scriptable Object. This is risky since it's easy to forget to ignore this file in the VCS. The good things is that if you are concerned you can store it in a json file in your user profile, outside the project.
- UX & Documentation: Some things (not everything) are explained in the manual pdf but not in the editor, no tooltips. Sadly that's not a very convincing experience. Also, the manual is not shown automatically after installation, nor is it linked in the Tools menu. It is left to the user to find out that it exists.
- No "Cancel" button while the answer is streamed.
- The code seems broken at some points: I have encountered one instance where I had to fix the code for it to work (off by one error). Though it happens to the best of us.
Spark AI
The third tool tested was "SPARK AI" (closed-source).
After importing into Unity 2022 it shows a friendly welcome dialog with a link to the settings to enter the API key.
If no API key has been entered then the log is flooded with errors, like this:
Once the API key is set it vanishes, all good.
The Spark AI main window has grouped every feature into a tab. That's neat.
It also offers some handy script creation and script iteration buttons for your components.
Simple query test
As with the other assets we have given it this simple test query:
"Create a 3D airplane controller that accelerates automatically up to max speed. Player controls pitch and yaw."
Result:
The result is a nice dialog to save the script.
Unfortunately, the creators of the tool have missed that the default folder for saving (/SparkScripts) may not exist.
Not a big problem. We can specify an existing folder in the settings. Though it would be nice if the tool just generates the missing folder.
Iterating the script
Spark AI also offers an iteration dialog for your scripts. Let's try adding a "jump" mechanic to our plane.
The "Create Script with AI" dialog shows the full ChatGPT response. We have to copy the script part and save it ourselves. Not ideal.
Like most of the other tools, it does not add [RequireComponent] attributes for the components it adds.
Luckily there is an "Ask for modifications" button. Let's try that.
Oh, nice. It did what we asked. And now there is a new "revert" button. Awesome!
Complex query test
With this query, we tested if the AI knows a bit about Unity.
"Add a controller that changes the mouse sensitivity of the scroll wheel using the old input system."
Just like the other assets this resulted in a broken script since it uses the new input system instead of the old one. Seems like a ChatGPT error. After three or four iterations it suddenly started offering solutions with the old input system. Very interesting. The AI is learning.
Verdict (the winner)
In my opinion "SPARK AI" is the best and most complete AI toolkit currently available for Unity. There are some areas to improve but overall it has good UX and just works. Being close-sourced is a bit of a bummer though.
Positive:
- Mostly very good UX: There is a nice welcome dialog after installation and the Spark AI dialog has a tidy look. Everything is sorted neatly into tabs and script iteration is fun to use. The only thing missing is streaming in the responses and a direct saving of new scripts.
- Supports multiple use cases: This one is not just a script generator. It can also generate images, shaders, and chat dialogs.
- Script Iteration: The iteration dialog is very easy to use. It's fun to improve scripts this way.
- Well-known developer: It's by a developer with good standing and a long history of supporting their assets.
Neutral:
- Support via Discord Chat and email.
Negative:
- Closed-Source: The code is hidden in a .dll file which makes it hard to see what's happening or fix errors.
- No Response Streaming: Sometimes you click "Send" and then nothing happens while the tool is potentially busy waiting for a response. You only get the answer once it is done. There is a fake progress bar but it's not very useful. Sometimes feels like the tool froze until you, eventually, get the response.
- Security: The API key is stored as plain text in the editor prefs. Though it's not inside the project, so at least there is no danger of committing it to the VCS.
- Log messages: A tiny thing. Sometimes it logs a bit too much (no API key).
- No "Cancel" button while the answer is loaded.
What does the AI think about all of this
I asked Chat GPT to list ten advantages of AI-assisted programming. I was surprised. The list matches quite well with what I thought.
Not all of them make sense to me though. But hey, at least this tells me that this AI is not that intelligent yet. We may still have some time left till the machines take over the world.
Thanks for point number 9 ChatGPT. Now I feel really appreciated as a programmer (being just a cost factor, ya know). Maybe I should scratch that one before handing this report over to my boss.
When asked to predict the future we tend to just linearly interpolate the current state. In truth, change is often exponential. This can be an advantage (technology) but also a disadvantage (climate change). The digital revolution has been by far the fastest change we have ever seen and chances are that this AI thing will accelerate it even more. Turning a blind eye is not an option but we should consider what it will do to us.
Asset Recommendations
I hope you liked the article. Here are some asset recommendations.
Sources
- Unity Usage Reports (2023-04-10): https://forum.unity.com/threads/editor-version-usage-information-updated-march-16-2023.485317/