Anyone know how to get the Objects in a Group using Java Script? GDevelop object groups that is

Like the title says, iv been trying to find out how to get the objects from a group in gdevelop to try and make an object culling extension.

I keep working with ChatGPT to try and find how, but its not going very well :frowning:

If i could apply the script im using for individual object to a group instead, that would turn it into an awesome tool that anyone could use.

The way its now is only specific to the object its referencing.

The script

// Initialize the deletedObjects array if it doesn't exist
runtimeScene.deletedObjects = runtimeScene.deletedObjects || [];

// Get the camera position and size
let cameraX = runtimeScene.getLayer("").getCameraX();
let cameraY = runtimeScene.getLayer("").getCameraY();
let cameraWidth = runtimeScene.getLayer("").getCameraWidth();
let cameraHeight = runtimeScene.getLayer("").getCameraHeight();

// Calculate the boundaries for object culling (600 pixels outside the camera view)
let leftBoundary = cameraX - cameraWidth / 2 - 600;
let rightBoundary = cameraX + cameraWidth / 2 + 600;
let topBoundary = cameraY - cameraHeight / 2 - 600;
let bottomBoundary = cameraY + cameraHeight / 2 + 600;

// Get all objects of type 'Tiles'
let tilesObjects = runtimeScene.getObjects("Tiles");

// Iterate over each object of type 'Tiles'
tilesObjects.forEach(object => {
    let objectX = object.getX();
    let objectY = object.getY();
    
    // Check if the object is outside the culling boundaries
    if (objectX < leftBoundary || objectX > rightBoundary || objectY < topBoundary || objectY > bottomBoundary) {
        // Store the position to recreate later
        runtimeScene.deletedObjects.push({
            x: objectX,
            y: objectY,
            name: "Tiles"
        });
        
        // Delete the object
        object.deleteFromScene(runtimeScene);
    }
});

// Recreate objects within the recreation boundaries
runtimeScene.deletedObjects = runtimeScene.deletedObjects.filter(function(data) {
    let objectX = data.x;
    let objectY = data.y;

    // Check if the position is within the recreation boundaries
    if (objectX >= leftBoundary && objectX <= rightBoundary && objectY >= topBoundary && objectY <= bottomBoundary) {
        // Recreate the object
        let newObject = runtimeScene.createObject("Tiles");
        newObject.setPosition(objectX, objectY);
        return false; // Remove from deletedObjects after recreation
    }

    return true; // Keep in deletedObjects if not recreated
});

I don’t know if it’s changed since then but I don’t think you can.

The concat method works. I like that.

let obj = runtimeScene.getObjects("NewSpriteA").concat(runtimeScene.getObjects("NewSpriteB"))

EDIT: Groups do work with extensions because you’re passing off the picked objects and the code uses the extension parameter name not the Group name. The calling event uses the group name.

You need to setup a parameter in a function and then get the objects through that parameter.

This rotates a random object using the function parameter OBJ. It doesn’t matter if it’s an object or a group.

const obj = eventsFunctionContext.getObjects("OBJ");
obj[Math.trunc(Math.random()*obj.length)].setAngle(Math.random()*360)
1 Like

I tried feeding the more info but it didnt help…

I dont know the first thing about scripting, so im not help…

@Keith_1357 dont supose you know how to just simple retrive a list of objects currently in the scene?

If i know that, i could just tell it to mod the script to cull all objects instead of a specific group, that would work too!

I’m not at my PC.

https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/modules/gdjs.html

It might be under runtime
https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/classes/gdjs.RuntimeScene.html

1 Like

Thanks for trying!

Iv been at it for a couple ours trying to work this out, but this is above my head, so ill just take the win that i was able to implement culling on my project and be happy.

Would be really cool if i could work out how to give this stuff to everyone, but this is the limit of my skill set lol :slight_smile:

1 Like

Not sure if this is any help

1 Like

One day. In the meantime, you’re learning and helping others learn along the way.

If you create an extension, you can pass it a group name from the calling function and use it within Javascript. Javascript works better in extensions. My Javascript knowledge is still weak but I have a sound enough knowledge that I can accomplish a lot with help from the web.

Chat bots help but it’s knowledge of GDevelop is weak and every thing else is inconsistent and often unreliable. What’s worse is it gets stuck in loops and keeps repeating wrong answers.

1 Like

UPDATE

Iv been working all day on this and… just had a major break!!! …almost had a mental break too, but crises averted lol

  • I now have a script that retrieves all objects from the scene and culls them when needed.
  • I now also have a method of creating an exclusion group for objects you dont want to cull
  • Final step is to make the buffer size resizeable, which should be easy, and then we have something that honestly, i think will increase the potential of the engine immensely! Im not just saying this because im making it.

Performance has always bugged me, event organizing can only do so much, i think this will solve something that iv always found to be GDevelops weekest point.

Kepp ya updated!!! :smiley:

I made everything work and its basically an extension now.

Its dynamic and works anywhere and everywhere! It boost performance like crazy in any project!!

Made a guide with the final script in it and how to use it with variables and object groups!