Reputation: 47354
I have a game that has a small console window which displays text messages about events that happen within the game. This console is a UITextView
, one message per line. The code I have below works for test purposes, but over the course of the game, I expect that this log would grow up to be very long.
What is a good practice for implementing user-visible logging within an iOS game? Is it a table view, some custom class, mutable string, read/write to file, etc?
-(void)updateConsoleWithMessage:(NSString*)message
{
messageConsole.text = [NSString stringWithFormat:@"%@\n%@",message,messageConsole.text];
if(messageConsole.text.length > 0 ) {
NSRange top = NSMakeRange(0, 1);
[messageConsole scrollRangeToVisible:top];
}
}
Upvotes: 0
Views: 195
Reputation: 2716
i would use a tableview because the memory mangement is very effective. so you only have the visible cells instantiated. the datasource would be an array with your messages as objects, so you can control via this array how many messages you will have in memory.
Upvotes: 1
Reputation: 19822
I think a queue will be better.
Build a queue (NSMutableArray) and build an object where you can add message. Now in a loop (NSTimer) with a certain cycles (for example each 3 seconds) - display the first message in queue (Pop). In next cycle just replace a message with next one.
You can also extend it so it displays more than 1 message (for example 3 if possible) and after certain amount of time cycle through next ones.
I think it's better approach because it will not go crazy big while you play longer (old invisible messages will be deleted). In your approach, unfortunately it will grow and grow and grow with each message you add.
If you want to use logging during development I highly recommend using NSLogger project (https://github.com/fpillet/NSLogger) - it allows you to debug in background, not using any space on screen at the same time making it easy to filter out different types, levels etc.
Upvotes: 1