Programmatically made World and Networking -> occasional bugs

Hello All.
I am currently programmatically creating a world made of a cube prefab (already present in the scene) and a floor (also present in the scene, made from a flattened cube).

The cube and the floor have different materials. Both are instantiated with instantiateSynced.

The instantiation of the world is created only once, depending on the user role (maker and observer), set from url parameters.

Sometimes for unclear reasons, the material of the objects are swapped.

Also, since deploying to glitch, sometimes the whole world is only partially renderered for the observer.
That seems like the server did not register or is not serving the full list of instantiated objects.

What do you think would be the problem @marcel :cactus: ?

Also, is there a better practice for programmatically creating scenes ? would having those prefabs as gltf be more efficient ? (i tried but nothing was instgantiated so i gave up)

Oh and I don’t get any skybox in safari, or chrome on my mac… but on safari ios it pops up … why is that ? Thanks !

Original Post on Discord

by user 700879811085598831

Hi, sounds interesting

  1. which version are you currently using?

  2. The url parameters contain only the role (or more settings for the world generation)
    Are you joining a room before creating the world? Are you using the SyncedRoom component and if so with which settings? Just asking if you are using the same room for all your worlds or if you use a different room for each world? If it’s the same room it could lead to collisions / data being used from a previous world.

  3. How big is your world? E.g. how many instantiations are you doing? Can you show a bit of sample code for how to generate the world with the instantiateSync call?

I dont think prefabs are the problem/solution. There are multiple alternative solutions to try to solve it but I’d like to figure out what might be wrong with your currentl approach right now first (could you send your project as a bug report? using the Needle Engine/Report a bug menu item)
Alternative a)
just sync the seed and settings via networking and then generate the world for each client again instead of syncing each instantiation
Alternative b)
manually sync the object data in one big array/object and sync one object (the “world-generation-state”) instead of multiple individual instantiations

About the skybox: do you get any errors in the browser console? Are you exporting the skybox from Unity too? I havent heard of any issue like that before. Do you have a link to your deployed site?

Thanks @Marcel !
i am using version 3.6.9

answering question 2) :
My synced Room component is set to GlitchTemplate, url parameter name to “room” and booleans join random, require room parameter and auto rejoin all set to true.

at the moment I let it generate a random room, then add my parameters such as : &id=player. this parameter set the creation of scene. First I check if there are any objects as children of a transform that is supposed to be the parent of all scene objects. then if none, I generate a 2D array that creates a maze with its path. I then instantiateSync cubes whereever the value of the 2D array is set to 1 (wall).

here is a pieces of code:

// Create cubes for the maze
        for (let i = 0; i < this.mazeSize; i++) {
            for (let j = 0; j < this.mazeSize; j++) {
                if (this.maze[i][j] === 1) {
                    // Create a cube for a wall
                    const opts = new InstantiateOptions();
                    opts.context = this.context;
                    opts.position = new Vector3(j * this.cubeSize, this.cubeSize/2,i * this.cubeSize);
                        const prefabInstance = GameObject.instantiateSynced(this.prefab, opts) as GameObject;
                        if (!prefabInstance) return;

by user 700879811085598831

I have been mostly testing a small size : 50 objects (cubes) as well as bigger: 450. But size doesn’t seem to be the problem

by user 700879811085598831

" If it’s the same room it could lead to collisions / data being used from a previous world." => I use the same room per session. Sometimes the creation of the scene fails to find already present object and creates another maze over the first but most of the time it works.

by user 700879811085598831

I think i will do as you suggested ! creating the seed (array) on one instance and communicating it to all to have local instantiations of the maze ! Thanks !
How should I send the seed ?
this.context.connect.sendBinary(arr:Uint8Array) ?
Would users joining late still get a callback once listening to
this.context.connection.beginListenBinrary(identifier:string, callback:(data : ByteBuffer) => void) ?

by user 700879811085598831

You can just send it via a json object e.g. send() (not sendBinary, that’s only useful for sending data with a lot of updates to reduce the load on the networking server, e.g. it’s being used by the SyncedTransform using flatbuffers)

As of the skybox, i am not particularly exporting it. Out of the box it shows up on my iphone but doesn’t on my mac : safari or chrome. here is the glitch website:
it should show the grey “blurred-skybox” from the SceneTemplate. Left as is, it won’t create anything unless you add &id=player .Any other instance joining the same room with a different id or none will become observers.

by user 700879811085598831

so you mean this.context.connect.send() ?

by user 700879811085598831

It shows correctly on win10 chrome, which chrome version do you use on your mac and can you check the browser console if there’s any message?

yes, it’s on the same object as sendBinary but you can pass in e.g. a jsonobject or just a string. Depending on what you need.

I get absolutely no error in the console. it does say needle version 3.6.4 though. and i have Version 114.0.5735.133 (Official Build) (arm64) of chrome (latest)

by user 700879811085598831

glitch seems to override my css. it was set to background:teal but it sets it to black. however i don’T have the skybox

by user 700879811085598831

Do you see the skybox in the sample scenes? e.g. Needle Engine Samples

what do i need to import ? VScode tell me there are no property “connect” on context. (by the way this is the kind of information i take hours looking for)

by user 700879811085598831

Glitch doesnt bring any css, when you deploy to glitch your bundled web files (in the dist folder) are just zipped and uploaded and then unpacked on glitch.

Ah sorry it’s this.context.connection

ha ! ok makes more sense ! I guess the key is to identify between messages ? do i set what ever I want on both ends? How do i convert my array to JSON ? i guess i can’t use Jsonutility …

by user 700879811085598831

The key is to subscribe to it yes. You send to send("my-message and also listen to beginListen("my-message

You dont need to worry about serialization. Just make a object with the array e.g. (send("test", { myData: [....] }