Raycast From Custom Behavior

How do I Raycast From Custom Behavior

I am currently trying to use the Raycast ‘condition’ to check for line of sight with an object. Normally this would be a walk in the park, but I am trying to do it in a custom behavior. As some of you may know, the raycast condition requires scene variables to store the hit position in, but so far I have been unable to find a way to access those variables.

What is the expected result

The object should be able to see the target if it has LOS and is in range. I have tested range and know for sure it works, so that is not the issue. It appears the main problem is the scene variables are not being updated, because their values are still (-1,-1), meaning nothing has been hit. Those values are a hard-coded position off the map.

What is the actual result

As mentioned previously, the scene variables are simply not updating.

Related screenshots

Code:

Behavior parameters:


*Please note that the behavior is ‘Target Selection’, and the icon is a crosshair

Any advice or general help is appreciated!

This might be a bit of a stretch- have you defined the variables hitX and hitY in the scene editor?

1 Like

Even if he hasn’t, it still should be setting to -1 in the first event?

Maybe throw a few debug logs in each event step to see where it’s catching. At least then you might be able to narrow down where it’s failing.

It does set the values to (-1,-1) (last half of the "What is the expected result" section), unless that’s also being done in the main game events. However, I’m trying to remove the possibility that the raycast isn’t aware of scene variables in a custom behaviour if they haven’t been explicitly declared.

I agree with @BigChalkGames comment of putting in a few console log statements and checking it in the debugger. It’s old skool, but it’s very effective.

1 Like

This might be a bit of a stretch- have you defined the variables hitX and hitY in the scene editor?

Yes, they are for sure defined

Even if he hasn’t, it still should be setting to -1 in the first event?

Maybe throw a few debug logs in each event step to see where it’s catching. At least then you might be able to narrow down where it’s failing.

I am using the animation frames of the object to determine where it is failing. The object is normally white, turns green if has LOS, and red if not.

Currently the object is always turning red. (This also has nothing to do with animation speed. The frames change every 1E37 seconds, so I doubt that’s an issue.)

The code is always reaching the red arrow in the image.

The blue is showing that I am creating an object at the location of the hit position, or the scene variables I am trying to assign.

It does set the values to (-1,-1) (last half of the "What is the expected result" section), unless that’s also being done in the main game events. However, I’m trying to remove the possibility that the raycast isn’t aware of scene variables in a custom behaviour if they haven’t been explicitly declared.

I should probably make it clear that while the scene variables are defined, they are only defined in the actual scene (I have not found any way to make true scene variables within a behavior, and regular variables are not accepted in the raycast function). If you have a way of doing this, that would immediately fix this issue.

Thank you @MrMen and @BigChalkGames for your help so far!

How have you determined that? Sorry for the question, but I want to ensure Targets actually has objects, and that the looping is happening.

As @BigChalkGames suggested, can you put a console log statement in there to show the values of hitX & hitY, and maybe the position of the loop’s current Target?

Kinda hard to see it, but I responded to that in my last post:

I am using the animation frames of the object to determine where it is failing. The object is normally white, turns green if has LOS, and red if not.

Currently the object is always turning red.

The code that turns the object green is circled in green in my code, while changing it to red is circled in red.

The scene variable is being debugged by creating an object based on the scene variables.

The blue is showing that I am creating an object at the location of the hit position, or the scene variables I am trying to assign.

Ok, wasn’t aware of the animation and colours.

Have you checked how many obstacles are hit with the raycast?

Sorry, I’m struggling to figure out where the problem is occurring. I’m getting the feeling that it’s with the raycast/GDevelop.

I am not quite sure. Whilst I haven’t played around much with raycasting, I (think) was able to get a simplified version of reallybasicgames’s raycast logic working just in an event and as a behavior with object groups (Red beam is if obstacle in the way).
Space: The behavior version
V: The events version.

Checking a group of enemies, a group of players and a group of obstacles.

I could be totally off the mark here though :sweat_smile:

I think this is what I am looking for. Would you mind sharing that project or screenshots with me so I can try to compare it with my code? If this is truly working in a custom extension then the problem must be with my logic.

Once again I appreciate both of your help!

Here you go: https://file.io/C0fsWewh7sb6 (2 week expiry).

I hope it helps :sweat_smile: