Be a Supporter!

Api Save Data

  • 1,235 Views
  • 12 Replies
New Topic
OctopusSandwich
OctopusSandwich
  • Member since: Dec. 5, 2003
  • Offline.
Forum Stats
Member
Level 13
Programmer
Api Save Data 2012-02-24 22:57:54

I am making a game where you can create things and share them publicly with Save Data but I'm having some trouble. I can get it so you can edit the name and the description of the file fine, but how do I make the actual file created save with it?

mike
mike
  • Member since: Feb. 24, 2000
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to Api Save Data 2012-02-27 13:27:50

Sorry that the documentation is still a little lax on this stuff, I promise to write it up soon! In the meanttime feel free to ask me any questions.

file.data is where you store the actual data. The data can be an Object, String, BitmapData, or ByteArray. Then you can call file.save() to save the file:

(This is AS3, but it works exactly the same in AS2)

import com.newgrounds.*;
var file : SaveFile;
file.name = "Test!";
file.description = "A file";
file.data = {foo: 10, blah: "haha"};
file.addEventListener(APIEvent.FILE_SAVED, onFileSaved);
file.save();

function onFileSaved(event : APIEvent) : void
{
  if(event.success) { trace("File saved!"); }
}
OctopusSandwich
OctopusSandwich
  • Member since: Dec. 5, 2003
  • Offline.
Forum Stats
Member
Level 13
Programmer
Response to Api Save Data 2012-02-27 15:14:50

At 1 hour ago, mike wrote: Sorry that the documentation is still a little lax on this stuff, I promise to write it up soon! In the meanttime feel free to ask me any questions.

file.data is where you store the actual data. The data can be an Object, String, BitmapData, or ByteArray. Then you can call file.save() to save the file:

(This is AS3, but it works exactly the same in AS2)

So in "file.data = {foo: 10, blah: "haha"};" where do I put the actual file variable name and where do I put the frame that you view the actual file on? Sorry, that one line is really confusing me.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Api Save Data 2012-02-27 15:43:27

At 9 minutes ago, OctopusSandwich wrote: So in "file.data = {foo: 10, blah: "haha"};" where do I put the actual file variable name and where do I put the frame that you view the actual file on? Sorry, that one line is really confusing me.

The section of code inside the curly braces is creating an Object (i.e. an object of type Object).
The indices of the Object are defines as "foo" and "blah", with the values 10 and "haha" respectively.

So essentially what you are looking at is this: {index : value}.
The index of an Object can only be a String. If a non-String is given then it will be converted to one, examples:

var foo:Object = {42:"hello world}; //index will be "42"
var bar:Object = {helloWorld:"hello world}; //index will be "helloWorld"

Watch out when trying to use a variable's value as the index though, it can produce unexpected results:

var foo:int = 1234;
var bar:Object = {foo:foo}; //index will be "foo", not "1234"
var baz:Object = {(foo.toString()):foo}; //index will be "1234"
var glub:Object = {(String(foo)):foo}; //index will be "1234"

So, if you wanted to store a frame number, you would just do something like:

var frame:int = 42;
file.data = {frameNumber:frame};

You can access the values of an Object the same way you would with an Array:

var foo:Object = {a:1, b:2, c:3};
trace(foo["a"], foo["b"], foo["c"]); //Output: 1, 2, 3

You can also access them like this:

var foo:Object = {a:1, b:2, c:3};
trace(foo.a, foo.b, foo.c); //Output: 1, 2, 3
OctopusSandwich
OctopusSandwich
  • Member since: Dec. 5, 2003
  • Offline.
Forum Stats
Member
Level 13
Programmer
Response to Api Save Data 2012-02-27 16:14:22

I probably should have mentioned that the movie clips instance that will be saved is "OctopusSandwich" and the 'view page' for the saved file to load on and to vote on the file after clicking on it from the Save File Browser is on frame 8. If I have to upload the fla I will cause I'm not understanding any of this.

on (release) {
import com.newgrounds.*;
var file:SaveFile = API.createSaveFile("Octopus Sandwich");
file.name = FileName;
file.description = FileDescription;
file.data = {foo: 10, blah: "haha"};
file.createIcon(icon);
file.save();

file.addEventListener(APIEvent.FILE_SAVED, onFileSaved);
function onFileSave(event:APIEvent) {
	if(event.success)
		trace("File saved!");
}
}
mike
mike
  • Member since: Feb. 24, 2000
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to Api Save Data 2012-02-27 19:00:00

Hey Octopus,
Your code looks pretty close, what is happening? Does the API spit out any errors messages in the Output panel? It'd be helpful if you upload your game to the preview area in the project page and passed me a link! The only thing I would change is to move the onFileSaved function outside the button onto the root timeline.

At 2 hours ago, OctopusSandwich wrote: I probably should have mentioned that the movie clips instance that will be saved is "OctopusSandwich"

I'm not sure I follow you here -- you can't save a movieclip, you have to save something like a String or an Object. This is what you put into file.data. If you wanted to save the position of a movieclip, you could do something like this:

file.data = {player_x: player._x, player_y: player._y};

And then you can restore the player's position after you load the file:

player._x = file.data.player_x;
player._y = file.data.player_y;

Does that make sense?

Jin
Jin
  • Member since: Sep. 9, 2006
  • Offline.
Forum Stats
Supporter
Level 50
Blank Slate
Response to Api Save Data 2012-02-29 11:27:39

What do these do, and how are they used?
saveLocal
saveGroups

How do you know which scoreboard the score was posted to in the APIEvent.SCORE_POSTED and SCORES_LOADED handler?


BBS Signature
mike
mike
  • Member since: Feb. 24, 2000
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to Api Save Data 2012-02-29 15:15:25

At 7 hours ago, MikeyS9607 wrote: Yo mike, do you know how to attach an icon in AS2 to save files? Say I have an image with an identifier of "saveicon.jpg"?

Hey Mikey, you can do it like this:

saveFile.createIcon(icon); // icon can be a BitmapData or MovieClip, it's automatically scaled to fit icon size.

mike
mike
  • Member since: Feb. 24, 2000
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to Api Save Data 2012-02-29 15:24:49

At 3 hours ago, Jin wrote: What do these do, and how are they used?
saveLocal

saveLocal saves some data locally in a shared object and encrypts it. It works like this:

saveLocal("myData", {foo: 5});
var myData = loadLocal("myData");

This is a relic from the early days of the API -- the actual SaveFile system only works if the user is logged-in on Newgrounds. We do have plans to unify these into one system so that if a person is not online/logged-in, you can still use the SaveFile system exactly as if they were online, and it will just save locally in a shared object.

saveGroups

A SaveGroup is just a folder for SaveFiles. For example, you might have a SaveGroup called "Levels" where user-created levels are stored.

You can create a save group under the Save Data section of the API Tools page in your project. After that, you should be able to save and load files, like this:

var saveFile : SaveFile = API.createSaveFile("Levels"); // Levels is the save group name
saveFile.name = "My Level";
saveFile.data = {foo: blah}; // actual file data
saveFile.createIcon(myIcon);
saveFile.save();
How do you know which scoreboard the score was posted to in the APIEvent.SCORE_POSTED and SCORES_LOADED handler?

For SCORES_LOADED, event.data will be the scoreboard that was loaded. You can then look at scoreboard.name, scoreboard.scores, etc. to display it.

For SCORES_POSTED, event.data.scoreBoard is the scoreboard.

Note that you can also add the event listener to a specific scoreboard instead of the API if you just want to listen to that particular scoreboard, like this:
scoreBoard.addEventListener(APIEvent.SCORES_LOADED, onScoresLoaded);

Jin
Jin
  • Member since: Sep. 9, 2006
  • Offline.
Forum Stats
Supporter
Level 50
Blank Slate
Response to Api Save Data 2012-03-13 07:24:58

How do I give the play button on the FlashAd a function?


BBS Signature
FiendMachine
FiendMachine
  • Member since: Jul. 13, 2010
  • Offline.
Forum Stats
Member
Level 21
Game Developer
Response to Api Save Data 2014-12-25 05:11:48

At 2/29/12 03:24 PM, mike wrote:

Is it possible to save a whole movie clip which has the level in it,created by the user?
Or do I have to save all the variables of the movieclip and then load the level using the variables?


BBS Signature
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Api Save Data 2014-12-25 05:51:58

At 12/25/14 05:11 AM, FiendMachine wrote: Is it possible to save a whole movie clip which has the level in it,created by the user?
Or do I have to save all the variables of the movieclip and then load the level using the variables?

You will have to save all variables.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Api Save Data 2014-12-25 07:40:27

At 12/25/14 05:11 AM, FiendMachine wrote:
At 2/29/12 03:24 PM, mike wrote:
Is it possible to save a whole movie clip which has the level in it,created by the user?
Or do I have to save all the variables of the movieclip and then load the level using the variables?

Please check the dates on threads before posting.