I tried to follow a video to make portals in my 2d game, but the following error continued to show:
Error at (3, 1): Unexpected “Identifier” in class body.
Code:
extends Area2D
export(int) var id = 0
var lockPortal = false
func LockedPortal():
lockPortal = true
yield(get_tree().create_timer(0.5), “timeout”)
lockPortal = false
func _ready():
pass
Any help is greatly appreciated
This looks like a Godot 3 code, and your post is tagged godot-4. In Godot 4, you must use @export
instead of export
, and yield
was replaced by await
.
1 Like
Yess thank you!
Im new to godot and I didn’t realize there was a change.
That works but theres a new problem now lol
Error at (9, 39): Expected closing “)” after grouping expression.
which is during this line:
await(get_tree().create_timer(0.5), “timeout”)
[39 is between ) and ,]
The syntax is different. It should be like this:
await get_tree().create_timer(0.5).timeout
There were further changes between Godot 3 and 4. Check out this page: Upgrading from Godot 3 to Godot 4 — Godot Engine (stable) documentation in English
1 Like
Thanks heaps, the errors are gone but unfortunately the function still isnt working. Ill have to scrap this code and try find a new way to make these portals work.
thanks anyway!
bro, can you help me too? This code was written by ChatGPT. Mistake 1 line: Unexpected “Identifier” in class body
extends Area2D
# Ссылка на Polygon2D
onready var polygon = $Polygon2D
# Исходный цвет
var original_color = Color(1, 1, 1) # Белый
var new_color = Color(1, 0, 0) # Красный
func _ready():
# Подписываемся на событие ввода
self.connect("input_event", self, "_on_input_event")
func _on_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton:
if event.pressed:
# Меняем цвет полигонa на новый
var material = polygon.material
if material is CanvasItemMaterial:
material.color = new_color
else:
var new_material = CanvasItemMaterial.new()
new_material.color = new_color
polygon.material = new_material
If you use Godot 4, this line should start with @onready
.
Bro, you’re my savior.
Even ChatGPT4.o failed
You are welcome. The AI is still not all-powerful. It can show you the way, but you also need to understand the code it suggests.
Help me too please, I am getting error at sync variables .
Line 10:Unexpected “Identifier” in class body.
Line 11:Unexpected “Identifier” in class body.
extends Node
const DEFAULT_IP = “127.0.0.1”
const DEFAULT_PORT = 3234
var network = ENetMultiplayerPeer.new()
var selected_IP
var selected_port
var local_player_id = 0
sync var players = {}
sync var player_data = {}
Called when the node enters the scene tree for the first time.
func _ready() → void:
get_tree().connect(“network_peer_connected”,self,“_player_connected”)
get_tree().connect(“network_peer_disconnected”,self,“_player_disconnected”)
get_tree().connect(“connection_failed”,self,“_connected_fail”)
get.tree().connect(“server_disconnected”,self,“_server_disconnected”)
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _connect_to_server():
get_tree().connect(“connected_to_server”,self,“_connected_ok”)
network.create_client(selected_IP,selected_port)
get_tree().set_network_peer(network)
func _player_connected(id):
print("Player: " + str(id) + “Connected”)
func _player_disconnected(id):
print("Player: " + str(id) + “disconnected”)
func _connected_ok(id):
print(“connected successfully to server”)
func _connected_fail(id):
print(“Failed to connect”)
func _server_disconnected():
print(“Server Disconnected”)
I have trouble with a headbob script i saw in a tutorial for godot 4
fps tutorial
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 10.0
#Mouse camera
const SENSITIVITY = 0.03
#Controller camera
const SENSITIVITY_CONTROLER = 0.03
#Special - Please program this soon
const DIVE_VELOCITY = 10
const FIRE_RECOIL = 30
#bob variation
const BOB_FREQ = 2.0
const BOB_AMP = 0.08
var t_bob = 0.01
var gravity = 9.8
@onready var head = $Head
@onready var camera = $Head/Camera3D
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
#Mouse camera control
func _unhandled_input(event):
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * SENSITIVITY)
camera.rotate_x(-event.relative.y * SENSITIVITY)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40), deg_to_rad(60))
#Controller camera control
if event is InputEventJoypadMotion:
head.rotate_y(-event.relative.x * SENSITIVITY)
camera.rotate_x(-event.relative.y * SENSITIVITY)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40), deg_to_rad(60))
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("Jump") 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 input_dir := Input.get_vector("Moveleft", "Moveright", "Moveup", "Movedown")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = 0.0
velocity.z = 0.0
# Head bob
_t_bob += delta * velocity.length() * float(is_on_floor())
camera.transform.orgin = _headbob(t_bob)
move_and_slide()
func _headbob(time) -> Vector3:
var pos = Vector3.ZERO
pos.y = sin(time * BOB_FREQ) * BOB_AMP
return pos
wchc
January 23, 2025, 3:15pm
12
You need to indent your headbob part of the code.
Assuming there are no other problems with your code, because you never specified what’s the issue.
Oh I forgot to mention that. I just had trouble why i had a syntax error with the headbob script. Since I added the indentation, I had a another error On line 66 # Head bob t_bob += delta * velocity.length() * float(is_on_floor()) camera.transform.orgin = _headbob(t_bob)
“Invalid assignment of property or key ‘orgin’ with value of type ‘Vector3’ on a base object of type ‘Transform3D’.”
Hey could you help me too, i have this but getting an error from line 7
extends CharacterBody2D
var destination = Vector2()
var distance = Vector2()
var velocity = Vector2()
var snapPosition = Vector2()
Declare an export variable (can be set in the editor)
export var speed = 250
var margin = 1
Called when the node enters the scene tree for the first time.
func _ready():
destination = position
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta):
if position != destination:
distance = Vector2(destination - position)
velocity.x = distance.normalized().x * speed
velocity.y = distance.normalized().x * 0
move_and_slide(velocity)
if (distance.x * distance.x < margin):
set_position(snapPosition)
else:
move_and_slide(velocity * 0)
if(destination.x > position.x):
get_node( "Player_Sprite" ).set.flip_h( false )
if (destination.x < position.x):
get_node( "Player_Sprite" ).set.flip_h( true )
pass
func _input(event):
if Input.is_action_pressed (“ui_leftMouseClick”):
destination = get_global_mouse_position()
snapPosition.x = destination.x
snapPosition.y = destination.y
Tom_Sullivan:
export var speed = 250
If you use Godot 4, this line should start with @export .
pluto1
February 26, 2025, 10:43am
19
i also need help with the same issue here is my code:
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -600.0
@onready var sprite_2d: AnimatedSprite2D = $Sprite2D
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_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
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
var isLeft = velocity.x < 0