Need Help with Code Not Working Exactly like A Working Example

Yes they do, but I would assume that is normal for the code.

func cannot be indented like this, so when Godot says “Expected indented block” it means there needs to be code after a function, in this case the function _process is empty. To resolve this you can add pass

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	
func start(pos):
	position = pos
	show()

We’re past that now, but thank you for the help anyway!

Its because there is nothing for the player to stand on it.

Add a static node to the main scene, rename as “ground”, and add the a collision shape to it. After that you would wanna add a color rect node just for the player to see the gorund.

Yeah, that’s exactly what I assumed too.

Yep, your on the right track!

Thank you so so much for the help as always!

Anytime! If you ever need help just post your problem here, there’s a lot of people online that could help!

Could I please have help on my other threads then, about the mouse input, the enemy AI in the pacman game, and the collision not working properly in the other platformer? No one has given the correct solution on those threads yet.

Hi, for this code snippet I get told “invalid operands float and Vector2 in operator ==”, which I can accept, but then do you know what I should type instead to get the correct answer to check the direction of the moving direction so that I can flip the sprite properly?

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	if direction == Vector2(-1, 0):
		$AnimatedSprite2D.flip_h = true
	if direction == Vector2(1, 0):
		$AnimatedSprite2D.flip_h = false
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

Direction is a input.get_axies() meaning it can be either 0 or 1, not -1:

Here is an updated version, I hope it works:

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	var direction = Input.get_axis("ui_left", "ui_right")

	if direction != 0:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED * delta)

	animate(direction)
	move_and_slide()

func animate(direction):
	$AnimatedSprite2D.play("run")
	if direction > 0:
		$AnimatedSprite2D.flip_h = true  
		
	elif direction < 0:
		$AnimatedSprite2D.flip_h = false  

It works perfectly. Thank you so much!

Updated Game with New Bug

Hi,

I made sure to follow the correct directions for instantiating a Global scene and so on, but even after I do so I get told “Cannot call method ‘instantiate’ on a null value.” for the platform variable.

I figured I needed to use Global variables in this case because declaring the variable in the gamenode.gd gave me an error about calling something out of its scope, because I was declaring it in the ready function and calling it in the physics_process(delta) function.

I noticed as well when I tried instantiating the platform at the very beginning of the gamenode.gd script before the first function, I got the same error about not being able to instantiate a null value as well.

Can you guys please help me figure out how to achieve what I want (make a platform that scrolls when you press the arrow keys Super Mario style)?

I think I figured out why… it’s cause when I declare a PackedScene in the Globals script, it doesn’t actually register as a PackedScene on the drop down menu to the right, even though it does when I declare it in the gamenode script.

I don’t know if this is a bug or intentional, but if it’s intentional can I please have help understanding how to do what I want correctly (make a platform that scrolls when you press the arrow keys)?

I haven’t actually tried it, but I think I figured out what might be the answer/workaround - declare the platform in the gamenode script and not the Globals script (due to the weirdness I discovered just recently), and then have a script for the platform itself, like attached directly to an instance of the platform - I already have that trick used to make the mushrooms disappear in my Pacman game, so it should work here as well.

No luck - it still gives me the same error about “cannot instantiate a null value” when I declare it in the gamenode script. Can you guys help me understand what I am doing wrong?

Okay, I figured out what I was doing wrong correctly this time. Now I will work on attaching a script to the platform object itself and seeing what it does - hopefully it works without any annoyances.

Indeed it does work perfectly. Awesome!

Okay, now unfortunately I am told “Expected indented block after ‘if’ block”, even though I did in fact indent the blocks properly according to this tutorial’s directions.

Here is the link to the game updated again, with the new error/bug:

Game with New Bug

Can someone please explain what I am doing wrong this time?

Thank you very much in advance.

Okay, in this case the function was redundant and I was just copy-pasting it from the tutorial, so now the error is gone.