Does sweetscape template language support optional arguments in functions and structs?
E.g. code below results in *ERROR Line 1(23): Syntax error.
void OutputInt( int d = 1 )
{
Printf( "%d\n", d );
}
OutputInt( );
Currently I’ve found a workaround by making some value work as undefined argument value.
#define UNDEFINED_INT_ARG -1
void OutputInt( int d )
{
if (d == UNDEFINED_INT_ARG)
d = 5;
Printf( "%d\n", d );
}
OutputInt( UNDEFINED_INT_ARG );
As for passing structs, since passing pointers is not supported, I currently detect unspecified arguments by one of the two ways:
Checking array element index:
#define UNDEFINED_ARRAY_ELEMENT 2147483647
struct Mesh {
uint a;
uint b;
uint c;
};
void OutputInt( uint mesh_index )
{
local uint t;
if (exists(m[mesh_index]))
t = 1;
else
t = m.a;
Printf( "%d\n", t );
}
Mesh m;
OutputInt( UNDEFINED_ARRAY_ELEMENT );
Checking struct on specified offset (element index method is probably better since checking struct on offset is parsing struct again and then hide the result, which is redundant):
#define UNDEFINED_STRUCT_OFFSET -1
struct Mesh {
uint a;
uint b;
uint c;
};
void OutputInt( uint mesh_offset )
{
local uint a;
if (mesh_offset == UNDEFINED_STRUCT_OFFSET) {
a = 15;
} else {
local uint cur_offset = FTell();
FSeek(mesh_offset);
Mesh m_temp<hidden=true>;
FSeek(cur_offset);
a = m_temp.a;
}
Printf( "%d\n", a );
}
Mesh m;
OutputInt( UNDEFINED_STRUCT_OFFSET );
OutputInt( startof( m ) );