unable to right to a variable from another script in Godot - godot

Trying to access variable gravity in this script
extends KinematicBody2D
class_name Actor
export var speed: = Vector2(300.0, 1000.0)
export var gravity = 3000.0
var velocity: = Vector2.ZERO
func _physics_process(delta: float) -> void:
velocity.y += gravity*delta
#velocity.y = max(velocity.y, speed.y)
velocity = move_and_slide(velocity)
from this script
extends Actor
func _physics_process(delta: float) -> void:
var direction = Vector2(
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),0.0
)
velocity =
I get the error Unexpected token: Identifier:velocity
am I using class_name incorrectly?

You can access both velocity and gravity variables from base class, but you need to do it within a function, e.g.:
extends Actor
func _physics_process(delta: float) -> void:
velocity += Vector2.ZERO # just an example

Related

Player not looking in the right direction

I have a player that you can move by clicking in a location. They are guided by pathfinding to also move around any potential obstacles in their way. Here is my script:
extends KinematicBody2D
export var speed = 200
var velocity = Vector2.ZERO
onready var navigation_agent = $NavigationAgent2D
func _ready():
navigation_agent.connect("velocity_computed", self, "move")
func _input(event):
if event.is_action_pressed("mouse_right"):
navigation_agent.set_target_location(event.get_global_position())
func _process(_delta):
if $RayCast2D.is_colliding():
if $RayCast2D.get_collider().is_in_group("enemy_ground_troop"):
self.velocity = Vector2.ZERO
ranged_attack()
if navigation_agent.is_navigation_finished():
return
var overlapping_areas = $EnemyDetector.get_overlapping_areas()
for area in overlapping_areas:
if area.is_in_group("enemy_ground_troop"):
navigation_agent.set_target_location(area.get_global_position())
look_at(area.get_global_position())
velocity = global_position.direction_to(navigation_agent.get_next_location()) * speed
look_at(global_position.direction_to(navigation_agent.get_next_location()))
$RayCast2D.global_rotation = self.global_rotation
navigation_agent.set_velocity(velocity)
func move(velocity):
velocity = move_and_slide(velocity)
func ranged_attack():
print_debug("fired ranged attack")
When I run the scene, the player is not looking where I want them too based on the look at commands. How can I fix this?
The look_at method takes a target position, not a direction.
So, instead of this:
look_at(global_position.direction_to(navigation_agent.get_next_location()))
Try this:
look_at(navigation_agent.get_next_location())

Invalid set index 'value' (on base: 'null instance') with value type of float

I have a game I'm making in Godot, 2D
I have a progres sbar called "Healthbar"
I'm trying to set it's value to the players HP value
the full code is as follows
extends KinematicBody2D
var health: float = 100
func ready():
pass
export var movespeed : int
export var batteryspeed: int
var battery = preload("res://Bullet.tscn")
func _physics_process(delta):
get_tree().get_root().find_node("HealthBar").value = health
var motion = Vector2()
if (health <= 0):
gameover()
if(Input.is_action_pressed("MoveUp")):
motion.y -= 1
if(Input.is_action_pressed("MoveLeft")):
motion.x -= 1
if(Input.is_action_pressed("MoveDown")):
motion.y += 1
if(Input.is_action_pressed("MoveRight")):
motion.x += 1
motion = motion.normalized()
motion = move_and_slide(motion * movespeed)
if(Input.is_action_just_pressed("Fire")):
fire()
look_at(get_global_mouse_position())
func fire():
var batteryInstance = battery.instance()
batteryInstance.position = position
batteryInstance.rotation_degrees = rotation_degrees
batteryInstance.apply_impulse(Vector2(), Vector2(batteryspeed, 0).rotated(rotation))
get_tree().get_root().call_deferred("add_child", batteryInstance)
func gameover():
get_tree().reload_current_scene()
func _on_Area2D_body_entered(body):
if "Enemy" in body.name:
health -= 10
and the part I'm having issues with, I suspect is
get_tree().get_root().find_node("HealthBar").value = health
What do I do to set the progressbar value to the health variable?
It turns out you must use the function
{Progress Bar}.set_value({value})

Kinematic Body 2D not moving

I'm trying to game a game using the Godot Engine but I'm stuck at the beginning! I can't make my KinematicBody2D move!
This is my Player.GD script
extends KinematicBody2D
var velocity = Vector2.ZERO
var move_speed = 480
var gravity = 1200
var jump_force = -720
var right = Input.is_action_pressed("move_right")
var left = Input.is_action_pressed("move_left")
var jump = Input.is_action_pressed("jump")
func _ready():
pass
func _physics_process(_delta):
var move_direction = int(right) - int(left)
velocity.x = move_speed * move_direction
move_and_collide(velocity)
Can someone, please, help me?
All this code will run when the KinematicBody2D is initialized:
var velocity = Vector2.ZERO
var move_speed = 480
var gravity = 1200
var jump_force = -720
var right = Input.is_action_pressed("move_right")
var left = Input.is_action_pressed("move_left")
var jump = Input.is_action_pressed("jump")
In consequence, it will not be taking input in real time. Instead you want the last three lines here:
func _physics_process(_delta):
var right = Input.is_action_pressed("move_right")
var left = Input.is_action_pressed("move_left")
var jump = Input.is_action_pressed("jump")
# …
Those are boolean, by the way. You can get a float from 0.0 to 1.0 if you use Input.get_action_strength instead. Which will also let your code ready for analog input.
I also want to point out that move_and_collide does not take a velocity, but a displacement vector. So to call it correctly, you want to multiply the velocity by delta:
func _physics_process(delta):
# …
move_and_collide(velocity * delta)
Or use move_and_slide, which does take a velocity. By the way, the up parameter that move_and_slide takes is to discern between floor, ceiling, and walls. Without, everything is considered a wall.

Identifier isn't declared in the current scope in godot

Code for the coin to make a score:
extends Area2D
onready var anim_player: AnimationPlayer = get_node("AnimationPlayer")
func _on_body_entered(body: PhysicsBody2D) -> void:
picked()
func picked() -> void:
PlayerData.Score += score
anim_player.play("Fade")
It isn't working in coin but works in enemy(code):
extends "res://Code/Both Script.gd"
onready var Stomp_Thingy: Area2D = $Stomp_Thingy
export var score: = 100
func _ready() -> void:
set_physics_process(false)
_Velocity.x = -Speed.x
func _on_Stomp_Thingy_body_entered(body: PhysicsBody2D) -> void:
if body.global_position.y > get_node("Stomp Thingy").global_position.y:
return
get_node("CollisionShape2D").disabled = true
queue_free()
func _physics_process(delta: float) -> void:
_Velocity.y += gravity * delta
if is_on_wall():
_Velocity.x *= -1.0
_Velocity.y = move_and_slide(_Velocity, FLOOR_NORMAL).y
func die() -> void:
queue_free()enter code here
PlayerData.Score += score
In script for coin, i think "score" isn't declared. So you can add "var score = 100" to that script. Or use autoload if you want score same on enemy and coin. Include more information.

How To Make KinematicBody2D Move By Drag in One Direction in GDScript?

I wanted to create a game similar to this (Slyway), but I had a problem with the child's movement, so I don't know what to use to do the movement during the draw. Is InputEventScreenTouch Or InputEventScreenDrag, all I come up with is this code that isn't working
extends KinematicBody2D
var velocity = Vector2.ZERO
var direction = Vector2.ZERO
var speed = 200
func input(event):
if event is InputEventScreenTouch:
if event.ispressed():
direction.x -= 1
func physicsprocess(delta):
if velocity.length() == 0:
_input(event)
velocity = Vector2.ZERO
velocity += direction * speed
velocity = moveand_slide (velocity)

Resources