kandi background
Explore Kits

godot | Godot Engine – Multi-platform 2D and 3D game engine | Game Engine library

 by   godotengine C++ Version: 3.4.4-stable License: Non-SPDX

 by   godotengine C++ Version: 3.4.4-stable License: Non-SPDX

Download this library from

kandi X-RAY | godot Summary

godot is a C++ library typically used in Gaming, Game Engine applications. godot has no bugs, it has no vulnerabilities and it has medium support. However godot has a Non-SPDX License. You can download it from GitHub, GitLab.
Godot Engine – Multi-platform 2D and 3D game engine
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • godot has a medium active ecosystem.
  • It has 47538 star(s) with 8553 fork(s). There are 1429 watchers for this library.
  • There were 5 major release(s) in the last 12 months.
  • There are 5981 open issues and 26682 have been closed. On average issues are closed in 283 days. There are 1397 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of godot is 3.4.4-stable
godot Support
Best in #Game Engine
Average in #Game Engine
godot Support
Best in #Game Engine
Average in #Game Engine

quality kandi Quality

  • godot has 0 bugs and 0 code smells.
godot Quality
Best in #Game Engine
Average in #Game Engine
godot Quality
Best in #Game Engine
Average in #Game Engine

securitySecurity

  • godot has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • godot code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
godot Security
Best in #Game Engine
Average in #Game Engine
godot Security
Best in #Game Engine
Average in #Game Engine

license License

  • godot has a Non-SPDX License.
  • Non-SPDX licenses can be open source with a non SPDX compliant license, or non open source licenses, and you need to review them closely before use.
godot License
Best in #Game Engine
Average in #Game Engine
godot License
Best in #Game Engine
Average in #Game Engine

buildReuse

  • godot releases are available to install and integrate.
  • It has 88692 lines of code, 1050 functions and 1042 files.
  • It has high code complexity. Code complexity directly impacts maintainability of the code.
godot Reuse
Best in #Game Engine
Average in #Game Engine
godot Reuse
Best in #Game Engine
Average in #Game Engine
Top functions reviewed by kandi - BETA

Coming Soon for all Libraries!

Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.

godot Key Features

Godot Engine – Multi-platform 2D and 3D game engine

Godot Pong Replay After Every Game

copy iconCopydownload iconDownload
func reset_position():
    Physics2DServer.body_set_state(
        ball.get_rid(),
        Physics2DServer.BODY_STATE_TRANSFORM,
        Transform2D.IDENTITY.translated(Vector2(512, 300))
    )
extends RigidBody2D


var _teleport_target:Vector2
var _teleporting:bool


func _integrate_forces(state: Physics2DDirectBodyState) -> void:
    if _teleporting:
        state.transform.origin = _teleport_target
        _teleporting = false


func teleport(position:Vector2) -> void:
    _teleporting = true
    _teleport_target = position
func reset_position():
    ball.teleport(Vector2(512, 300))
-----------------------
func reset_position():
    Physics2DServer.body_set_state(
        ball.get_rid(),
        Physics2DServer.BODY_STATE_TRANSFORM,
        Transform2D.IDENTITY.translated(Vector2(512, 300))
    )
extends RigidBody2D


var _teleport_target:Vector2
var _teleporting:bool


func _integrate_forces(state: Physics2DDirectBodyState) -> void:
    if _teleporting:
        state.transform.origin = _teleport_target
        _teleporting = false


func teleport(position:Vector2) -> void:
    _teleporting = true
    _teleport_target = position
func reset_position():
    ball.teleport(Vector2(512, 300))
-----------------------
func reset_position():
    Physics2DServer.body_set_state(
        ball.get_rid(),
        Physics2DServer.BODY_STATE_TRANSFORM,
        Transform2D.IDENTITY.translated(Vector2(512, 300))
    )
extends RigidBody2D


var _teleport_target:Vector2
var _teleporting:bool


func _integrate_forces(state: Physics2DDirectBodyState) -> void:
    if _teleporting:
        state.transform.origin = _teleport_target
        _teleporting = false


func teleport(position:Vector2) -> void:
    _teleporting = true
    _teleport_target = position
func reset_position():
    ball.teleport(Vector2(512, 300))

Is there a `HVBoxContainer` for Godot?

copy iconCopydownload iconDownload
func _notification(what):
    if what == NOTIFICATION_SORT_CHILDREN:
        layout()
func layout() -> void:
    # …
    for child in get_children():
        var control := child as Control
        if not is_instance_valid(control) or not control.visible:
            continue

        var size := control.get_combined_minimum_size()
    # …
-----------------------
func _notification(what):
    if what == NOTIFICATION_SORT_CHILDREN:
        layout()
func layout() -> void:
    # …
    for child in get_children():
        var control := child as Control
        if not is_instance_valid(control) or not control.visible:
            continue

        var size := control.get_combined_minimum_size()
    # …

How do I check if two KinematicBody2D's are overlapping in Godot?

copy iconCopydownload iconDownload
if (
        event is InputEventScreenDrag
        or event is InputEventScreenTouch
        or event is InputEventMouse
    ):
        var viewport := get_viewport()
        var position:Vector2 = viewport.get_canvas_transform().affine_inverse().xform(event.position)
        var objects := get_world_2d().direct_space_state.intersect_point(position)
        for object in objects:
            print(object.collider)
for i in get_slide_count():
    var collision = get_slide_collision(i)
    print("Collided with: ", collision.collider.name)
var parameters := Physics2DShapeQueryParameters.new()
parameters.transform = global_transform
parameters.set_shape($CollisionShape2D.shape)
var results = get_world_2d().direct_space_state.intersect_shape(parameters)
for result in results:
    print(result.collider)
-----------------------
if (
        event is InputEventScreenDrag
        or event is InputEventScreenTouch
        or event is InputEventMouse
    ):
        var viewport := get_viewport()
        var position:Vector2 = viewport.get_canvas_transform().affine_inverse().xform(event.position)
        var objects := get_world_2d().direct_space_state.intersect_point(position)
        for object in objects:
            print(object.collider)
for i in get_slide_count():
    var collision = get_slide_collision(i)
    print("Collided with: ", collision.collider.name)
var parameters := Physics2DShapeQueryParameters.new()
parameters.transform = global_transform
parameters.set_shape($CollisionShape2D.shape)
var results = get_world_2d().direct_space_state.intersect_shape(parameters)
for result in results:
    print(result.collider)
-----------------------
if (
        event is InputEventScreenDrag
        or event is InputEventScreenTouch
        or event is InputEventMouse
    ):
        var viewport := get_viewport()
        var position:Vector2 = viewport.get_canvas_transform().affine_inverse().xform(event.position)
        var objects := get_world_2d().direct_space_state.intersect_point(position)
        for object in objects:
            print(object.collider)
for i in get_slide_count():
    var collision = get_slide_collision(i)
    print("Collided with: ", collision.collider.name)
var parameters := Physics2DShapeQueryParameters.new()
parameters.transform = global_transform
parameters.set_shape($CollisionShape2D.shape)
var results = get_world_2d().direct_space_state.intersect_shape(parameters)
for result in results:
    print(result.collider)

AnimationPlayer seek() using threading

copy iconCopydownload iconDownload
thread.call_deferred("wait_to_finish")
if thread == null:
    thread = Thread.new()
if thread == null or thread.is_active():
    thread = Thread.new()
tool
extends AnimationPlayer
export(float,0,1,0.1) var aniTimer = 0.0 setget aniTimer_Changed
var _thread:Thread

func sep(thread:Thread):
    seek(aniTimer, true)
    # calculations #
    thread.call_deferred("wait_to_finish")

func aniTimer_Changed(new_val):
    aniTimer=new_val
    if _thread == null or _thread.is_active():
        _thread = Thread.new()

    # warning-ignore:return_value_discarded
    _thread.start(self, "sep", _thread)
-----------------------
thread.call_deferred("wait_to_finish")
if thread == null:
    thread = Thread.new()
if thread == null or thread.is_active():
    thread = Thread.new()
tool
extends AnimationPlayer
export(float,0,1,0.1) var aniTimer = 0.0 setget aniTimer_Changed
var _thread:Thread

func sep(thread:Thread):
    seek(aniTimer, true)
    # calculations #
    thread.call_deferred("wait_to_finish")

func aniTimer_Changed(new_val):
    aniTimer=new_val
    if _thread == null or _thread.is_active():
        _thread = Thread.new()

    # warning-ignore:return_value_discarded
    _thread.start(self, "sep", _thread)
-----------------------
thread.call_deferred("wait_to_finish")
if thread == null:
    thread = Thread.new()
if thread == null or thread.is_active():
    thread = Thread.new()
tool
extends AnimationPlayer
export(float,0,1,0.1) var aniTimer = 0.0 setget aniTimer_Changed
var _thread:Thread

func sep(thread:Thread):
    seek(aniTimer, true)
    # calculations #
    thread.call_deferred("wait_to_finish")

func aniTimer_Changed(new_val):
    aniTimer=new_val
    if _thread == null or _thread.is_active():
        _thread = Thread.new()

    # warning-ignore:return_value_discarded
    _thread.start(self, "sep", _thread)
-----------------------
thread.call_deferred("wait_to_finish")
if thread == null:
    thread = Thread.new()
if thread == null or thread.is_active():
    thread = Thread.new()
tool
extends AnimationPlayer
export(float,0,1,0.1) var aniTimer = 0.0 setget aniTimer_Changed
var _thread:Thread

func sep(thread:Thread):
    seek(aniTimer, true)
    # calculations #
    thread.call_deferred("wait_to_finish")

func aniTimer_Changed(new_val):
    aniTimer=new_val
    if _thread == null or _thread.is_active():
        _thread = Thread.new()

    # warning-ignore:return_value_discarded
    _thread.start(self, "sep", _thread)

How to create a scalable multi-line button in Godot?

copy iconCopydownload iconDownload
var b := Button.new()
b.size_flags_vertical = 0
b.size_flags_horizontal = SIZE_EXPAND_FILL
add_child(b)

var l := RichTextLabel.new()
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
l.set_anchors_and_margins_preset(Control.PRESET_WIDE)
l.text = "Some\nMultiline\nText"
b.add_child(l)

yield(get_tree(), "idle_frame")
b.rect_min_size.y = l.get_content_height()
yield(get_tree(), "idle_frame")
b.rect_min_size.y = l.get_content_height()
-----------------------
var b := Button.new()
b.size_flags_vertical = 0
b.size_flags_horizontal = SIZE_EXPAND_FILL
add_child(b)

var l := RichTextLabel.new()
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
l.set_anchors_and_margins_preset(Control.PRESET_WIDE)
l.text = "Some\nMultiline\nText"
b.add_child(l)

yield(get_tree(), "idle_frame")
b.rect_min_size.y = l.get_content_height()
yield(get_tree(), "idle_frame")
b.rect_min_size.y = l.get_content_height()

How to change the XFade on a transition node in script?

copy iconCopydownload iconDownload
$AnimationTree.get("parameters/Transition/current")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var node := blend_tree.get("nodes/NodeName/node")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
transition.xfade_time = rand_range(0.0, 120.0)
$AnimationTree.tree_root.get("nodes/Transition/node").xfade_time = rand_range(0.0, 120.0)
-----------------------
$AnimationTree.get("parameters/Transition/current")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var node := blend_tree.get("nodes/NodeName/node")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
transition.xfade_time = rand_range(0.0, 120.0)
$AnimationTree.tree_root.get("nodes/Transition/node").xfade_time = rand_range(0.0, 120.0)
-----------------------
$AnimationTree.get("parameters/Transition/current")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var node := blend_tree.get("nodes/NodeName/node")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
transition.xfade_time = rand_range(0.0, 120.0)
$AnimationTree.tree_root.get("nodes/Transition/node").xfade_time = rand_range(0.0, 120.0)
-----------------------
$AnimationTree.get("parameters/Transition/current")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var node := blend_tree.get("nodes/NodeName/node")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
transition.xfade_time = rand_range(0.0, 120.0)
$AnimationTree.tree_root.get("nodes/Transition/node").xfade_time = rand_range(0.0, 120.0)
-----------------------
$AnimationTree.get("parameters/Transition/current")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var node := blend_tree.get("nodes/NodeName/node")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
transition.xfade_time = rand_range(0.0, 120.0)
$AnimationTree.tree_root.get("nodes/Transition/node").xfade_time = rand_range(0.0, 120.0)
-----------------------
$AnimationTree.get("parameters/Transition/current")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var node := blend_tree.get("nodes/NodeName/node")
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
var blend_tree := $AnimationTree.tree_root as AnimationNodeBlendTree
var transition := blend_tree.get("nodes/Transition/node") as AnimationNodeTransition
transition.xfade_time = rand_range(0.0, 120.0)
$AnimationTree.tree_root.get("nodes/Transition/node").xfade_time = rand_range(0.0, 120.0)

Godot inventory array, how to fill the first element

copy iconCopydownload iconDownload
my_array[index] = new_value
var index_of_null := my_array.find(null)
if index_of_null == -1:
    # null was not found
    print("the array is full") #or whatever
else:
    my_array[index_of_null] = new_value
-----------------------
my_array[index] = new_value
var index_of_null := my_array.find(null)
if index_of_null == -1:
    # null was not found
    print("the array is full") #or whatever
else:
    my_array[index_of_null] = new_value
-----------------------
for i in my_array.size():
    sorting func()

How do I make copies of a Node in Godot

copy iconCopydownload iconDownload
Node copynode = new Node();
copynode = this;
AddChild(copynode);
[Export]
public float Speed;
var scene = (PackedScene)ResourceLoader.Load("res://scenes/Scene.tscn");
Node scene_instance = ground.Instance();
AddChild(scene_instance);
-----------------------
Node copynode = new Node();
copynode = this;
AddChild(copynode);
[Export]
public float Speed;
var scene = (PackedScene)ResourceLoader.Load("res://scenes/Scene.tscn");
Node scene_instance = ground.Instance();
AddChild(scene_instance);
-----------------------
Node copynode = new Node();
copynode = this;
AddChild(copynode);
[Export]
public float Speed;
var scene = (PackedScene)ResourceLoader.Load("res://scenes/Scene.tscn");
Node scene_instance = ground.Instance();
AddChild(scene_instance);
-----------------------
Node copynode = new Node();
copynode = this;
AddChild(copynode);
[Export]
public float Speed;
var scene = (PackedScene)ResourceLoader.Load("res://scenes/Scene.tscn");
Node scene_instance = ground.Instance();
AddChild(scene_instance);
-----------------------
Node copynode = new Node();
copynode = this;
AddChild(copynode);
[Export]
public float Speed;
var scene = (PackedScene)ResourceLoader.Load("res://scenes/Scene.tscn");
Node scene_instance = ground.Instance();
AddChild(scene_instance);

Godot: Getting a node results in Nil Value

copy iconCopydownload iconDownload
export var duration_timer_path:NodePath
onready var duration_timer = get_node(duration_timer_path)
if not is_instance_valid(duration_timer):
    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)

# use duration_timer here
func get_duration_timer() -> Timer:
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = get_node("DurationTimer") # or use a NodePath
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)
    return duration_timer
func get_duration_timer() -> Timer:
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)
    return duration_timer
-----------------------
export var duration_timer_path:NodePath
onready var duration_timer = get_node(duration_timer_path)
if not is_instance_valid(duration_timer):
    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)

# use duration_timer here
func get_duration_timer() -> Timer:
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = get_node("DurationTimer") # or use a NodePath
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)
    return duration_timer
func get_duration_timer() -> Timer:
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)
    return duration_timer
-----------------------
export var duration_timer_path:NodePath
onready var duration_timer = get_node(duration_timer_path)
if not is_instance_valid(duration_timer):
    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)

# use duration_timer here
func get_duration_timer() -> Timer:
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = get_node("DurationTimer") # or use a NodePath
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)
    return duration_timer
func get_duration_timer() -> Timer:
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)
    return duration_timer
-----------------------
export var duration_timer_path:NodePath
onready var duration_timer = get_node(duration_timer_path)
if not is_instance_valid(duration_timer):
    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)

# use duration_timer here
func get_duration_timer() -> Timer:
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = get_node("DurationTimer") # or use a NodePath
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)
    return duration_timer
func get_duration_timer() -> Timer:
    if is_instance_valid(duration_timer):
        return duration_timer

    duration_timer = Timer.new()
    duration_timer.name = "DurationTimer"
    add_child(duration_timer)
    return duration_timer

Why doesn't my code use the correct animation?

copy iconCopydownload iconDownload
func _physics_process(_delta:float) -> void:
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, 0)
    move_and_collide(velocity * delta)
    var walking_speed := sign(
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    move_and_collide(velocity * delta)
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    velocity = move_and_slide(velocity)
    falling_speed = velocity.y
var state_machine = $AnimationTree.get("parameters/playback")
state_machine.travel("name_of_some_state")
var state:String = state_machine.get_current_node()
-----------------------
func _physics_process(_delta:float) -> void:
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, 0)
    move_and_collide(velocity * delta)
    var walking_speed := sign(
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    move_and_collide(velocity * delta)
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    velocity = move_and_slide(velocity)
    falling_speed = velocity.y
var state_machine = $AnimationTree.get("parameters/playback")
state_machine.travel("name_of_some_state")
var state:String = state_machine.get_current_node()
-----------------------
func _physics_process(_delta:float) -> void:
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, 0)
    move_and_collide(velocity * delta)
    var walking_speed := sign(
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    move_and_collide(velocity * delta)
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    velocity = move_and_slide(velocity)
    falling_speed = velocity.y
var state_machine = $AnimationTree.get("parameters/playback")
state_machine.travel("name_of_some_state")
var state:String = state_machine.get_current_node()
-----------------------
func _physics_process(_delta:float) -> void:
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, 0)
    move_and_collide(velocity * delta)
    var walking_speed := sign(
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    move_and_collide(velocity * delta)
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    velocity = move_and_slide(velocity)
    falling_speed = velocity.y
var state_machine = $AnimationTree.get("parameters/playback")
state_machine.travel("name_of_some_state")
var state:String = state_machine.get_current_node()
-----------------------
func _physics_process(_delta:float) -> void:
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, 0)
    move_and_collide(velocity * delta)
    var walking_speed := sign(
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    move_and_collide(velocity * delta)
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    velocity = move_and_slide(velocity)
    falling_speed = velocity.y
var state_machine = $AnimationTree.get("parameters/playback")
state_machine.travel("name_of_some_state")
var state:String = state_machine.get_current_node()
-----------------------
func _physics_process(_delta:float) -> void:
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, 0)
    move_and_collide(velocity * delta)
    var walking_speed := sign(
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    move_and_collide(velocity * delta)
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    velocity = move_and_slide(velocity)
    falling_speed = velocity.y
var state_machine = $AnimationTree.get("parameters/playback")
state_machine.travel("name_of_some_state")
var state:String = state_machine.get_current_node()
-----------------------
func _physics_process(_delta:float) -> void:
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, 0)
    move_and_collide(velocity * delta)
    var walking_speed := sign(
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    move_and_collide(velocity * delta)
var gravitational_acceleration := 100 # pixels per second squared
var falling_speed := 0

func _physics_process(_delta:float) -> void:
    falling_speed += gravitational_acceleration * delta
    var walking_speed := (
        Input.get_action_strength("ui_right") -
        Input.get_action_strength("ui_left")
    )
    anim.flip_h = walking_speed < 0
    var velocity := Vector2(walking_speed, falling_speed)
    velocity = move_and_slide(velocity)
    falling_speed = velocity.y
var state_machine = $AnimationTree.get("parameters/playback")
state_machine.travel("name_of_some_state")
var state:String = state_machine.get_current_node()

Community Discussions

Trending Discussions on godot
  • Godot Pong Replay After Every Game
  • Is there a `HVBoxContainer` for Godot?
  • How do I check if two KinematicBody2D's are overlapping in Godot?
  • How do you pass data into a chosen scene
  • AnimationPlayer seek() using threading
  • How can I fix all my area2D functions in Godot to not run on startup?
  • Gaps and dislocations when connecting sprites
  • Camera controller with fly mode in Godot
  • How to create a scalable multi-line button in Godot?
  • Godot odd behavior with on_body_entered and on_body_exited
Trending Discussions on godot

QUESTION

Godot Pong Replay After Every Game

Asked 2022-Apr-17 at 00:06

I'm currently working on my Pong game project using Godot.

Everything is working well but there's only one problem.

After one side gets scored, the ball is stuck in the middle and not working.

What should I add? Here is my code:

# Declare member variables here.
var ball
var player
var computer
var player_score = 0
var computer_score = 0
var winning_score = 5
var initial_velocity_x = 250
var initial_velocity_y = 10

# Called when the node enters the scene tree for the first time.
func _ready():
    player = get_node('player')
    computer = get_node('computer')
    ball = get_node('ball')


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    player.position.y = get_viewport().get_mouse_position().y
    computer.position.y = ball.position.y
    
    if ball.position.x > 1024:
        reset_position()
        ball.linear_velocity.x = initial_velocity_x
        ball.linear_velocity.y = initial_velocity_y
        computer_score += 1
        
    if ball.position.x < 0:
        reset_position()
        ball.linear_velocity.x = -initial_velocity_x
        ball.linear_velocity.y = initial_velocity_y
        player_score += 1
    
    if player_score >= winning_score or computer_score >= winning_score:
        player_score = 0
        computer_score = 0
    
func reset_position():
    ball.position.x = 512
    ball.position.y = 300

ANSWER

Answered 2022-Apr-17 at 00:06

I'm guessing the ball is a RigidBody2D.

They are intended to be moved by the physics engine, and moving them directly (e.g. setting position) can cause problems. This one of the reason I often recommend beginners to use KinematicBody(2D), so they are not "fighting" the physics engine.

Anyway, that is a cop-out answer. You want to teleport a RigidBody2D, let us see how to do it.


The following is - in my opinion - the elusive proper way to teleport a RigidBody2D:

func reset_position():
    Physics2DServer.body_set_state(
        ball.get_rid(),
        Physics2DServer.BODY_STATE_TRANSFORM,
        Transform2D.IDENTITY.translated(Vector2(512, 300))
    )

You are basically saying: "Hey, Physics Engine! Yes, you. I want you to place the body at this position." And the Physics Engines goes: "OK".

The Physics2DServer API is intended for advanced use, when using nodes won't do. And as such, not beginner friendly.


Instead, the most common solution to teleport a RigidBody2D involves adding an script to it. It looks like this:

extends RigidBody2D


var _teleport_target:Vector2
var _teleporting:bool


func _integrate_forces(state: Physics2DDirectBodyState) -> void:
    if _teleporting:
        state.transform.origin = _teleport_target
        _teleporting = false


func teleport(position:Vector2) -> void:
    _teleporting = true
    _teleport_target = position

And then you use it like this:

func reset_position():
    ball.teleport(Vector2(512, 300))

Here you have told the Physics Engine to ask you every time if you want to change something (it will call _integrate_forces if you define it). And occasionally you answer by telling it to move the body.

Source https://stackoverflow.com/questions/71898123

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install godot

You can download it from GitHub, GitLab.

Support

Godot is completely free and open source under the very permissive MIT license. No strings attached, no royalties, nothing. The users' games are theirs, down to the last line of engine code. Godot's development is fully independent and community-driven, empowering users to help shape their engine to match their expectations. It is supported by the Software Freedom Conservancy not-for-profit. Before being open sourced in February 2014, Godot had been developed by Juan Linietsky and Ariel Manzur (both still maintaining the project) for several years as an in-house engine, used to publish several work-for-hire titles.

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

Explore Related Topics

Share this Page

share link
Reuse Pre-built Kits with godot
Compare Game Engine Libraries with Highest Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.