(SOLVED) Issues with shared objects and saving best times


(TaylorAnderson) #1

Helloo. So I want to make some of the focus of my new game be around beating levels really fast, so I thought it would make sense to save each level’s fastest time beaten as a shared object for consistency. Unfortunately I am having problems implementing this plan, and I have no idea why at this point.

Here are my shared object functions (inside my Level class):

	public function GetBestTime():Number
	{
		sharedObject = SharedObject.getLocal(levelName);
		
		if (sharedObject.data.bestTime > 0)
			bestTime = sharedObject.data.bestTime;
			
		sharedObject.flush();

		
		return bestTime;
		
	}
	public function SetBestTime(n:Number):void
	{
		sharedObject = SharedObject.getLocal(levelName);
		sharedObject.data.bestTime = n;
		sharedObject.flush();
	}

For context, I use the GetBestTime() function at the very top of my class to determine its bestTime variable, and I use the SetBestTime() function when the player finishes the level.


(Ultima2876) #2

Have you tried using the net.flashpunk.utils ‘Data’ class? This is basically a simplified shared object utility class that makes saving and loading data a little easier.

Docs here :slight_smile:

import net.flashpunk.utils.Data;

public function getBestTime(): Number
{
  return Data.readUint("BestTime", 0);
}

public function setBestTime(n: Number): void
{
  Data.writeUint("BestTime", n);
}

(TaylorAnderson) #3

I was not aware of this class! I will try to use that instead.


(TaylorAnderson) #4

Alright, this still isn’t working, which means its obviously my code that needs fixing.

Where I use my GetBestTime function:

	override public function added():void
	{
		
		super.added();
		bestTime = GetBestTime()
		bestTimeText.text = bestTime.toString();

Where I use my SetBestTime

	if (!setTime && (bestTime > timer || bestTime == 0))
{
	SetBestTime(timer);
	setTime = true;
					
}

(Jonathan Stoler) #5

Are you using Data.load(NAME_GOES_HERE) and, subsequently, Data.save(NAME_GOES_HERE)?


(TaylorAnderson) #6

I’m using the exact same function that Ultima described. Should I be using that method instead?


(Jonathan Stoler) #7

Use Data.load() before using any of the Data.readXXX methods so it knows which file to look for. You can, for example, put it at the front of the begin() method of your first World.

You must use Data.save() after any Data.writeXXX methods in order for your changes to be stored.

Both of these arguments take a filename as a parameter, although it is optional. However, if you don’t specify a filename in any of your games, there will be conflicts between games on the same domain, since they’ll all write to the default savefile. So I strongly recommend you use one, and it’s good practice anyway.

You’ll need to pass your filename (which is essentially an arbitrary string, but it would be a good idea to name it after your game) as a parameter to both Data.load() and Data.save() to make sure they’re pointing to the file you want to read/write to.


(TaylorAnderson) #8

Alright, I will implement this and give you an update. Thanks!


(TaylorAnderson) #9

OH! That worked! Thanks so much!!! I was getting so frustrated. Thank you! I will consider this topic SOLVED.


(Ultima2876) #10

This topic was automatically closed after 3 days. New replies are no longer allowed.