Godot Version
4
Question
I want when the game start to set a characterbody2d position to a another characterbody2d position
func _ready() position = characterbody2d.position
i tried that code but it doenst work pls help.
4
I want when the game start to set a characterbody2d position to a another characterbody2d position
func _ready() position = characterbody2d.position
i tried that code but it doenst work pls help.
You need to put “:” after you declare a method:
func _ready():
position = characterbody2d.position
You also need a variable called “characterbody2d”
i just forgot to put it there. the code still dont work with “:” and i already have those variable
Have you tried “global_position”?
func _ready():
global_position = characterbody2d.global_position
And if that also doesnt work, can you show your scenetree?
nope the same as position. the error say “invalid get Index ‘global_position’(on base: null instance)”
That means characterbody2d is null. Where do you set the value of characterbody2d?
`extends CharacterBody2D
@onready var cow_boy = $Character/CowBoy
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var expcoll = $expcoll
var speed = 300
var click_pos = Vector2()
var target_pos = Vector2()
func _ready():
click_pos = position
expcoll.disabled = true
global_position = cow_boy.global_position`
this is the code. The bomb scene(the scene with this script) is not in the treescene.
Can you show the scenetree of this bomb?
You are trying to access the cowboy with @onready, but cowboy is not a child of the bomb. You should only use @onready with children.
Does the cowboy spawn the bombs? If yes can you show the code for that?
if Input.is_action_just_pressed("attk"): #NORTH FACING
if is_touch == true:
get_parent().add_child(bomb)
bomb.position = position
try
bomb.global_position = global_position
Where do you instantiate the bomb?
i instantiate the bomb in the character script not in the bomb
Yes obviously. Whats currently happening when you try to spawn a bomb?
uhm so i click the mouse so that the bomb spawn and its schould go to mouse cursor position and its work. But the first thing the bomb do when its spawn is the bomb go to the position where the mouse cursor not is. Idk how to explain ima send u a vid
Whats the complete code for the bomb?
I think you have to rewrite your bomb_spawn, since you probably want a new bomb everytime you press attack:
var bomb_scene = load("path/to/bomb/scene")
if Input.is_action_just_pressed("attk"): #NORTH FACING
if is_touch == true:
var bomb = bomb_scene.instantiate()
bomb.position = position
get_parent().add_child(bomb)
extends CharacterBody2D
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var expcoll = $expcoll
var cow_boy = get_tree().get_first_node_in_group("Player")
var speed = 300
var click_pos = Vector2()
var target_pos = Vector2()
func _ready():
click_pos = position
expcoll.disabled = true
position = cow_boy.position
func _physics_process(delta):
if Input.is_action_just_pressed("attk"):
click_pos = get_global_mouse_position()
if position.distance_to(click_pos) > 3:
target_pos = (click_pos - position).normalized()
velocity = target_pos * speed
move_and_slide()
if Input.is_action_just_pressed("skill"):
expcoll.disabled = false
animated_sprite_2d.play("exploding")
await get_tree().create_timer(0.5).timeout
queue_free()
this is the whole bomb script
Try this script for spawning the bomb:
var bomb_scene = load("path/to/bomb/scene")
func _input(event):
if Input.is_action_just_pressed("attk"): #NORTH FACING
if is_touch == true:
var bomb = bomb_scene.instantiate()
bomb.click_position = get_global_mouse_position()
bomb.position = position
get_parent().add_child(bomb)
And use this script for the bomb:
extends CharacterBody2D
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var expcoll = $expcoll
var speed = 300
var click_pos = Vector2()
var target_pos = Vector2()
func _ready():
expcoll.disabled = true
func _physics_process(delta):
if position.distance_to(click_pos) > 3:
target_pos = (click_pos - position).normalized()
velocity = target_pos * speed
move_and_slide()
if Input.is_action_just_pressed("skill"):
expcoll.disabled = false
animated_sprite_2d.play("exploding")
await get_tree().create_timer(0.5).timeout
queue_free()
You can just drag your scene from the filemanager into the load( ) ← brackets
Its workkk thanks you so muchh