TruScript Reference
Get started with the TruScript programming language
| This page covers some of the technical aspects of TruScript. If you’d like to get started with TruScript without diving into the technical details, check out the other pages in this section! |
What is TruScript?
TruScript is a dynamically-typed, imperative programming language designed for creating interactive experiences in TruWorlds.
We built TruScript to be the first-class scripting language for the TruWorlds game engine. It features a familiar syntax and structure that will feel comfortable to anyone who has used other scripting languages. If you haven’t written any code before, TruScript is also a great place to start.
We think you will greatly enjoy TruScript. Its flexibility and ease of use makes experimenting with different kinds of gameplay experiences quick and fun.
Compared to other languages
TruScript uses an indentation-based syntax similar to Python, making it easy to read, write, and understand structured logic and control flow.
Its runtime behavior is closer to Lua and JavaScript: Variables are global unless declared otherwise, functions are first-class values that capture information about their surrounding scope, and everything is a value of a single dynamic type system.
TruScript’s memory management and internal state is actually backed by the TruWorlds game engine, so you don’t need to worry about memory allocation or garbage collection. We take care of that complexity for you! TruScript is also designed to be safe and secure, running in a custom sandboxed VM environment.
TruScript Example
This documentation will provide sample code snippets to illustrate TruScript examples and teach you how to use TruWorlds features. Here’s one now:
# This is a comment! You can put anything you'd like in a comment.
# Comments are super useful for explaining your code to others, or even to yourself when you come back to it later.
###
If you put three hash symbols before a line, it will be treated as a multi-line comment.
Multi-line comments end when you put another set of three hash symbols on a line by itself.
###
# Inheritance
extends Part
greeting = "hello"
count = 3
print(greeting)
print(count)
func some_function(param_a, param_b):
if param_a < 0:
warn("param_a can't be negative")
return nil
return param_a + param_b
func make_parts():
for i in range(0, 10):
local myPart = new Part()
myPart.Position = new Vector3(i, 0, 0)
myPart.Color = Color3.Random()
myPart.Parent = world
make_parts()
# Track pizza toppings using arrays
chef_name = "Pat"
pizza_toppings = ["cheese", "pepperoni", "mushrooms"]
pizza_toppings.push("chicken")
pizza_toppings.push("pineapple") # Pineapple belongs on pizza, so add some!
pizza_toppings.remove("mushrooms")
printf("Chef {0} is making a pizza with {1} toppings", chef_name, pizza_toppings.length)
# Track player lives using tables
player_lives = {
"Pat" = 3,
"Nicholas" = 3,
"Matthew" = 3
}
func kill_player(player_name):
if player_lives[player_name] > 0:
player_lives[player_name] -= 1
printf("Player {0} has {1} lives remaining", player_name, player_lives[player_name])
else:
printf("Game over for player {0}!", player_name)
table.remove(player_lives, player_name)