Sign In Register

How can we help you today?

Start a new topic

Idle Game TIcks

Hey guys,


I'm trying to work out how i would set up a tick that can run when the users client is off driven by gamesparks.


I have 2 examples where i need to implement this, one is where i have my "watch an advert for a 30 minute boost" that's currently just done as a coroutine and doesn't persist once the client is closed. the 'duration' + 'multiplier' are stored in gamesparks each time the client is turned off and as well as when the advert is watched and loaded when the client is opened.


the boost is applied to an idle tick and i would like to recalculate that value "CUR_EXP" on device auth in the client.


The second example i have is an energy system. I have this stored client side with the same logic. 25 max energy, 3 minutes to give one energy.


Energy is stored as 'MAX_ENERGY' as well as 'CUR_ENERGY' in gamesparks.


I'm sure this already exists as a function within GameSparks i just haven't come across this yet.


Currently all above stats are being stored in the privatedata of the player.


Is anyone able to assist in how i would have an offline generation of these two stats, potential code would definitely cut out a lot of time for me but if you don't have the time just linking a tutorial would also be useful :)


Thanks,

Ryan


So this is what i think is a pretty inefficient way of doing this, but at least it works, i would've liked to have kept this all as one event i suppose, but oh well no big deal.


Here's code for anyone who never needs a bit of a hack job in the future:


 

//in here, old time is the time i logged off last.
// new time is the time i just logged in with.
var OldNewTime = Spark.getPlayer().getPrivateData("NEW_TIME"); //Get the playerData object
Spark.setScriptData("NEW_TIME", OldNewTime); // return the player data via script-data

var OLD_TIME = OldNewTime;
var NEW_TIME;

NEW_TIME = Date.now();

var TIME_DIF = NEW_TIME - OLD_TIME;


Spark.getPlayer().setPrivateData("NEW_TIME", NEW_TIME); //Save the data in the player's personal private data object
Spark.getPlayer().setPrivateData("TIME_DIF", TIME_DIF);

Cheers,

Ryan


1 person likes this

So the above code has a few errors in it when you actually try to export it into unity.


Unity likes having a save event and a load event so i'm using this as my save event, this has no attributes in the event just cloud code called 


 

var OldNewTime = Spark.getPlayer().getPrivateData("NEW_TIME"); //Get the playerData object


var OLD_TIME = OldNewTime;
var NEW_TIME = Date.now();

var TIME_DIF = NEW_TIME - OLD_TIME;

var TimeData = {
    "TIME_DIF": TIME_DIF
};
Spark.getPlayer().setPrivateData("TimeData", TimeData);

Spark.getPlayer().setPrivateData("NEW_TIME", NEW_TIME); //Save the data in the player's personal private data object

 i then just call this in unity without loading back data.


After that i have a Load_Time_Dif event which also doesn't have any events but just has the cloud code:


 

var TimeData = Spark.getPlayer().getPrivateData("TimeData"); //Get the playerData object
Spark.setScriptData("TimeData", TimeData); // return the player data via script-data

 

In unity i'm calling both of these once the device is authenticated (calc followed by load) then i'm calling the calc side OnApplicationQuit()


this way i'm getting the 'NewTime' ready to be compared the next time i log in.


at that point i am just dividing it by 1000 to get it into seconds then 480 to get an energy point every 8 minutes, i'll probably play around with it but it doesn't need to be complicated for the game im doing :)


Hopefully that'll give anyone who is doing this an idea of how to do this - if someone more experienced than me has a better way / any improvements feel free to add it below.


Cheers,

Ryan

Hey Ryan,


I'm not sure what you mean about unity needing a save/load event, I would handle most of this using cloud code.


Im not sure how you are running the ticks in your game, but I have 5 second ticks in my game so the offline calculations work something like:


Last tick processed is saved every time I process a tick.

Ticks are only processed on the server on a purchase that effects the income of that tick, otherwise its a display in unity.

On disconnection, all ticks are processed and last tick is saved.

OnAuthentication request, I calculate the amount of tick that have passed since last tick ((now - last tick) / tickTime), I then add number of ticks * tick time to the last tick processed.

Because this is on request, when the player receives response, they have all the updated data.

Okay so update,


I can get my server time through Date.Now()


so what i've got is a cloudscript called CALC_TIME_DIFF and what i'm aiming to do is call this once when logging off and then again when logging on.


there's 3 variables i care about, TIME_OLD, TIME_NEW, TIME_DIFF.


At the start of my event i need to load in my player data then set the TIME_OLD = TIME_NEW


Once that is done i can set TIME_NEW = Date.now();


TIME_DIFF is then TIME_NEW - TIME_OLD.


The issue i'm struggling with is, i don't know how to tell TIME_OLD to equal the stored value of TIME_NEW that's stored in my playerdata.


So far here's what i have...


 

var TimeData = Spark.getPlayer().getPrivateData("TimeData"); //Get the playerData object

Spark.setScriptData("TimeData", TimeData); // return the player data via script-data



var OLD_TIME;

var NEW_TIME;



OLD_TIME = TimeData[NEW_TIME];



var TIME_DIF = 0;







OLD_TIME = NEW_TIME;



NEW_TIME = Date.now();



TIME_DIF = NEW_TIME - OLD_TIME;



var TimeData = {

"NEW_TIME": NEW_TIME,

"OLD_TIME": OLD_TIME,

"TIME_DIF": TIME_DIF

}; // We pack the data in an object



Spark.getPlayer().setPrivateData("TimeData", TimeData); //Save the data in the player's personal private data object

 So TimeData[TIME_NEW] doesn't give back a value even though it doesn't error.


is anyone able to help me out, i want to use the information from my playerData previously stored in this cloudcode.


Thanks,

Ryan

Yes, that's how I did it for the similar feature on our game, good job!

Login to post a comment