Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411579 Posts in 69386 Topics- by 58445 Members - Latest Member: Mansreign

May 05, 2024, 05:13:06 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Flash: save a map as a string
Pages: [1]
Print
Author Topic: Flash: save a map as a string  (Read 1393 times)
Almost
Level 0
***



View Profile WWW
« on: July 02, 2010, 06:31:58 PM »

Hello!

So I am fairly new to flash, and am trying to figure out how to best save a map with actionscript. Although I haven't tried, I think sharedobjects would work locally, but I would like to be able to save these maps online. In particular, I was hoping on using Kongregate's content sharing API to manage a "user maps" feature. However, as I understand, for this to work a "map" would have to be represented by a single string.
Currently, a map in my game is defined by a few arrays of numbers/strings (one for walls, one for units, one for enemies).

Is there a good way to convert an array to raw data and then to a string and back or something like that?
The only other thing I could think of is making a messy save function that creates a long string like "walls[1,2,6,7]units[3,4],[3,5]enemies[4,8]". I could probably do this, but it seems like there is probably an easier way.
Logged

A game of mine: Ten Second War
Glaiel-Gamer
Guest
« Reply #1 on: July 02, 2010, 06:38:54 PM »

bytearray.writeObject
Logged
st33d
Guest
« Reply #2 on: July 03, 2010, 02:10:21 AM »

Usually I have my map as a grid of indices that refer to specific tiles and some extra xml describing more complicated objects.

I mostly stick to this, because it means I can twiddle with the xml by hand if I need to and if I ever get a corrupted map I can just open up the xml, glance at it, and know what's wrong straight away. It may not be the most efficient way, but it's maintainable and easy to debug.
Logged
raigan
Level 5
*****


View Profile
« Reply #3 on: July 03, 2010, 06:34:14 AM »

bytearray.writeObject

how does this help serialize to/from a string? You could use bytearray.toString() one way, but you'd then have to parse that yourself the other way, which is likely to be messy.

Possibly you could create the bytearray using writeObject(), then scan over it reading each byte and converting it to a string that's appended with the other strings, and then reverse the process (turning each string into a string, stuffing them in a bytearray, and using readObject()) to decode.

(I said "string" and not "char" because annoyingly you can't actually convert a byte into a char and have it work -- since some of the values are stupid "backspace"/etc. which fuck up the string; there are only really 7 bits of useable space in a char. You can either jump through hoops trying to deal with this to save a few bits, or you can do this simple thing which is to convert each byte into a 2-digit hexadecimal string.)


But I think the OP's suggestion is fine, although I would suggest using delimiters instead of array notation to make parsing easier:
Code:
var str:String = "1,2,6,7#3,4!3,5#4,8";

var wueList:Array = str.Split("#");

var wallStr:String = wueList[0];
var unitList:String = wueList[1].split("!");
var enemyStr:String = wueList[2];

var wallArray:Array = new Array();
for(var i:int = 0; i < wallStr.length; i++)
{
   wallArray.push(int(wallStr.charAt(i));
}
//etc.

I may be biased because this is how we've done things in the past; definitely bytearrays make more sense if you don't need everything to be a string, or if there's some way to easily convert bytearray<->string.
Logged
Glaiel-Gamer
Guest
« Reply #4 on: July 03, 2010, 06:51:35 AM »

bytearray.writeObject
I may be biased because this is how we've done things in the past; definitely bytearrays make more sense if you don't need everything to be a string, or if there's some way to easily convert bytearray<->string.

Here's the functions I have


Code:
function BAtoString(a:ByteArray):String{
var s:String = "";
a.position = 0;
while(a.bytesAvailable){
var b:uint = a.readByte();
s += ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'][b%16];
b=b>>4;
s += ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'][b%16];
}
a.position = 0;
return s;
}

function StringtoBA(s:String):ByteArray{
var a:ByteArray = new ByteArray();
for(var i:int = 0; i<s.length; i+=2){
var b:uint = (hint(s.charAt(i))) + (hint(s.charAt(i+1))<<4);
a.writeByte(b);
}
a.position = 0;
return a;
}

function hint(s:String):uint {
switch(s){
case "0": return 0;
case "1": return 1;
case "2": return 2;
case "3": return 3;
case "4": return 4;
case "5": return 5;
case "6": return 6;
case "7": return 7;
case "8": return 8;
case "9": return 9;
case "a": return 10;
case "b": return 11;
case "c": return 12;
case "d": return 13;
case "e": return 14;
case "f": return 15;
}
trace("invalid character");
return 16;
}

also, if you're using a level sharing API, they generally will accept byteArrays.

and also, ByteArray.compress will do a nice compression on your level code to make it nice and short for sharing.

i wrote up a tutorial on this here
http://www.newgrounds.com/bbs/topic/1107353
Logged
Almost
Level 0
***



View Profile WWW
« Reply #5 on: July 03, 2010, 07:10:52 PM »

Thank you all. You've been very helpful. I'll probably go with Glaiel's ByteArray to string conversion.

[edit] I just tried both methods and bytearrays don't seem to compress very well.
a custom tostring function gave me 7 lines (if wordwrap is in effect)and a bytearray converted to a string with glaiel's method of the same data gave me 58 lines (again, if wordwrap is in effect)
[edit again] ... because I was unintentionally saving more data then I meant to when using the bytearrays. saving the unit objects rather than just their positions
« Last Edit: July 08, 2010, 04:53:39 PM by Almost » Logged

A game of mine: Ten Second War
roboprez
Level 1
*


Got to love them pixels


View Profile WWW
« Reply #6 on: July 07, 2010, 12:10:03 AM »

Code:
public function returnMap(map:Array):String {
var csvReturn:String
for (var j:int = 0; j < map[0].length; j++)
{
for (var i:int = 0; i < map.length; i++)
{
if(i == 0){
if(j == 0){
csvReturn += map[i][j];
}else{
csvReturn += "\n"+map[i][j];
}
}else{
csvReturn += ", " + map[i][j];
}
}
}
return csvReturn
}
This is some code I wrote yesterday that returns a CVS formatted string. Like this
Code:
0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,0,
0,0,0,1,1,1,1,0,
0,0,1,1,1,1,0,0

If the page breaks don't work nicely replace the "\n" With some thing like # to show the next line.

I haven't written a parser for it yet
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic