Page 1 of 1

Saving Clone Positions

PostPosted: Thu Oct 21, 2010 11:48 pm
by chrisb77
Hi, I've been using Game Editor for a few weeks now and I am really enjoying it! The game I am designing at the moment is a survival/exploration platformer; a cross between Minecraft and Super Mario :) . The main mechanic of the game involves the Player Actor creating block Actors which can form a fort against a horde of zombies. One of the gimmicks is that the player can design their own fort.

The great news is that this mechanic works really nicely so far. The only difficulty I am having is when the player saves and loads the game. I would like to save the block Actors' x and y variables to the save file, but because they are clones the game only saves the variables of one of the blocks (which means all of the player's fort building work is undone as soon as they save/load the game... then they are zombie stew). Worth noting is that these clones are all created in-game, so I cannot edit their variables in the editor (as far as I can tell so far).

I am sure there is a really simple way to save the x and y variables of in-game clones, but I couldn't find it and I did try! I would be really grateful if someone could push me in the right direction. I'm sorry if this was explained in a previous topic, I did have a look but I didn't find it.

Many thanks,
Chris

Re: Saving Clone Positions

PostPosted: Fri Oct 22, 2010 12:33 am
by DarkParadox
I would imagine the solution to be something like the following...
(This post is assuming you know the save/load file functions, and how to put a code in Global Code. If not I will elaborate on how to do this.)

Use the variable menu to create two new array variables (Whatever size you want, as long as the size is big enough for the amount of clones). Preferably named clone_x and clone_y (The names I will use for my example code..). Make sure you've set these variables to save (something is under save groups).


Beforehand, place this in global code. It's Feral's handy cloneID function that I use very often when handling cloned actors.
Code: Select all
char buffer[50];
char * cloneID(const char *cname, short unsigned int cindex)
{
    sprintf(buffer, "%s.%d", cname, cindex);
    return buffer;
}



Now, on the save action, use this code (or a modification of this code) to store the clones x/y values in the two arrays:

Code: Select all
int i;
for(i=0; i<ActorCount("MyClones")-1; i++) // Replace MyClones with the actor name of your cloned actor.
{
    code_x[i] = getclone(cloneID("MyClones", i))->x;
    code_y[i] = getclone(cloneID("MyClones", i))->y;
}

Of course, as with most of the codes I hand out, this is untested and off the top of my head.
If there are any problems don't hesitate to reply or send me a PM.

Re: Saving Clone Positions

PostPosted: Fri Oct 22, 2010 4:30 pm
by chrisb77
Thanks so much for your help, Darkparadox, and for your speedy reply. I am fairly familiar with the save/load functions, but I have only been able to quicksave/quickload in-game with saveVars. I think if I had a better grasp on that, I may be able to get your code (or a modification thereof) to work. Is there an existing tutorial on save/load file functions?

Once again, thanks for your help.

Chris

Re: Saving Clone Positions

PostPosted: Fri Oct 22, 2010 6:19 pm
by TSO
I don't think there is any large tutorial saving or loading details...
use dark paradox's code if you can figure it out...
should have an array in a savegroup and it'll save/load all the array numbers... probably should have a limit though so make sure array can cover number of clones and prevent poissble lag.

if it helps use some code to help view what the variables do to hopefully understand it better...
or request more assistance...

Re: Saving Clone Positions

PostPosted: Fri Oct 22, 2010 6:55 pm
by DarkParadox
As best I can, I'm going to try to provide code that should work correctly..
I realized that I was missing a variable that would be rather required and how to load and spawn; here's the best way to do it that I can think of:

Use the variable menu to create two new array variables (Whatever size you want, as long as the size is big enough for the amount of clones). Preferably named clone_x and clone_y (The names I will use for my example code..). Make sure you've set these variables to save (something is under save groups).

Alongside these variables, create a variable called "clone_c". (short for clone count)
e.g.:
Image

First you will, again, need to place Feral's cloneID() function into global code.:
Code: Select all
char buffer[50];
char * cloneID(const char *cname, short unsigned int cindex)
{
    sprintf(buffer, "%s.%d", cname, cindex);
    return buffer;
}

Image


Now, on the save action, use this code (or a modification of this code) to store the clones x/y values in the two arrays:

Just as a note, this code cannot be used if any actors of this clone have been destroyed by any means, e.g. DestroyActor().
The reason for this is that GE doesn't re-arrange the clone ID's of the actors, leaving holes which breaks the for loop when it attempts to pull the x/y value from the clone actor that doesn't exist anymore.
Code: Select all
int i;
clone_c = ActorCount("MyClones")-1; // This is saved so we can record the number of actors to spawn on load.

// Replace MyClones with the actor name of your cloned actor.
// "-1" because the actor array starts at 0, while ActorCount starts at 1.
for(i=0; i<ActorCount("MyClones")-1; i++)
{
    // Grabs the X/Y value from the clone based on the current iteration of the for() loop.
    code_x[i] = getclone(cloneID("MyClones", i))->x;
    code_y[i] = getclone(cloneID("MyClones", i))->y;
}
saveVars("Clones.sv", "CloneVars"); // Replace CloneVars with whatever save group you feel like using..


Now, as for loading the clones positions and created them, there is a limitation:
This code can be used at any time, but it will not rid you of any already-existing clones.
Code: Select all
loadVars("Clones.sv", "CloneVars");
int i;
for(i=0; i<clone_c; i++)
{
    CreateActor("testactor", "icon", "(none)", "(none)", clone_x[i], clone_y[i], false);
}


This code of course, is untested like the previous codes, but should still work.

Re: Saving Clone Positions

PostPosted: Sun Oct 24, 2010 9:18 am
by chrisb77
Thanks again. As soon as I can I'll test the new code and let you know how it goes.

Chris

Re: Saving Clone Positions

PostPosted: Sun Oct 24, 2010 12:08 pm
by chrisb77
Darkparadox, I have input the codes as instructed (I have only modified the names of the variables and the actor names). GE accepted all the codes except the load function:

loadVars("Clones.sv", "CloneVars");
int i;
for(i=0; i<clone_c; i++)
{
CreateActor("testactor", "icon", "(none)", "(none)", clone_x[i], clone_y[i], false);
}


...which returns the error:

Error line 2: Unexpected declaration loadVars
Error line 3: Invalid assignment

I may have missed something out along the way. Any ideas? Thanks for your time on this.

Chris

Re: Saving Clone Positions

PostPosted: Sun Oct 24, 2010 10:27 pm
by skydereign
This would work. You have to declare variables first.

Code: Select all
int i;
loadVars("Clones.sv", "CloneVars");
for(i=0; i<clone_c; i++)
{
CreateActor("testactor", "icon", "(none)", "(none)", clone_x[i], clone_y[i], false);
}