So, I have this simple scene setup. It was made for debugging only:
What I am trying to do is, as the title says, add only one of the child nodes shown to an array. I’ve made it so both FirstBody and SecondBody are next to each other like this:
Only the SecondBody, which has the raycast not interacting with anything, should be added to the array. Yet both of them appear in there. Here is the code for first and second body, which are just instances of the scene which has this script attached. I know I am adding only the name, not the reference to the instance itself, but the behaviour is the same. dataManage is just an autoload which initializes the string array:
using Godot;
using System;
public partial class character_body_2d : CharacterBody2D
{
RayCast2D ray;
bool added = false;
public override void _Ready()
{
base._Ready();
ray = GetNode<RayCast2D>("RayCast2D");
}
public override void _PhysicsProcess(double delta)
{
if (!ray.IsColliding()) {
GD.Print($"The one that isn't colliding is {Name}");
Modulate = Colors.Green;
if (!added) {
added = true;
dataManage.Things.Add(Name);
}
}
else {
Modulate = Colors.Red;
GD.Print($"The one that IS colliding is {this.Name}");
}
Velocity = new Vector2(0, 1) * 10;
MoveAndSlide();
}
}
I am applying the same code I made in a larger project which has a dynamic size of enemies. The idea was to send a signal every time an event happened and have an array of specific enemy instances so I can manage them quickly
Thanks, but I’ve tried that too, it didn’t work :(. Same thing happened, with every instance being added to the group. I tinkered with the code for a lotta time and arrived at the conclusion that’s just how groups work. But since the same is happening here, thought the issue might be something else
Could you show us a video of the bug? Also, maybe add an print to print the bodys the raycast is colliding with and enable seeing raycasts while debugging. These will all help us help you.
well, i’ve gone ahead and decided to zip it. i also added the scripts in gdscript, since it’s such a small project. The project already has the debug statements (just a bunch of prints) and the collision shapes visible, so just running it should give enough information. if anyone’s interested on taking a look, there it is. thank you for even reading the post, regardless!
I trying understand what your propose in this,
There’s prints in your code but you don’t show any logs.
if both object appears in your list that mean all requirements was “true” for any _PhysicsProcess
Only one of the objects should appear in the list, that is the problem. It should be impossible for FirstBody to be in there if its raycast is colliding with something.
that what i expected first frame raycast not colliding he needed more time @prynpo
In _Ready disabled _PhysicsProcess and enable with tween after 1 second
that’s allow me wait for raycast be warmup and start. That allow me skip checking is first _PhysicsProcess
public override void _Ready()
{
base._Ready();
ray = GetNode<RayCast2D>("RayCast2D");
SetPhysicsProcess(false);
Tween tween = CreateTween();
_ = tween.TweenCallback(Callable.From(() => SetPhysicsProcess(true))).SetDelay(1f);
}
alternative you make raycast from script not node and get collision from first call.