If you use something like the Text Blitter you can append to a string, not sure if you can append as easily using the regular string object (never tried!). You just set the source string and append a character from it one at a time.
Appending to a string like that is extremely slow.
Strings are immutable data types. That means that every time you want to edit a string, you have to create an entirely NEW string, and delete the old one. That means that to create a string of length N via appending each char, you use up a little over (N^2)/2 memory instead of N.
So if you have a game that writes a text like this that is fairly large, your game will experience dramatic slowdown when you try to append piece by piece.
Instead its safe to use the Binary object; binarys are mutable, meaning you can actually just add a character to the end of the same object, instead of creating a new one. So just create an empty binary, and append 1 character byte at a time to it, and you can retrieve the current string from it.
the reason that strings use so much memory is that theyre dynamic. when you append to a string it has to make an entirely new entry in memory to accomodate the new length of chars and delete the old memory locations. amirite?
but the process of appending to a string char by char shouldnt even be noticeable on any newish computer pixel.
Depends on what you're using it for. In my text-based FPS game, I used strings along the Y vertical that pulled from the main text to create the effect, which meant I had around 32 strings of up to length 24 each. And so instead of having to create 32 string objects per frame, it was creating 9600 strings per frame, which made it run at about 1 FPS instead of 50 FPS.
So if you're going to be building anything inside a fast loop its especially important, or having more than one object, etc etc.
When you set the assign the string to the string object from the binary, doesn't that still have to delete and recreate the memory for the string object??
thats exactly what i was thinking as soon as i saw that code snippet.
as long as you stay away from something like pixeltheifs text based fps where the entire raycasted world is rendered as ascii characters, you'll be fine.
Originally Posted by Klikmaster When you set the assign the string to the string object from the binary, doesn't that still have to delete and recreate the memory for the string object??
Yes, but in terms of overhead, its much faster on an order of magnitude. This is specifically for dealing with when you want to create a string out of chopped up chars, and display the end result. If you display every single substring along the way, you are creating exactly as many string objects. But you are only using 2 per frame, so it will never be system intensive.
Instead, if you are doing something that creates a 'large' text out of smaller chunks, and then displays the end result, like my Text Based FPS, it is impossible without doing something like this.
Imagine for a second that you added a function where if you hit the "Enter" button while someones char-by-char text was writing to the screen, it would jump one line ahead by running your "Append 1 char" function inside a fast loop 30 times or something. If you did it this way, it would still only be creating 1 string of weight 32 instead of 32 strings of combined weight 528