Have tried for 1 hour, but cant get the right code to call the raycast function.
How do i call it properly. Bricks are my gameobjects that are scanned for.
No enemy ever hits a brick, which seems not possible, as there are plenty between enemy and player.
const player = runtimeScene.getObjects("Player")[0];
const enemies = runtimeScene.getObjects("Enemy");
var bricks = runtimeScene.getObjects("Brick");
enemies.forEach((enemy, index) => {
var hitx=0;
var hity=0;
// Perform raycast - returns true if hit an obstacle
var hasObstacle = false;
hasObstacle = gdjs.evtTools.object.raycastObjectToPosition(
bricks,
enemy.getX(),
enemy.getY(),
player.getX(),
player.getY(),
hitx,
hity,
false
);
if (hasObstacle) {
console.log("EN:"+enemy.getX()+" "+hitx+"/"+hity);
}
}
i have a complex movement behavior, that uses vectors to calculate force for enemy movement. It swarms the player, yet now i want to check if any real sight is given for an enemy towards player, if not (raycast hits something) i want a slightly different behavior that tries to navigate around obstacles.
now i share a bit what i tried towards js:
1.this code somehow works, it gives a hitpoint back in hasObstacle, yet i find it hard to eval, what object it hit. (in js)
let hasObstacle = enemy.raycastTest(enemy.getX(),enemy.getY(),player.getX(),player.getY(),false);
2.this code doesnt work. yet the ray start and end are correct, as i visualize them with a drawing.
// Set Objects
const player = runtimeScene.getObjects("Player")[0];
const enemies = runtimeScene.getObjects("Enemy");
const bricks = runtimeScene.getObjects("Brick");
enemies.forEach((enemy, index) => {
// Visualize Ray
const shapePainter = runtimeScene.getObjects("LaserDrawer")[0];
shapePainter.drawLineV2(enemy.getX(),enemy.getY(),player.getX(),player.getY(), 4);
// Execute raycast
const raycastResult = gdjs.evtTools.object.raycastObjectToPosition(
bricks, // Object type to detect
enemy.getX(), enemy.getY(), // Start position
player.getX(),player.getY(),// End position
false // inverse
);
// Process results
if (raycastResult.length > 0) {
const hitObject = raycastResult[0];
console.log("Hit object at:", hitObject.getX(), hitObject.getY());
// Get hit position (approximate)
const hitX = hitObject.getX();
const hitY = hitObject.getY();
// Debug visualization (optional)
shapePainter.drawCircle(hitX, hitY, 10);
}
}
Still dont know whats wrong, as it doesnt throw errors.
Just recorded the difference between js and event driven raycast. You can see, what i try to accomplish, yet Javascript is not giving intended results.
Thanks for the hints, again its not very clear how to obtain these input parameters. Do you have any clear example how to call the function? The following will lead to errors as eventsFunctionContext is “unkown” but suggested as valid when typing js code.
And an ambigious functions description comes from hovering the code. I read, that it is a hashtable of runtimeObjectArray. Thats why i tried with hashtable too, no success. No error either.
I thought you meant that you’re doing a custom behavior in an extension. You should only use JS in extensions. Putting JS directly in scene events is against good practices and will quickly lead to spaghetti code.
I can do javascript in an extension or in a function. Yet i fear, it wont solve the problem with the raycast, or will the script work?
Am still elaborating, why there are no error messages and no valid results.
Isnt there an example, that provides a proper call?