0

I am trying to reference a shared object (one that saves data), but whenever I try to, I get a crash.

This code works fine:

var var1:Object = { value:1 };
var varRef:Object = var1;

if(var1.value == 1) {
    varRef.value = 50;
}

trace(varRef.value); // outputs 50;
trace(var1.value); // outputs 50;

But when I try to use shared objects, it doesn't work.

import flash.net.SharedObject;
var iapso:SharedObject = SharedObject.getLocal("purchases");
var varRef:Object = iapso.data.testing;

varRef = 90
trace ("The shared value is " + iapso.data.testing);
trace ("This should mirror it" + varRef);

If you're able to figure out the issue, please post a fixed version.

Thanks.

0

2 Answers 2

4

You must come from a programming background where pointers can be dereferenced.

In this case, varRef is not varRef = &iapso. Setting its value does not change the value of iapso.data.testing;

Initially, you set varRef as a pointer to iapso.data.testing:

var varRef:Object = iapso.data.testing;

Then, you immediately change it to a constant value object literal 90:

varRef = 90;

This does not set the value of testing to 90 - this changes the value of varRef.

You could set varRef to iapso.data, then set testing as in:

var varRef:Object = iapso.data;
varRef.testing = 90;

Then, the following would produced expected results:

trace(iapso.data.testing); // 90
trace(varRef.testing); // 90
Sign up to request clarification or add additional context in comments.

1 Comment

Jason Sturges you did it again! Great answer.
2

setting varRef will not update the value in iapso.data - numbers are copied, not referenced.

var bar:Object = { data:1 };
var foo:Object = bar.data;

trace(foo); //"1"
bar.data = 2;
trace(foo); //"1"

If you really want to use a reference, use data instead, eg:

import flash.net.SharedObject;
var iapso:SharedObject = SharedObject.getLocal("purchases");
var varRef:Object = iapso.data;

varRef.testing = 90
trace ("The shared value is " + iapso.data.testing);
trace ("This should mirror it" + varRef.testing);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.