godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.26k stars 101 forks source link

Make `PackedByteArray.has_encoded_var()` not print errors #12596

Open Germenzi opened 3 weeks ago

Germenzi commented 3 weeks ago

Describe the project you are working on

N / A

Describe the problem or limitation you are having in your project

I am using StreamPeerTCP to communicate, and when using get_var, the main thread may be blocked forever. To avoid that, I use internal PackedByteArray to store the data peer already recieved and then check this data with PackedByteArray.has_encoded_var(0).

This method should expect that data may be incomplete, so I don't want to see many errors when var isn't recieved completely.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Don't print any errors if var decoding failed.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Example::


var some_data : Variant = "Hello, world!"
var some_data_bytes : PackedByteArray = var_to_bytes(some_data)
print(some_data_bytes.has_encoded_var(0)) # no erros
some_data_bytes.resize(len(some_data_bytes )-2) # makes the data incomplete 
print(some_data_bytes.has_encoded_var(0)) # will print an error, but I just want to check...

If this enhancement will not be used often, can it be worked around with a few lines of script?

N / A

Is there a reason why this should be core and not an add-on in the asset library?

N / A

Calinou commented 3 weeks ago

You can use Engine.print_error_messages = false before any method call that may print an error, then Engine.print_error_messages = true just after. (Remember to always set it back to true after!)

Germenzi commented 3 weeks ago

You can use Engine.print_error_messages = false before any method call that may print an error, then Engine.print_error_messages = true just after. (Remember to always set it back to true after!)

I didn't know about that, but it still doesn't work with the Error tab in the editor where these erros appears (sorry, I didn't make myself clear when I used the word print), so, unfortunatly, the problem still actual.

And in general, given the blocking behavior of the function StreamPeerTCP.get_var(), it would be to great have an ability to check can I get this var without blocking (it can be forever because of attacks).

P.S. Also maybe Its good idea to make the same option for making such errors not to be "printed"? Is it possible?