-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve workflow of File and Directory #1551
Comments
Open each file every time folder is iterated? In this case |
Given gdscript's python inspiration, could be worth looking at their newer pathlib stuff? https://docs.python.org/3.8/library/pathlib.html |
@bruvzg the constructor should only fail if the path has an invalid syntax. There should be no fail for "file not found" unless you actually try to read from it. Why? Because for example: var file = File.new("res://abc")
if file.exists():
pass |
I agree that the current file apis are cumbersome and Java's file api is a good source for inspiration. Also, constructors should never throw an error or fail. |
agreed, especially the boilerplate code for iterating over the contents of a directory is really quite unwieldy:
My initial thought about improving situations like those at least somewhat would be for GDScript to support assignment within while conditions, but I also like what's been proposed and am in favor of the consideration to combine the two APIs. |
Definitely need to rework the mode flags. First, it's easy to confuse var f = File.new()
var mode = f.READ_WRITE
if !f.file_exists():
mode = f.WRITE
f.open("user://path", mode)
f.seek_end() I suggest replacing these flags with
I just use the following pattern: var dir = Directory.new()
dir.open("res://path/")
dir.list_dir_begin(true) # skip_navigational
while true:
var fname = dir.get_next()
if !fname: break
if !fname.match("*.ext"): continue
... Firstly, the entire first file is now processed in a loop, like all the others, and secondly, since all processing is at the beginning of the loop, I can safely use
In principle, this is also acceptable, only we need a different method, for example But, in built-in classes, usually |
Updates after a couple years:
(P.S.: Can a maintainer rename godotengine/godot#40547 to specify both I think we'll need an expert to take a look at the suggested API. Despite being the proposal author, I am not fully convinced it is how things should be.
Hmm, I don't think this is good (I know my proposal implied it though). I can see users doing something like var file = File.new(...)
if file.exists():
file.open(...) Which can cause race conditions (as well as permissions issues in some cases). The user should do this: var file = File.new(...)
if file.open(...) == OK:
# now we can read stuff! Whether |
Describe the project you are working on:
Considering reworking much of the File and Directory workflow for 4.0, and making this issue to brainstorm and discuss what we should change.
Kind of a continuation of #1225
Describe the problem or limitation you are having in your project:
Currently, the system for files and directories is quite verbose and takes a lot of boilerplate.
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
I want to take inspiration from Java's
File
API."Doesn't really make sense tbhFile
" can represent a file or a directoryDirectory contents can be iterated withResolved in Add get_contents method to Directory class godot#40547get_contents()
Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
Edit 2022:
File.READ
in the constructor (removed it in example), since 1) users might not want to READ the file's contents, 2) Errors couldn't be handled.get_path
andget_name
areString
methods, I don't think we should just copy paste each of them...(Directory.get_contents() is implemented already, see old suggestion here)
If this enhancement will not be used often, can it be worked around with a few lines of script?:
Files and Directories are used very often
Is there a reason why this should be core and not an add-on in the asset library?:
File is already part of Godot's core
The text was updated successfully, but these errors were encountered: