GUI label and String.Format -> game freeze

Started by EnterTheStory (aka tolworthy), Sun 10/07/2011 13:45:20

Previous topic - Next topic

EnterTheStory (aka tolworthy)

Could extreme usage of String.Format or a GUI label cause a game to freeze?

Once every few months someone comments that my game freezes when my "recent dialog" screen pops up. It uses this code:
Code: ags

String memoryDialog[115]; // records every line of dialog as it is spoken
String dialogText; // temporary
function updateDialogLabel(int line) // adds lines from "memoryDialog" to the end of  "dialogtext"
{	String text ="";
	if((line >=0)&&(line <101)) // elsewhere the contents of memoryDialog are looped after line 100
	{ if(memoryDialog[line] !=null)
		dialogText =String.Format("%s[%s", dialogText, memoryDialog[line]);
	}
}
function showMemoryDialog()
{ int line = memoryDialogBot -8;					// start 8 lines before current line
	dialogText ="";
	updateDialogLabel(line);
	int i =0; while(i<9){ line++;updateDialogLabel(line);i++;} // adds 9 recent lines 
	dialog0.Text =dialogText;
	if(memoryDialogBot >5)dialogUp.Visible =true; else dialogUp.Visible =false; // up and down arrows on the GUI
	if(memoryDialogBot <memoryDialogNow)dialogDown.Visible =true; else dialogDown.Visible =false;
}

I know there is a limit of 2000 characters and 35 lines, but I think it's unlikely I reach those limits. Is there anything else about this code that shouts 'potential freeze'?

monkey0506

What do you mean "limit of 2000 characters and 35 lines"?

I'm not sure why it would be freezing up, but you could consider using String.Append instead of String.Format, it might be faster:

Code: ags
dialogText = dialogText.AppendChar('[');
dialogText = dialogText.Append(memoryDialog[line]);

EnterTheStory (aka tolworthy)

#2
Thanks. I'll do that.

The 2000 characters and 35 line limit was from another thread on GUIs and String.Format - can't find the link now, sorry.  2.72 allowed GUI label text to include 2048 characters, and I don't know if it was increased.

monkey0506

Ah well, String.Append doesn't even have that limit (even when String.Format did). I think the limit was increased to somewhere around 5000 characters because of the things I was doing with data serialization and (data) vectorization. CJ specifically told me that unless I directly needed to format values that for my purposes I should be safe using String.Append.

I know you said you don't think you're even hitting that limit, but it couldn't hurt to completely circumnavigate it altogether, could it?

SMF spam blocked by CleanTalk