Repository navigation
pass strings reference to a function in gdscript #15580
GianpyRock
started this conversation in
Scripting
Replies: 2 comments
Where are you taking this from, is it based on actual profiling? As far as I'm aware String is internally backed by Copy on Write semantics, so they should only be copied if you make changes. |
0 replies
|
So... I re-read what I wrote and honestly, I fumbled.
That day I saw random memory usage spikes and confused myself while debugging. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Proposal
Hello, I want to propose to add a keyword for passing strings as a reference into a function call instead of being forced to create a copy of it.
I've encountered a case where I want to decode a string using a parser method and such parser method will be called multiple times with the source string passed unchanged.
But the problem is that the source string is copied each time is passed, it won't change and for me this seem a big waste of process to copy a string just to read it and then trash it away.
Seems to be no feature to do so in current gdscript, the most similar thing is using
StringNamebut for the case I'll describe below this type doesn't seem optimal for it.Problem
The most obvious solution is that the string must be passed as a reference, but Godot does not have a way to do that with strings.
I should instead pass an object, or an array, that contains the string or define the string as a global variable in the current class.
Or in an extreme case, make bindings with the c++ or c# api to have this feature in the gdscript.
The Feature
Could it be added a new keyword that can pass the string as a reference instead?
It could be
refor reused the existinginkeyword to be more similar to c# syntax that implements passing variables by reference.This seems very obvious that the function is asking for a reference of a string, and not a copy of a string.
My Case
I've encountered this case in my project, the game encodes a level as a string and put it into a text file, each line contains informations I want to decoded composed by words and each word is like a name and a value.
I have my reasons I choose a text file to do this instead of resources or scene file:
I wrote a function that accepts the string of the line as input, an offset and string length, then decodes the length to extract each word.
I realised that the decode loop will copy the string and I think that's a waste of process that I don't like in my code.
To give full context, this is how the parse loop is structured.
I think my proposal is valid and I want to share it, I searched and couldn't find about any similar proposal for string reference as I described in this post.
All reactions