Check if file exists before trying to load file

I tried RegularFileExists but it does not work; it never finds the file, even if it is 100% there.

if RegularFileExists (‘castle-data:/test.txt’) then Label1.Caption:= ‘File exists!’;
(does not find it).

if RegularFileExists (‘castle-data:/test.txt’) then Label1.Caption := ‘File exists!’ else Label1.Caption := ‘Not found!’; even gives a SIGSEGV error.

RegularFileExists takes a filename.

Please read the API documentation of RegularFileExists ( Castle Game Engine: CastleFilesUtils ) – it shows that parameter is FileName, and it points you to the solution:

Consider using URIExists or URIFileExists instead of this function, since in CGE you should use URLs for everything.

About SIGSEGV – most likely this is unrelated to RegularFileExists usage, and there’s something wrong with Label1 value. We’d have to see a simple testcase to reproduce the problem to help.

Yes, I know it’s a filename and I read the API before.
Now when I use URIFileExists it works allright but with RegularFileExists it still does not work; the result is always False. If syntax is the same and URIFileExists works fine, are you sure it’s not a bug?

     if URIFileExists('castle-data:/test.txt') then Label1.Caption := 'it exists' else Label1.Caption := 'it does not exist';
     if RegularFileExists ('castle-data:/test.txt') then Label1.Caption := 'it exists' else Label1.Caption := 'it does not exist';

Yes, this was my fault; I had put the File Search before

Label1 := DesignedComponent (‘Label1’) as TCastleLabel;

RegularFileExists takes a filename. It means a literal filename that is understood by your current operating system, like c:\something\something.txt on Windows (c:/something/something.txt also works on Windows; on Unix example would be /home/username/something.txt).

castle-data:/test.txt is not a valid filename, it is a URL. The castle-data is the URL protocol. See Data directory | Manual | Castle Game Engine and Network, downloading and using URLs | Manual | Castle Game Engine about URLs in CGE. In general it is best to use URLs everywhere across CGE.

RegularFileExists should actually be internal, to avoid confusion. Though CastleFilesUtils unit contains some other routines that take filenames… I added to TODO to hide them at some point.

Well, URIFileExists works fine. I need this because when I load a lot of spritescreens, some of them are not finished for all characters (for all walking directions) so I get a lot of SIGSEGV when playing.


1 Like