0 votes

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 bodyentered() and bodyexited() 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
Godot version 3.2.3
in Engine by (54 points)

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

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.

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.

Yes but when I type "missile. " I don't get the "onewaycollision" code suggestion or the "disabled" property suggestion. This is what I meant when I wrote above.

Try missile.get_node("CollisionShape") or however the collision node is named, that should give you access to those properties for the instance.

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 missile

func onDangerAreabodyentered(body):

if body is KinematicBody2D:
trigger = true
while(trigger):
missile = missileScene.instance()
missile.getnode("CollisionShape").onewaycollision = false
missile.get
node("CollisionShape").disabled = false
$SpawnPosition.addchild(missile)
yield(get
tree().create_timer(3),"timeout")

func onDangerAreabodyexited(body):

if body is KinematicBody2D:
trigger = false

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.