Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Peppe |
I have four scenes in my project: the main scene, the platform scene(area2d,collisionshape, position1 and 2, staticbody2d(collisionshape and sprite) ) , the missile scene(kinematicbody2d, collisionshape, sprite) and the spaceship scene(kinematicbody2d,collisionshape, sprite) . I have only two scripts in the project: one is attached to the root node of the main scene and to this script I connected the body_entered() and body_exited() signal from the platform scene, plus there is another function that randomize the positions of the vector, and the other script in the spaceship scene. The problem comes when I start the game and when I move the spaceship to the top, the platform instantiates missiles (when the spaceship gets close enough the platform creates missiles that at the moment do nothing) and at a certain point the game crashes and I get the errors: “body set shape disabled” and “body set shape as one way collision”. I tried using the call_defferred() function to add a missile to each position but it didn’t work. How can I solve this problem?
Code of MAIN SCENE:
extends Node
var boolean = false
var vecPositions
var randomPosition
var MissileInstance
var MissileScene = preload("res://scenes/Missile.tscn")
var random = RandomNumberGenerator.new()
func random_Position(array : Array):
random.randomize()
var randPosition = array[random.randi() % array.size()]
return randPosition
func _on_Platform_body_entered(body):
if body is KinematicBody2D:
boolean = true
while(boolean):
vecPositions = [$Platform/Position1,$Platform/Position2]
randomPosition = random_Position(vecPositions)
if randomPosition == $Platform/Position1:
MissileInstance = MissileScene.instance()
$Platform/Position1.add_child(MissileInstance)
elif randomPosition == $Platform/Position2:
MissileInstance = MissileScene.instance()
$Platform/Position2.add_child(MissileInstance)
yield(get_tree().create_timer(3),"timeout")
func _on_Platform_body_exited(body):
if body is KinematicBody2D:
boolean = false
Code of SPACESHIP SCENE:
extends KinematicBody2D
var target
var click = false
var dir
var speed = 200
var collision
func _input(event):
if event is InputEventMouseButton:
target = get_global_mouse_position()
click = true
func _physics_process(delta):
if click:
dir = position.direction_to(target)
collision = move_and_collide(dir * speed * delta)
if collision:
pass
How’s the CollisionShape node set up in the Missile scene? Is it marked as “one way collision”
Experiment using these commands before adding the missile as a child, see if anything changes:
MissileInstance.one_way_collision = false
MissileInstance.disabled = false
MisterMano | 2021-08-25 04:30
Actually now I changed the name of MissileInstance, now it is “missile” and I moved the script that was in “main scene” to the “platform” scene specifically on the “Platform(root Node of the platform scene)” object. However, the “missile” variable refers to the missile object scene and not directly to the Collision object. To do as you have written the scene (or the object) that I have indicated in the script of the Platform that is “missile” must be a child of “Platform”, in this way I could access the object and then the property and do as you have written but even in that case you would not help me because the object “missile” would be in the scene and I would not have concluded anything because my goal is to instantiate it and not create it in the scene.
Peppe | 2021-08-25 06:57
Do note that the suggestion was to change the parameters of the instanced object you created. You don’t need to add it as a child to a scene to change its parameters, you can do that before, which is what I suggested.
MisterMano | 2021-08-25 14:31
Yes but when I type "missile. " I don’t get the “one_way_collision” code suggestion or the “disabled” property suggestion. This is what I meant when I wrote above.
Peppe | 2021-08-25 16:08
Try missile.get_node("CollisionShape")
or however the collision node is named, that should give you access to those properties for the instance.
MisterMano | 2021-08-25 16:51
It works as you said but the missile problem persists with these errors. I tried putting in one of the two instructions you wrote above but nothing, then I put them both in before adding as a child the missile:
extends StaticBody2D
var trigger
var missileScene = preload(“res://scenes/Missile.tscn”)
var missilefunc _on_DangerArea_body_entered(body):
if body is KinematicBody2D:
trigger = true
while(trigger):
missile = missileScene.instance()
missile.get_node(“CollisionShape”).one_way_collision = false
missile.get_node(“CollisionShape”).disabled = false
$SpawnPosition.add_child(missile)
yield(get_tree().create_timer(3),“timeout”)func _on_DangerArea_body_exited(body):
if body is KinematicBody2D:
trigger = false
Peppe | 2021-08-25 17:14