Finding the longest string in an array

c1trus

Member
Im working on GUI right now and want to make sure the menu backdrop changes in size to fit the longest string in a given array. What function could I use to accomplish this? (I'm using GMS:S 1.4)
 

NightFrost

Member
Width in pixels (not letters)? You can use string_width for that. Just remember that is uses the font you've currently set for draw, so it has to be the same when you measure width and when you draw.
 

c1trus

Member
Width in pixels (not letters)? You can use string_width for that. Just remember that is uses the font you've currently set for draw, so it has to be the same when you measure width and when you draw.
Im aware of that particular function, I'm wondering though how to find out the string width of the largest string in an array. I'm assuming the max function is used somewhere here.
 

NightFrost

Member
Compose a loop to walk through array items and have a variable keeping track of largest discovered width. Assuming the array is called Strings_Array:
GML:
var _Widest = 0;
var _Size = array_length_1d(Strings_Array);
for(var i = 0; i < _Size; i++){
   var _Width = string_width(Strings_Array[i]);
   if(_Widest < _Width) _Widest = _Width;
}
 
Top