Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2015-05-05 14:42:25

mathy
Member
From: Netherlands
Joined: 2015-04-14
Posts: 18

Halp! Special blocks while saving/loading maps

So I'm making a bot for Super Smash Brothers in EE, and it's faaar from done, but there's one problem I have with saving maps (and it can save and load individual maps), and that is that it won't save any special blocks I place.

The code I have for saving maps is:

static void SaveMap(string Filename)
        {
            string path = Environment.CurrentDirectory + @"\" + Filename + ".txt";
            StringBuilder sb = new StringBuilder();

            foreach (Block b in blockList)
            {
                if (b.x > 32 && b.x < 67 && b.y > 49 && b.y < 75)
                {
                    sb.Append(b.l).Append("-").Append(b.x).Append("-").Append(b.y).Append("-").Append(b.bid).Append(",");
                }
            }
            File.WriteAllText(path, sb.ToString());
            Say("Done saving! Map saved as \"" + Filename + "\".");
        }

And there's a second problem I experienced: it never seems to say the last line. Does that mean it will continue saving the blocks in that area forever? And how do I fix that then?


akXyPnH.pngPWaJiSA.png7Oq4Nl9.png
ERMAHGERD I AM THERE

Offline

#2 2015-05-05 15:08:53

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: Halp! Special blocks while saving/loading maps

mathy wrote:

So I'm making a bot for Super Smash Brothers in EE, and it's faaar from done, but there's one problem I have with saving maps (and it can save and load individual maps), and that is that it won't save any special blocks I place.

The code I have for saving maps is:

static void SaveMap(string Filename)
        {
            string path = Environment.CurrentDirectory + @"\" + Filename + ".txt";
            StringBuilder sb = new StringBuilder();

            foreach (Block b in blockList)
            {
                if (b.x > 32 && b.x < 67 && b.y > 49 && b.y < 75)
                {
                    sb.Append(b.l).Append("-").Append(b.x).Append("-").Append(b.y).Append("-").Append(b.bid).Append(",");
                }
            }
            File.WriteAllText(path, sb.ToString());
            Say("Done saving! Map saved as \"" + Filename + "\".");
        }

And there's a second problem I experienced: it never seems to say the last line. Does that mean it will continue saving the blocks in that area forever? And how do I fix that then?

I suggest making your Block object serializable.
That way you can make a List/array of your Block objects and save/load the List/array directly.

Code I use for serializable object saving/loading (You'll have to make a quick Google on how to serialize your objects, it's real easy):
http://pastebin.com/jfXQem8N

The code is put in a class, but obviously you can always take the methods out and place them elsewhere.

Offline

#3 2015-05-05 16:59:42

mathy
Member
From: Netherlands
Joined: 2015-04-14
Posts: 18

Re: Halp! Special blocks while saving/loading maps

I have absolutely no idea how to do it. I'm just a programming nub, and whenever I find something that could help, I either don't know which things to replace, or where to place it (or even what it means).


akXyPnH.pngPWaJiSA.png7Oq4Nl9.png
ERMAHGERD I AM THERE

Offline

#4 2015-05-05 19:23:11, last edited by den3107 (2015-05-05 19:24:44)

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: Halp! Special blocks while saving/loading maps

I'm just going to paste a short tutorial with code I have:
If I'm correct, what you're trying to do is safe a Block object
example:

Hidden text

Now what you can do to safe this object, is make it serializable by adding "[Serializable()]" in front of the class, giving you (bold is what is added):

Hidden text

Now that we got our Block object serializable, we can save and load it with the code I gave you in the code snippet:
http://pastebin.com/jfXQem8N

This code snippet consists out of a class with 2 functions/methods:

"save<T>" and "load<T>"

You can take the methods out of the code snippet and place them wherever you'd like, just like any other method.

Now what we're going to do is actually save and load something.
First we read our world and put the found blocks in Block objects (like what you have already managed to do from the looks of your own code snippet (blockList)).
Here is how I assume blockList is initialized:

List<Block> blockList = new List<Block>();

And after that it's filled with Block objects

Now we got our list of Block objects. All that's left to do is save/load it!

The "T" in the methods' names mean that the type isn't defined, meaning any Object can be put here.
To save your List you just call the method in the following way:

save<Block>(blockList, path, fileName);

Obviously you'll first have to define "path" and "fileName".

Congratulations! You've saved your Block objects!

Now how to load them back in:

Hidden text

And then you just do with the read blocks whatever you want!

Hope it's clear now, just tell me if it's still not clear (I might answer in a PM though).


P.S. I personally often don't know how to use big code snippets from others too, which if why I personally always like to make things myself (the save/load functions I haven't made myself btw).

Offline

#5 2015-05-06 12:24:59, last edited by DarkDragon4900 (2015-05-06 12:28:01)

DarkDragon4900
Member
Joined: 2015-03-17
Posts: 251

Re: Halp! Special blocks while saving/loading maps

I prefer to add an object array for extra data and keep it null when using it unless extra data is needed.

public class Block
{
    public int Layer;
    public int X;
    public int Y;
    public int BlockID;
    public object[] Args;
    public Block(int layer = 0, int x = 0, int y = 0, int bid = 0, object[] args = null)
    {
        Layer = layer;
        X = x;
        Y = y;
        BlockID = bid;
        Args = args;
    }
}

And JSON(Javascript object node) to save and load objects.

Serializing class
Saving/Loading

Offline

DarkDragon49001430911499501867

Board footer

Powered by FluxBB

[ Started around 1714210995.7466 - Generated in 0.077 seconds, 13 queries executed - Memory usage: 1.45 MiB (Peak: 1.59 MiB) ]