Combat Cards


Streaming Combat Cards by docboffin
8 +00002006-10-12T21:34:52+00:00312006bUTCThu, 12 Oct 2006 21:34:52 +0000 2006, 9:34 pm
Filed under: Code, LSL, Scripting

The biggest change we needed to make to the Combat Cards code for the new horror expansion was to add support for data streaming. For a long time we’ve stored the data for the different combat cards as comma separated values in a notecard, one line per card. Early versions of Combat Cards just looped through this notecard turning the lines in to LSL lists which were concatenated together to create a big list with all the game data in. This worked fine up to about 9 cards, but when we went to the 15 current cards the server and client scripts started crashing with stack/heap collisions: LSL’s way of saying that scripts have run out of memory.

After getting some tips from the Scripters of Second Life and rummaging around on the LSL wiki, I found out that one problem was that LSL lists are very memory inefficient and that often storing data as strings is the way to go. So, version 1.0 and 1.1 of Combat Cards loaded all the card data as before, but stored each notecard line as a string rather than breaking it in to a list. When the script needed a specific value it would find the string it needed and temporarily convert it to a list to find the value. It sounds horribly inefficient, but it was fast enough and saved enough memory that Combat Cards would run again.

I always new it was going to be a temporary solution and, sure enough, when we added the data for the 12 new horror cards the stack/heap collisions returned. I could have looked for more memory saving hacks to try to squeeze the data in, or started splitting the scripts up in to multiple scripts to get more memory, but I realised that would only be putting off the problem again. What I wanted was a solution that meant we could have as many cards as we could imagine without ever running out of script memory. The solution was to stream the card data in to the scripts: only load card data as it was needed discard it whenever it wouldn’t been needed for a while.

It took a lot of work, but that is exactly what Combat Cards 1.2 does. The client script scans the inventory of the Combat Cards Deck object to find out which cards are in the deck and then randomly picks cards out of that set to draw cards. Only when cards are drawn is data loaded from the notecard: the card index is extracted from the card name and that line of the notecard is loaded in to the script. The client then sends the cards in the players hand to the server and the server script also loads the appropriate lines from the notecard in to memory. When cards are discarded, the data for those cards is discarded too.

So, now we have a solution for data management in Combat Cards that means that client scripts only need to load 6 cards in to memory and the server script only needs to load 12 cards, no matter how many different cards we come up with. The next limit we’ll run in to is presumably the maximum length of a notecard, but I don’t think we’ll hit that for a while yet.

Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: