Published using Google Docs
Scratchy Godot
Updated automatically every 5 minutes

Introduction

This is a reference to help Scratch programmers find the equivalent features using the Godot game engine and the GdScript language

Scratch Blocks and their equivalent in Godot

class_name MySprite extends Sprite2D

func _ready:

Each script will have one _ready() function where you can put things to do at the start

if Input.is_key_pressed(KEY_SPACE):

This will be a check in the _process() of the scene or a sprite script

func _input(event):

  if event is InputEventMouseButton and event.pressed:

    if get_rect()
     .has_point(to_local(event.position)):

        print("You clicked the cat!")

There are lots of ways to do this depending on how specific you need to be.

How to detect a click on a Sprite in Godot (EASY) - YouTube

func _input(event):

  if event is InputEventMouseButton and event.pressed:

await Delay.Seconds(1)

Requires my utility pack. Otherwise you have to do


await get_tree().create_timer(1).timeout

func _process(delta):

The frame update function runs in each script

for i in 10

Looping (gdscript.com)

Events.PlayerScored.emit()

This requires the signal being added to the Events global script

Events.PlayerScored.emit()

await Events.ScoreUpdated

This requires two signals being added to the Events global script. One for the event you’re sending, and one for the one you want to wait for

func _ready() -> void:

  Events.PlayerScored.connect(OnPlayerScored)

func OnPlayerScored(points):

  print("Player got " + str(points) + " points!")

This requires the signal being added to the Events global script

func _process(delta):

  if :

Not really the same in XNA since our Update is always called.  You probably are using this because you want to prevent something else from happening yet.  You could either use a state variable, or just an if-else.

if :

If the value inside the parentheses is true, then do all the code inside the curly block.
Conditional Statements (gdscript.com)

if:

else:

If the value inside the parentheses is true, then do all the code inside the first curly block, otherwise do all the code in the 2nd curly block.

This isn’t something you would do in Godot. Use a signal event instead

while SomeValue == false:


if !SomeValue:

For things that should repeat within this update.


For things that should happen once in this update, and then again in the next update until the condition is met. In Godot we don’t hold up processing, if the current conditions aren’t right, then we run the inner code once, and it’ll repeat on the next update..

return

Exit from the current function

get_tree().quit()

Completely exits the game

texture = load("res://ScratchGraphics/BeachBall.png")


frame = 1

Switch a Sprite2D texture to a different image file


Switch an AnimatedSprite2D to a different frame

frame = (frame + 1) % sprite_frames.get_frame_count("default")

You normally wouldn’t switch costumes like this, but instead create an animation timeline and just run that automatically.

frame

Get the current animation frame number of the sprite.

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

On a sprite

ToDo

ToDo

On the stage

ToDo

ToDo

On a Sprite

ToDo

ToDo

On the Stage

ToDo

ToDo

On a Sprite

ToDo

ToDo


On the Stage

ToDo

ToDo

On a Sprite

ToDo

ToDo

On the Stage

ToDo

ToDo

On a Sprite

ToDo

ToDo

On the Stage

ToDo

ToDo

scale *= 1.10

Increases the size by 10%.

scale = 1.0

Set the size.  1.0 is the default size.

.

scale

Current Size (percentage) is a property of the sprite.

show()

Make the sprite visible

hide()

Make the sprite invisible

z_index = 99

Sprites with a bigger z_index are in front of sprites with lower z_index

z_index -= 1

We are not limited to moving back, you can set the z_index to any number

Move(10);

Move some distance in the current direction.

rotation_degrees += 15

04 Rotate in Degrees Godot Minimal Tutorial - YouTube

rotation_degrees -= 15

rotation_degrees = 90

ToDo

ToDo

ToDo

ToDo

position = Vector2(0,0)


position.x = 0
position.y = 0

Move the sprite immediately to a new position

global_position = Sprite2.global_position

Move this sprite immediately to the same position as another sprite

GoTo(Mouse.Position);

Move this sprite immediately to the same position as the mouse.

create_tween().tween_property(self, "position", Vector2(0,0), 1.0)

Move to another point over a given amount of time. Tweens can be used to animate any kind of value, not just position.

position.x += 10

Move to the right by some amount.

position.x = 0

Move to a specific x position

position.y += 10

Move up by some amount

position.y = 0

Move to a specific y position

ToDo

ToDo

position.x

Current x position of the sprite

position.y

Current y position of the sprite

rotation_degrees

Rotation of the sprite in degrees

+

Add two values

-

Subtract 2nd value from first

*

Multiply 2 values

/

Divide first value by 2nd value

randi_range(1, 10)

Pick a random number in the specified range.

<

Is the left less than the right

==

Are two values equal

>

Is the left more than the right

and


&&

Are both conditions true

or


||

Is either condition true

!

convert true to false and vice versa

"hello " + "world"

combine two strings.

"world"[0]

Get the first character in a string. Array indexes are zero based in most languages, not 1 based like in Scratch.

"world".length()

Get the length of a string

%

Get the remainder after dividing the first number by the 2nd.  For example:

5 % 2 = 1

Using %2 you can determine if the first number is even or odd.  In this case, the result is 1, so therefore 5 is odd.

You can also determine if a number divides evenly by 3

9 % 3 = 0

roundi(3.141)

Round the value to 0 decimal places.

abs(10)

Returns the absolute value of a number.  This means if the number is more than zero, then it doesn’t change.  But if it’s less than zero, then it becomes the same number above zero.

abs(5) = 5
abs(-5) = 5

sqrt(10)

Returns the square root of a number.  This is the number that would have to be multiplied by itself to get the number requested.
sqrt(9) = 3


sin(0.17)


cos(0.17)

Returns the sine or cosine of an angle in radians.
What are radians? (upchieve.org)

To use degrees instead of radians, you’d do it like this

sin(10 PI/180)

tan(0.17)

Returns the tangent of the specified angle (in radians). This is the ratio of the opposite side to the adjacent side.  It’s the slope of the angle.



asin(0.5)


acos(0.5)


atan(0.5)

Inverse trigonometric functions.

ToDo

Returns the natural logarithm of a specified number.

ToDo

Calculates the base 10 logarithm of a number.

exp(10.0)

Calculate the exponential value of a number

pow(10.10)

The power is the value of one number or expression raised to another number.

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

Is the sprite touching any screen edge. We also can check individual edges.

ToDo

Is the sprite touching the mouse pointer.  This function also takes any X,Y position to tell if the sprite is touching that point.

ToDo

Is this sprite touching the other sprite.

ToDo

ToDo

ToDo

ToDo

get_global_mouse_position().x

Current X position of the mouse.

get_global_mouse_position().y

Current Y position of the mouse.

Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)

We’re not limited to the left mouse button, we can also use the right and middle buttons as well as the scroll wheel.

Input.is_key_pressed(KEY_SPACE)

Is a key currently held down

distance_to(get_global_mouse_position())

Get the distance between this sprite’s center point and another point.  In this case, the mouse pointer position.

distance_to(Sprite2.global_position)

Get the distance between this sprite’s anchor point and another sprite’s anchor point.

ToDo

Reset the scene timer.

ToDo

Each scene has a timer that is created and started automatically when the scene is shown. You can access this simply from the scene as “Timer”.  Additional timer objects can be created if needed.


Sprite1.position.x

Sprite1.position.y

Get a value from another object.  This could be a Scene, Sprite, or any other complex object in your program.

ToDo

ToDo

ToDo

ToDo

Sprite2.scale.x

Get the scale of a sprite where 1.0 is normal size.

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

Play a sound.

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

ToDo

MyVariable: bool = true;


MyVariable: int = 3;


MyVariable: float = 3.334


MyVariable: string = "Stuff"


MyVariable: MyClass = new MyClass();

In GdScript variables have different types.  The most common GdScript types are:

bool

Holds only values true or false

int

Numbers without a decimal point

float

Numbers with a decimal point

string

Words or letters

2D games also commonly use these

Vector2

Stores an X and Y position in 2D space

Vector3

Stores an X, Y, and Z position in 3D space

Variables (gdscript.com)

You can also create your own classes for more complicated values, and store these in a variable.

Classes (gdscript.com)

Does not apply. Just delete it from the code.

MyVariable

Retrieve the value from a variable

MyVariable = 0

Set a value into a variable

MyVariable += 1

Change a variable by some amount.

ToDo

ToDo

ToDo

ToDo

var numbers = [1,3,3]

var MyList = ["one", "two"]

Create a list of things.
Arrays (gdscript.com)

Does not apply. Just delete it from the code.

MyList

Use a list variable

MyList.append("three")

Add something to a list

MyList.remove_at(1);

Delete something from a list. Remember the first index is 0 not 1

MyList.insert(0, "zero");

Insert something into a list.  The first index of a list is 0, not 1.

MyList[0] = "stuff";

Replace an item in a list. The first index of a list is 0, not 1.

MyList[0]

Get an item from a list. The first index of a list is 0, not 1.

MyList.count()

Number of items in a list.

MyList.find(thing)

Position of the item in the list. 0=first item, -1=not found


Godot base script equivalent of Scratch script:

Each Godot script includes two functions:

There are different types of scripts based on the type of thing you attach it to. In Scratch, the only things you can attach scripts to are sprites and the stage. In Godot a player object can consist of many components, each with its own script.  Each scene can also have a collection of scripts.

Code Formatting:

GdScript uses tab indentation to indicate code that is in a block. This looks very similar to the way Scratch presents code that is in a block as indented to the right slightly.  In GdScript the line before the block ends with : to indicate the start of a block. You don’t need to do anything to end the block. The next unindented line will be the start of a new block.  Here’s an example:

 

Features in Godot Code that are not in Scratch: