templates - partially initialize structure

Cross post from binary - 010 editor templates - partially initialize structure - Stack Overflow

Is there way to define some struct in 010 Editor template that will be only partially initialized and initializing of some parameters will be finished later. I’ve found that I can create another structure like MeshUvData(Mesh &mesh) that will be initialized somehow as MeshUvData(mesh[i]) but I’m looking for more readable to keep the data in 1 struct.

Example:

struct Mesh {
    float num_verts;
    float coords[num_verts*3];
    float uv[num_verts*2];
};

uint num_meshes;
uint num_textures;

// here need to initialize everything besides `uv` parameter
Mesh meshes[num_meshes];
Texture textures[num_textures];

// here need to finish initializng just `uv` attribute
Mesh meshes[num_meshes];

Hello Andrej730

Thanks for your message. There are a few different ways to accomplish this. One option would be to declare uv as local like this:

struct Mesh {
    float num_verts;
    float coords[num_verts*3];
    local float uv[num_verts*2];
};

Then you could fill in the values of uv later on by using a for loop. To read the uv data from the file you could either create a temporary struct or just use the ReadInt/ReadFloat functions to read the data. If you need an example of this you would have to post more about how the uv coordinates are stored.

Another possibility may be to use the FSeek function inside the Mesh struct to jump to the location of the uv coordinates, define them, and then use FSeek to jump back. This would only be useful if you can calculate the position of the uv coordinates inside the Mesh struct.

There are a few other options using multiple struct definitions and FSeek but using the local array is probably the easiest. Remember if using local variables to right-click on the template results and make sure ‘Show Local Variables’ is checked.

Graeme
SweetScape Software

Thank you for your reply!

Another possibility may be to use the FSeek function inside the Mesh struct to jump to the location of the uv coordinates, define them, and then use FSeek to jump back. This would only be useful if you can calculate the position of the uv coordinates inside the Mesh struct.

Yeah, I don’t really have any offsets where UV data is located - I just know that it’s located after the textures. So using FSeek would be unreasonable here since I would need to duplicate the entire structure logic for all the data between meshes and meshes’ uv data into meshes.

Then you could fill in the values of uv later on by using a for loop. To read the uv data from the file you could either create a temporary struct or just use the ReadInt/ReadFloat functions to read the data. If you need an example of this you would have to post more about how the uv coordinates are stored.

Local variables work but the main problem with them is that it also exposes all other local variables I have and the view becomes a bit polluted. Any way to expose only specific local variables?

There are a few other options using multiple struct definitions and FSeek but using the local array is probably the easiest.

What could be the other options?

You can tell 010 Editor to hide certain local variables by using hidden. For example:

local int myVal <hidden=true>;

Another option would be to build in the code to read the uv array into the Mesh structure and then define the Mesh structure once at the beginning of the file and once at the end. You would need to use FSeek and the ‘exists’ keyword to check if variables have been defined. For example:

struct Mesh {
    float num_verts;
    float coords[num_verts*3];
    
    if( exists( /* variables need to find uv */ ) )
    {
        local int pos = FTell();
        FSeek( /* start of uv array */ );
        float uv[num_verts*2];
        FSeek( pos );
    }
};

uint num_meshes;
uint num_textures;

// here need to initialize everything besides `uv` parameter
Mesh partialMeshes[num_meshes];
Texture textures[num_textures];

// here need to finish initializng just `uv` attribute
FSeek( startof( partialMeshes[0] ) );
Mesh completeMeshes[num_meshes];

Graeme
SweetScape Software

1 Like

Another option would be to build in the code to read the uv array into the Mesh structure and then define the Mesh structure once at the beginning of the file and once at the end. You would need to use FSeek and the ‘exists’ keyword to check if variables have been defined. For example:

Approach with saving offsets instead of duplicating textures logic in mesh sounds much more straightforward, will look into it, thank you!

You can tell 010 Editor to hide certain local variables by using hidden. For example:

Is there way to set it <hidden=true> by default for locals? So, if I’m using “show local variables” I’d need to set hidden flag to false only for the local variables I need not the other way around (setting hidden to true for all variables I don’t need).

And small side question - is there way to show more detailed Syntax error messages? Usually it’s just showing the line like *ERROR Line 11(8): Syntax error. and no other clues for what the error might be.

struct Mesh {
    local int num_verts = 3;
    float coords[num_verts*3];
    
    if( exists( textures ) )
    {
        float uv[num_verts*2];
    }
}

locl int num_meshes<hidden=true> = 2;

If you have a bunch of local variables you want to hide, you can create a separate type using typdef like this:

typedef local int localint <hidden=true>;
localint num_verts;

There is no way currently to provide more detailed information from syntax errors but we are planning on adding that to a future version. Cheers!

Graeme
SweetScape Software

1 Like