Cookies in TGB?
by Isaac Barbosa · in Torque Game Builder · 02/01/2007 (10:36 am) · 26 replies
I want to save some cookies into the player computer to keep tracking of some variable to use when a game is restarted, but for know I don't know how to do that... the approach I want to take now is use and object and its variables as the cookie itself, saving and loading, but I'm sure that this is not the best approach. Would somebody point me in the right direction?
Thanks
Thanks
#2
02/01/2007 (11:05 am)
I'd just write it out to a file. I don't think you can do "cookies" in the web sense. But it does file support. After each game just increment it and check it to see if <= 100.
#3
I found now th File I/O tutorial :)
I will try that right now.
02/01/2007 (11:13 am)
Thanks Chip!I found now th File I/O tutorial :)
I will try that right now.
#4
Ok, I understand now how to write text. But not how to write and save a variable :(
And if I want to save lots of variables? is there a way to write a specific line, in this case a specific variable?
Thanks again
02/01/2007 (11:30 am)
function writeFile()
{
%file = new FileObject();
if(%file.openForWrite("~/data/files/test.txt"))
{
%file.writeLine("Hello World!");
echo("File Written");
}
else
{
error("File is not open for writing");
}
%file.close();
%file.delete();
}Ok, I understand now how to write text. But not how to write and save a variable :(
And if I want to save lots of variables? is there a way to write a specific line, in this case a specific variable?
Thanks again
#5
Then just be sure to exec() the .cs file. You can even delete the cs file after you exec it so the actual script doesn't stay there for long and it becomes the binary .dso file.
02/01/2007 (11:32 am)
Another way to acheive this without using the File I/O is objects .save function... for example:new SciptObject(GameData);
GameData.currentLevel = 99;
GameData.trophyCount = 10;
GameData.save("~/data/savedInfo.cs");Then just be sure to exec() the .cs file. You can even delete the cs file after you exec it so the actual script doesn't stay there for long and it becomes the binary .dso file.
#6
So I will be able to save any variable I want as a global variable and use it when the savedInfo.cs file is read?
I will experiment with this later today. This seems just like what I'm looking.
02/01/2007 (11:39 am)
Ah... that sounds pretty neat Matthew.So I will be able to save any variable I want as a global variable and use it when the savedInfo.cs file is read?
I will experiment with this later today. This seems just like what I'm looking.
#7
So just attach your data to "GameData" (much cleaner than global variables also) and then you can save it on exit and exec it on load to keep persistent data.
02/01/2007 (11:46 am)
Not quite... all objects have a .save() in TGB. So you can create ScriptObjects and save them out to store data. You can do the same for any other objects though ScriptObjects work nice since they are there just for data manipulation. So just attach your data to "GameData" (much cleaner than global variables also) and then you can save it on exit and exec it on load to keep persistent data.
#8
02/01/2007 (12:00 pm)
I learnt something as well from that. Thanks Matt!
#9
Would you be able to place an example on how you use this method to save, read and update a global variable? I can't grasp the idea from the docs.
@Matthew: I was unable to understand that gameData thing :(
02/01/2007 (3:37 pm)
Hey Chip :)Would you be able to place an example on how you use this method to save, read and update a global variable? I can't grasp the idea from the docs.
@Matthew: I was unable to understand that gameData thing :(
#10
tdn.garagegames.com/wiki/TGB/ScriptTutorials/ScoreTutorial
and there is a File I/O tutorial sample. I will try that tomorrow. Too tired now :(
02/01/2007 (4:19 pm)
Hopefully I found this:tdn.garagegames.com/wiki/TGB/ScriptTutorials/ScoreTutorial
and there is a File I/O tutorial sample. I will try that tomorrow. Too tired now :(
#11
@Isaac: Sorry you didn't... let me try and explain more.
All objects have a "save" method on them... This includes Static Sprites, Animated Sprites, Scrollers, and even non visible objects like ScriptObject. Basically a "ScriptObject" is just a dummy object that you can create to store data... like this:
Now we have an object called "GameData". Since it's an object we can store fields on it... say you want to store the current level, the ammount of trophies, difficulty setting, etc... anything you want, like this:
Now we have this data stored... to save it out you simply can do this:
Now if I browse to my game directory, then into the "data/savedInfo" I see a gameInfo.cs. The file looks like this:
Now if I throw this line in my start up scripts then every time my game starts it re-loads this data that I save out.
[/code]
02/01/2007 (4:27 pm)
@Chip: Glad you did :)@Isaac: Sorry you didn't... let me try and explain more.
All objects have a "save" method on them... This includes Static Sprites, Animated Sprites, Scrollers, and even non visible objects like ScriptObject. Basically a "ScriptObject" is just a dummy object that you can create to store data... like this:
new ScriptObject(GameData);
Now we have an object called "GameData". Since it's an object we can store fields on it... say you want to store the current level, the ammount of trophies, difficulty setting, etc... anything you want, like this:
GameData.currentLevel = 99; GameData.trophyCount = 15; GameData.difficulty = 3;
Now we have this data stored... to save it out you simply can do this:
GameData.save("data/savedInfo/gameInfo.cs");Now if I browse to my game directory, then into the "data/savedInfo" I see a gameInfo.cs. The file looks like this:
//--- OBJECT WRITE BEGIN ---
new ScriptObject(GameData) {
currentLevel = "99";
difficulty = "3";
trophyCount = "15";
};
//--- OBJECT WRITE END ---
I just saved my data. This is a much easier way to save simple data than the File I/O. To re-load this data all I have to do is use this command:
[code]
exec("data/savedInfo/gameInfo.cs");Now if I throw this line in my start up scripts then every time my game starts it re-loads this data that I save out.
[/code]
#12
and then store data on it
An easy way to save both out and load t hem in at once is using a "SimSet"... like this:
This creates a SimSet... adds our two ScriptObjects to it, then saves it out... the resulting file would be:
02/01/2007 (4:32 pm)
Now even a step deeper... (if you didn't understand the above then ignore this for now). Say you wanted to save out multiple ScriptObjects. Say we have the GameData one with the data from above... plus we create one for player profile like this:new ScriptObject(PlayerProfile);
and then store data on it
PlayerProfile.name = "King_BoB"; PlayerProfile.colorTheme = "red";
An easy way to save both out and load t hem in at once is using a "SimSet"... like this:
new SimSet(GameDataSet);
GameDataSet.add(GameData);
GameDataSet.add(PlayerProfile);
GameDataSet.save("data/savedInfo/gameDataSet.cs");This creates a SimSet... adds our two ScriptObjects to it, then saves it out... the resulting file would be:
//--- OBJECT WRITE BEGIN ---
new SimSet(GameDataSet) {
canSaveDynamicFields = "1";
new ScriptObject(GameData) {
currentLevel = "99";
difficulty = "3";
trophyCount = "15";
};
new ScriptObject(PlayerProfile) {
colorTheme = "red";
name = "King_BoB";
};
};
//--- OBJECT WRITE END ---
#13
02/02/2007 (8:58 am)
@ Matthew: Those last explanations are very clear. This is what I was looking for. If TGB documentation were completed with examples like this it will be AWESOME! Thanks.
#14
For example, in the saving of the simset, it assumes I've already created/loaded my game data. However, later in the example I could just call exec() to create/load my data in the simset automatically.
02/02/2007 (10:48 am)
Nice posts. However, loading the data seems weird too me. For example, in the saving of the simset, it assumes I've already created/loaded my game data. However, later in the example I could just call exec() to create/load my data in the simset automatically.
#15
Heh..I'm confused. How could you save data if the data hasn't been created or loaded yet? That's a pretty basic assumption--have to actually have data to save it.
02/02/2007 (10:54 am)
Quote:
For example, in the saving of the simset, it assumes I've already created/loaded my game data. However, later in the example I could just call exec() to create/load my data in the simset automatically.
Heh..I'm confused. How could you save data if the data hasn't been created or loaded yet? That's a pretty basic assumption--have to actually have data to save it.
#16
Very true... and you are right that were you to implement something like I suggested you would already have the .cs file there with the default data, so you never would need to create the SimSet in the console. That was purely for example purpose, though before you depend on having the file you may just create it from the console and save it out like I exampled and then assume from that point on that there's a a file with that SimSet. Then you simply just add whatever data ScriptObjects you want to that SimSet to be sure they're saved out... a good simle check can help as well, like this:
So you don't have to worry whether or not the object is already there.
02/02/2007 (10:54 am)
Quote:later in the example I could just call exec() to create/load my data in the simset automatically.
Very true... and you are right that were you to implement something like I suggested you would already have the .cs file there with the default data, so you never would need to create the SimSet in the console. That was purely for example purpose, though before you depend on having the file you may just create it from the console and save it out like I exampled and then assume from that point on that there's a a file with that SimSet. Then you simply just add whatever data ScriptObjects you want to that SimSet to be sure they're saved out... a good simle check can help as well, like this:
if(!isObject(GameData)) new ScriptObject(GameData); GameData.currentLevel = 101;
So you don't have to worry whether or not the object is already there.
#17
Appreciate the extra input.
02/02/2007 (1:49 pm)
Got it. So by some design, either from config datablocks, or a default cs file, the "data" gets initialized. From there, saving and loading using built in .save() and exec() like above can be used.Appreciate the extra input.
#18
So I can use this to check if a variable exists? This works as load()? where is the load function? Which is the right way to d onot overwrite your existing data instead of just load and refresh with new data?
02/02/2007 (5:46 pm)
Quote:if(!isObject(GameData))
new ScriptObject(GameData);
So I can use this to check if a variable exists? This works as load()? where is the load function? Which is the right way to d onot overwrite your existing data instead of just load and refresh with new data?
#19
I mean, if I use this code every time I start my game, I know those variables will be set as 0 every time... but if I want to update to
GameData.currentLevel = 2;
I should do something like
GameDate.currentLevel = GameData.currentLevel + $levelsPlayed?
So next time GameData is saved will be saved as 2?
Sorry if this question is confusing... I'm very confused now... maybe I need to experiment with this a few days more (the fact that confuses me is that actually since I have no a load function I don't know is just running the cs file is just like load)
Thanks
02/02/2007 (5:56 pm)
new ScriptObject(GameData)
{
GameData.currentLevel = 0;
GameData.trophyCount = 0;
GameData.difficulty = 0;
}I mean, if I use this code every time I start my game, I know those variables will be set as 0 every time... but if I want to update to
GameData.currentLevel = 2;
I should do something like
GameDate.currentLevel = GameData.currentLevel + $levelsPlayed?
So next time GameData is saved will be saved as 2?
Sorry if this question is confusing... I'm very confused now... maybe I need to experiment with this a few days more (the fact that confuses me is that actually since I have no a load function I don't know is just running the cs file is just like load)
Thanks
#20
I just comment first line and now is working, eveytime I play a game my variable is updated :) Amazing, is so easy whe you understand it!
Another question: will the gameInfo.cs file be saved with another extension to avoid somebody edit the saved data?
02/03/2007 (8:26 am)
//new ScriptObject(GameData);
GameData.gamesplayed = GameData.gamesplayed++;
GameData.save("Moverguacamole/data/savedInfo/gameInfo.cs");I just comment first line and now is working, eveytime I play a game my variable is updated :) Amazing, is so easy whe you understand it!
Another question: will the gameInfo.cs file be saved with another extension to avoid somebody edit the saved data?
Torque Owner Isaac Barbosa
IQ Games
If I want to have a trophy room in my game and I want to award a prize after play 100 games, I need to save how much games a player has played every time the player has a game session. So if player plays 90 games the first time he launch my game and close it, next time the first game he plays has to be saved as the 91th game he plays. I know how to do this in flash and I guess the logic is the same, but which one are the methods to use here? save? load?
thanks again