03-20-2018, 02:40 AM
(This post was last modified: 03-22-2018, 08:43 PM by DomesticAnt.
Edit Reason: Solved my own problem
)
I'm trying to write a simple path finding algorithm, and I've run into a problem with cbot arrays that I can't seem to work around:
I'm trying to create a 2d array representing the map at a 1m scale, so the array is nominally 808 by 808 elements.
If I try to initialize all the elements to some default value, Colobot GE slows to a crawl and I eventually have to kill the process.
Since Colobot appears to support sparse arrays, it seemed like a good idea to initialize only those array elements that the simple pathfinder actually needed to access (typically just a few hundred). The problem with THAT is that I can't find any way at all to test if an array element has been initialized.
Consider the following simple example program:
So "sizeof(foo)" is no help.
"if( foo[0] == null )" fails to compile
EDIT:
While messing about with something unrelated, I stumbled upon a solution using CBot's largely undocumented try-catch syntax. The following program seems to run as expected:
My SafeRead() function isn't as generic as I'd like, but it should be trivial to adapt for use in most situations where something like this is needed.
I'm trying to create a 2d array representing the map at a 1m scale, so the array is nominally 808 by 808 elements.
If I try to initialize all the elements to some default value, Colobot GE slows to a crawl and I eventually have to kill the process.
Since Colobot appears to support sparse arrays, it seemed like a good idea to initialize only those array elements that the simple pathfinder actually needed to access (typically just a few hundred). The problem with THAT is that I can't find any way at all to test if an array element has been initialized.
Consider the following simple example program:
Code:
extern void object::artest()
{
errmode(0); //This suppresses the "Variable not Initialized" error message, but doesn't stop the script from halting
message( "Hello World" );
int foo[]; // sizeof(foo) == 0
foo[10] = 42; // sizeof(foo) == 11
if( foo[0] == 0 ) {
message( "This never happens" );
}
else {
message( "This also never happens" );
}
message( "You won't see this message" );
}
So "sizeof(foo)" is no help.
"if( foo[0] == null )" fails to compile
EDIT:
While messing about with something unrelated, I stumbled upon a solution using CBot's largely undocumented try-catch syntax. The following program seems to run as expected:
Code:
extern void object::artest()
{
errmode(0); //This suppresses the "Variable not Initialized" error message, but doesn't stop the script from halting
message( "Hello World" );
int foo[]; // sizeof(foo) == 0
foo[10] = 42; // sizeof(foo) == 11
for( int i = 0; i < sizeof(foo); i++ ) {
int v = SafeRead( foo, i );
message( "foo[" + i + "] = " + v );
}
message( "Program finished" );
}
int object::SafeRead( int[] ar, int n ) {
int defaultvalue = -99;
int rv;
try {
rv = ar[n];
}
catch( CBotErrNotInit ) {
message( "SafeRead() Caught Not-Initialized Error, returning default", DisplayError );
return defaultvalue;
}
catch( CBotErrOutArray ) {
message( "SafeRead() Caught Out-of-Bounds Error, returning default", DisplayError );
return defaultvalue;
}
return rv;
}
My SafeRead() function isn't as generic as I'd like, but it should be trivial to adapt for use in most situations where something like this is needed.