Help with godot 3 to godot 4 code

Godot Version

4.2

Question

attempting to make a bullet based off a tutorial, (movement works, i just copied all the code) i used a godot 3 tutorial, and dunno how to fix it for godot 4

var bulletspeed = 2000
var bullet = preload(“res://Scenes/Bullet.tscn”)

func _process(delta):
var input_vector = Input.get_vector(“Left”, “Right”, “Up”, “Down”)
velocity = input_vector * 200
move_and_slide()
look_at(get_global_mouse_position())

if Input.is_action_just_pressed("Shoot"):
	fire()

func fire():
var bullet_instance = bullet.instance()
bullet_instance.position = get_global_position()
bullet_instance.rotation_degrees = rotation_degrees
bullet_instance.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))
get_tree().get_root().call_deferred(“add_child”, bullet_instance)

Here’s a few things to remember when describing an issue:

  • Describe the specific problem you’re experiencing
    • What is not working?
    • What should it be doing?
  • Describe the immediate context in and around the problem
    • Show relevant formatted code (use the </> button)
    • What error messages are you getting?
    • How does your scene tree look?
    • etc…

It’s hard to solve a problem without knowing all the information.

1 Like

There is documentation for migrating from Godot 3 to 4: Upgrading from Godot 3 to Godot 4 — Godot Engine (stable) documentation in English

1 Like

Try to learn Godot 4 properly, here is the correct codes where you made the mistake:

func fire():
    var bullet_instance = bullet.instantiate()
    bullet_instance.position = get_global_position()
    bullet_instance.rotation_degrees = rotation_degrees
    bullet_instance.apply_impulse(Vector2.ZERO, Vector2(bulletspeed, 0).rotated(rotation))
    get_tree().get_root().call_deferred(“add_child”, bullet_instance)

The bullet.instance() is wrong it will be bullet.instantiate() and using Vector2() is odd, use Vector2.ZERO

1 Like