GDScript Reference

# If you don’t know how to do something, google it!


# to write a comment, type a hashtag


# lines are executed in sequence


# words typed are variables, and can be assigned by typing "var" and then its name

# GDScript uses the word "var", but leave out "var" in python

var x = 1

var number = 5


# display the value a variable holds

print(number)


# you can do math, with +,-,*,/,% (modulus), ** (exponentiation), sqrt(), etc

print(number / 2)


# variables can be reassigned, by setting them equal to something else

# variables that store numbers can be used as a number

number = 2 / x

number = number - 4


# operations such as the one directly above can be simplified:

number -= 4 #this does the same thing


# variables can also hold:

# True/False (aka Booleans)

var thing = False

# words, etc (aka Strings), surround in quotes

thing = "Hi, I'm Adrian"

# lists of other data types

thing = [number, False, "Hi, I'm Adrian", 52.67]


# access elements of a list by using []

# the first element in a list is element 0

print(thing[1]) # will print False


# you can reassign list elements

thing[1] = True

print(thing) # will print [-6, True, "Hi, I'm Adrian", 52.67]

# you can add elements onto lists with __.append(), remove with __.pop()

thing.append(3.4)

print(thing) # will print [-6, True, "Hi, I'm Adrian", 52.67, 3.4]

thing.pop(2)

print(thing) # will print [-6, True, 52.67, 3.4]


# similarly to how we can do math, we can also evaluate logic (conditionals)

# 'and' will return True only if both inputs are true

# 'or' will return True if either or both inputs are true

var bool1 = False

# remember, thing[2] is now 52.67

# here, thing[2] < 4 is False and bool1 is False

print(thing[2] < 4 or bool1) # will print False as both sides are false

# to compare any data types, you can use == (equal to), != (not equal to)

# to compare numbers, you can use <, >, <= (less than or equal to), >=

# you can also use the words 'or', 'not', 'and'

print(thing[2] != 3 and not bool1) # will print True


# 'If' statements will run code inside of them if they receive the value True

# lines of code inside of the if statement will be indented one level

# you can use the words if, else, elif (else if)

# you can use conditionals here:

if True: 

    print("is true")

#will print "is true"


if thing[2] == 2: 

    print(thing)

    print("as thing[2] == 2, we will not evaluate the rest of the if statement")

elif bool1: # is equivalent to writing "elif bool1 == True"

    print("bool1 is True")

else:

    print("bool1 is not true")

#will print "bool1 is not true"


# Loops operate over ranges and lists (aka iterables)

# ranges work with the format range(stop), range(start,stop), or range(start,stop,step)

# ranges start at 0 and step by 1 by default

for i in range(1,11): 

    print(i)

# will print 1,2,3,4,5,6,7,8,9,10 (stops before 'stop' number)


# lists are also 'iterables'

for i in [1,5,2]: 

    print(i * 2)

# will print 2,10,4


# inside of the for loop, we can access this new variable I have called 'item'

for item in thing: 

    print(item)

# will print -6, True, 52.67, 3.4


# functions allow you to simplify code and remove re-used elements

# you can put multiple things inside of the function's parenthesis,

# which can be used as variables by code inside of your function

# use "func" in GDScript, and "def" in Python

func repeated_sqrt(value, times):

    for i in range(times): #will start at 0 and go up to times - 1

        value = sqrt(value)

    return value

# return will immediately exit out of the function,

# and give this value to wherever the function was called


# this function can be used as below:

print(repeated_sqrt(5.2,3)) # will print sqrt(sqrt(sqrt(5.2)))

var number1 = 1

print(repeated_sqrt(thing[2],number1)) # will print sqrt(52.67)

number += repeated_sqrt(thing[2],2) + 1

# will increase number by sqrt(sqrt(52.67)) + 1


# function returns don't need to be used

func printvalue(value):

    print(value)

    return "hello"


printvalue(2) #will print 2

print(printvalue(3)) #will print 3,hello