Developer Error: must call update before calling getGeometryInstanceAttributes

Hi,
Again, I'm new to Cesium, so here's a spoiler for nub questions :slight_smile:
I'm getting the above error ("Developer Error: must call update before calling getGeometryInstanceAttributes").
Here's my code:

/*********************** Code Starts ***************************/

// create or update polygons (50/50 chance)
function addOrUpdate(){

function createNewPolygon(){
// Create the polygon geometry.
                    function getCoordinateValue(diff, base){
                        return Math.random()*diff-base;
                    }

                    var positions = Cesium.Cartesian3.fromDegreesArray([
                        getCoordinateValue(50, -115.0), getCoordinateValue(15, 35),
                        getCoordinateValue(50, -115.0), getCoordinateValue(15, 35),
                        getCoordinateValue(50, -115.0), getCoordinateValue(15, 35),
                        getCoordinateValue(50, -115.0), getCoordinateValue(15, 35),
                        getCoordinateValue(50, -115.0), getCoordinateValue(15, 35)
                    ]);

                    // Create a geometry instance using the polygon geometry.
                    var polygonInstance = new Cesium.GeometryInstance({
                        geometry : Cesium.PolygonGeometry.fromPositions({
                            positions : positions,
                            vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
                        }),
                        attributes : {
                            color : Cesium.ColorGeometryInstanceAttribute.fromColor(
                                Cesium.Color.fromRandom({alpha : 1.0})
                            )
                        },
                        id: viewer.scene.primitives._primitives.length
                    });

                    return polygonInstance;
}

function updateExistingPolygon(){
var primitives = viewer.scene.primitives._primitives;

                        var index = Math.round(Math.random()*(primitives.length-1));
                        var chosenPrimitive = primitives[index];
                        var attributes = chosenPrimitive.getGeometryInstanceAttributes(index);
                        attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.fromRandom({alpha : 1.0}));
}

var newPolygons = ;
if (Math.random()<0.5)
newPolygons.push(createNewPolygon());
else
updateExistingPolygon();
}

viewer = new Cesium.Viewer(elm.attr('id'));

setInterval(addOrUpdate(), 1000, 5);

/*********************** Code Ends ***************************/
The line:
var attributes = chosenPrimitive.getGeometryInstanceAttributes(index);

The code roughly works, only once in a while it gives the above error (it sometimes succeeds for the same id, only to fail later, and then succeeds again for that same id).

Any idea why?
Couldn't find much about this error...

Hi Yonatan,

This is a limitation that we’d like to remove (#2174). In the meantime, check the ready property before calling getGeometryInstanceAttributes.

Here is a complete code example to copy and paste into Sandcastle: https://gist.github.com/pjcozzi/4bc8fbf5a2907de6c3a9

Patrick

Thank you.
Just to make sure I understand your solution in my case:

  1. For each primitive instance (I have several):

1.1) If it is ready, I apply updates.

1.2) If it is not ready:

1.2.1) I buffer the updates for this primitive as they come.

1.2…2) Once it is ready, I apply any updates in the buffer.

Is this the direction or have I missed something?

Thanks again.

You got it.

Patrick

Works like a charm. Thanks :slight_smile: