Forum BigDB Value Keeps Resetting Itself In C#

Discussion and help relating to the PlayerIO database solution, BigDB.

Value Keeps Resetting Itself In C#

Postby Tenko » May 3rd, 2011, 8:21 pm

*EDIT*
The problem is likely a synchronization issue. Nevermind what I wrote about references. Will post again if I fix it. Sorry about that! Thanks to the poster for replying! :mrgreen:
*EDIT*

I had to play with a lot of variables to find the solution to this problem.

Let's say that, in one function thread, a C# server loads a UInt value from BigDB, changes that value, and then attempts to save the changes in a new separate BigDB load statement.

Assuming the value of Cash is 9,000,000 in the databaseobject:
Code: Select all
PlayerIO.BigDB.Load("Players", Username, delegate(DatabaseObject result) {
// ...
Cash = result.GetUInt("Cash"); // Console.WriteLine reports Cash as 9,000,000
// ...
}
Cash += 100,000; // Console.WriteLine reports Cash as 9,100,000
PlayerIO.BigDB.Load("Players", Username, delegate(DatabaseObject result) {
// The moment this second load statement occurs, the value of Cash gets reset back to 9,000,000.
// Console.WriteLine reports Cash as 9,000,000
// ...
      result.Set("Cash", Cash); // Sets Cash to its current value of 9,000,000 instead of the correct value of 9,100,000.
// ...
);


I figured that, somehow the second BigDB load statement caused the value of Cash to reset itself, even though I never loaded that value into the Cash variable in the second BigDB statement. So the value must be getting reset due to some reference crap.

The solution is to create a new UInt variable called NewCash, and to make sure that NewCash never gets passed the reference to Cash. Simply using "NewCash = Cash;" will not work, because it passes the reference which BigDB then alters upon the second BigDB load statement. Here's how I made it work:

Code: Select all
uint NewCash;
NewCash = Cash + 10; // This ensures that a new value MUST be passed into NewCash, so that
                // the value of NewCash and Cash are not equal, and the reference is not passed.
NewCash -= 10; // This resets the value of NewCash to the value of Cash, without passing a reference.
NewCash += Cost; // This adds the amount desired. Cost equals 100,000 in my example.
result.Set("Cash", NewCash); // This correctly saves the new value 9,100,000 to BigDB.
Last edited by Tenko on May 4th, 2011, 5:33 am, edited 1 time in total.
Tenko
 
Posts: 8
Joined: November 25th, 2010, 5:26 pm

Re: Value Keeps Resetting Itself In C#

Postby cjcenizal » May 4th, 2011, 12:16 am

Tell me if I'm wrong, but is it possible that you have a synchronization problem? If you're making calls to the server (e.g. when you call the PlayerIO.BigDB.Load() method), won't your callbacks be called asynchronously?

For example, it sounds like you've checked your Console output to make sure your callbacks are indeed being called in the correct order. But if they aren't being called in the correct order, it's possible the callback in your first BigDB.load() call is occurring after you set Cash += 100000, thus overwriting it with the "incorrect" value of 9000000. This would explain the problem. What do you think, could that be it?

Also, I'm not a C# whiz, but I did some research and found this resource: http://msdn.microsoft.com/en-us/library/sbkb459w.aspx. According to this, the assignment operator assigns the value, not the reference. So I don't think this is a reference problem. I'd find it really hard to believe it would assign a reference to a primitive like an unsigned int anyway... but hey, I'm a n00b so who knows.
cjcenizal
Paid Member
 
Posts: 115
Joined: March 29th, 2011, 12:31 am

Re: Value Keeps Resetting Itself In C#

Postby Tenko » May 4th, 2011, 5:22 am

What you are saying makes perfect sense. I checked the = assignment operator and also saw that uint was an assign-by-value type. I'm still struggling with this, going to try rewriting everything to only run when the callback completes. Thank you for posting this, its probably what is wrong. I was totally assuming that the C# code "paused" on the BigDB.Load statement and would not execute further until its callback or error was finished. I see now that my assumption was most likely the cause of all my trouble?
Tenko
 
Posts: 8
Joined: November 25th, 2010, 5:26 pm

Re: Value Keeps Resetting Itself In C#

Postby cjcenizal » May 4th, 2011, 5:32 am

Glad I could be of help! I guess if there's a lesson in this, it'd be that the best assumption you can make is that if something is breaking in our code, it's almost always because we, the programmers, are the ones who are breaking it. :) I re-learn that one pretty much every day lol. :P
cjcenizal
Paid Member
 
Posts: 115
Joined: March 29th, 2011, 12:31 am

Re: Value Keeps Resetting Itself In C#

Postby Tenko » May 4th, 2011, 7:18 am

I could tell you were right from the moment I read your post.

I had two functions, each of which called BigDB 4 times in a row without pausing to give the callbacks a chance to execute. I split each function into 4 separate functions, with each additional function being called only upon a success callback from a BigDB.Load statement.

After I debugged the errors from the rewriting, and after 4 shots of vodka, I found that all the functions worked perfectly.

Thanks for posting to let me know what I was doing wrong! :mrgreen:

And yeah, It's almost always because we're the ones who are breaking the code... except for the BigDB "circular references". Those still don't make sense. Glad the solution to that is a bit easier.
Tenko
 
Posts: 8
Joined: November 25th, 2010, 5:26 pm


Return to BigDB