View Full Version : Flash -- text files
djr33
06-17-2006, 11:20 AM
For a project, I need to be able to save the current values of variables in a text file, then load them again.
Using the function load() and specifying the text file, it's quite easy to get the values back, using a format similar to the get method of the web (var1=val1&var2=val2&...).
However, the send() function, which seems to be the inverse of load(), only sends get or post data.
Load will work with the text files, then, but send will just send post data to a text document which will, if lucky, do nothing or, if unlucky, give you errors or crash or something.
So.... ironically, it would be easy in this case to do it on the web with php, etc, but the text file needs to be the output for this as it will be local. No webpages should be used.
also, the other hard part will be not just adding to the .txt, but actually creating a new .txt when you save.
Any ideas?
One thing I thought of was if there's a way to code a program in C++ or somethign that would be able to accept POST data, but that seems quite complex.
What format do you want them in? You can code a program to do it, but it would have to run as a web service.
PHP doesn't have to be run on a server, you know :)
djr33
06-17-2006, 08:27 PM
Yeah, I know you can install php locally, but it's more work than people would want to do.
This isn't a personal thing, but something that will be distributed to people, many of whom would probably have no idea how to (or interest in) installing php to their computer.
.txt format would be great. perhaps coming up with a new extension would be nice, just so they don't think it's a text file. Maybe .fsv (or .fsave), or something. But that shouldn't be hard to change, right?
But... for formatting of the info itself, just a long string of var=val&var=var.... with no linebreaks/spaces/etc. would be great.
.txt format would be great. perhaps coming up with a new extension would be nice, just so they don't think it's a text file. Maybe .fsv (or .fsave), or something. But that shouldn't be hard to change, right?A format isn't the same as an extension. :)
Yeah, I know you can install php locally, but it's more work than people would want to do.Well, it sounds to me as if the function you're suggesting you use can only handle sending requests via HTTP.
djr33
06-17-2006, 09:17 PM
A format isn't the same as an extension.Right, but sometimes it's easier to save a text file as .txt than it would be to save it as .jpg, for example. Some programs and/or options make custom extensions a bit harder to use. But.. yeah, I'd think it possible if coding a custom app.
It could send locally.... you could sent it to a file on the harddrive. Maybe with javascript, it would work.
however, it would be nice to not to rely on JS.
You mentioned making a program... could it accept POST data?
You mentioned making a program... could it accept POST data?Yes. But this would essentially mean building a mini HTTP server, which is a remarkably ugly and inefficient way of doing things.
It could send locally.... you could sent it to a file on the harddrive. Maybe with javascript, it would work.GET requests cannot be sent to local files.
djr33
06-18-2006, 10:03 AM
GET requests.... not even if you have php installed? With a php file? Why is that?
But.... flash also offers POST. Is it the same with that, though?
....a remarkably ugly and inefficient way of doing things.Yeah, but what's your better suggestion?
I'll keep looking before I dive into that.
Just can't find anything on google.. I've looked a lot.
not even if you have php installed? With a php file? Why is that?
But.... flash also offers POST. Is it the same with that, though?You'd have to understand the basic workings of HTTP to understand this. To send a request via HTTP, be it GET, POST, HEAD, whatever, the program (or script, in your case) must connect to an HTTP server, to which it sends something like this (to get http://mysite.com/dir/fish.html):
GET /dir/fish.html HTTP/1.1
Host: mysite.com
Note the two linebreaks on the end. The server then responds with some information about the resource and the result of the request:
HTTP/1.x 200 OK
Date: Sun, 18 Jun 2006 10:31:16 GMT
Server: Apache
Connection: Close
Content-Type: text/html;charset=Shift_JIS
(again, note the two linebreaks on the end), before proceeding to send the requested data. Both examples were simplified for the purpose of demonstration, but perfectly accurate; you can try it in telnet if you're a fast typist. As you can see. HTTP is very different from accessing a local file, so unless there's a webserver running on the local computer, it's not going to happen. Flash may provide some sort of HTTP-styled wrapper for opening local files, but I doubt it; from what you say (that it offers both GET and POST to local files) I think that you're simply wrong. :)
I daresay it has a more suitable function for accessing local files. You should go through the docs again.
djr33
06-18-2006, 10:41 AM
Yeah, I'll definitely keep looking. I've just found the load function which does work locally, but the send only does webbased (get/post) sending.
Google doesn't help when "flash text file" is searched for, and there isn't much info it. I don't doubt there is another function, but I haven't found any info on it.
I know HTTP is different, but thanks for the specific example.
Bottom line... you can't use a standalone file (like an .exe) to accept POST (etc) data, right? I suspected this, but wanted to be sure.
I didn't say it offered GET and POST for local files, but just that it offers both and you could supply a relative or local URL if you desired. Not saying that it would neccessarily work.
Bottom line... you can't use a standalone file (like an .exe) to accept POST (etc) data, right?You can, but it must use a socket connection somewhere along the line (I.E. either run as a CGI program or act as its own webserver).
djr33
06-18-2006, 10:48 AM
or act as its own webserverSo... a standalone .exe could be it's own?
That would be crazy to code, though, right?
Remember, this would need to be easy to use, as it's going to be distributed with the flash file to use, not just be used by someone who could install or format things for it.
Other ideas? Something I'm not thinking of searching for in google?
So... a standalone .exe could be it's own?
That would be crazy to code, though, right?Not really... a basic webserver such as you're talking about wouldn't be too difficult to implement. It would be a completely ridiculous proposition, though -- firewalls would have all sorts of problems, for a start, and I'm sure there must be a way of doing this properly. It would be like building a castle so you could hang your hat on a hook in one of the rooms.
djr33
06-18-2006, 10:56 AM
Sure. Not saying it isn't a big thing for something fairly little.
I'll look more at flash.
Hope I find something better for this. Just haven't had much luck so far.
BLiZZaRD
06-19-2006, 07:29 PM
Not sure I FULLY understand what exactly you are trying to accomplish here... BUT...
You sound like a very intelligent person, so reading between the lines and manipulating what you need to get the results you are after shouldn't be too tough.
If you are looking for the ability to save things locally on someone's PC, using flash... try shared objects.
HERE (http://www.flashkit.com/tutorials/Games/Saving_G-Ruben_Ph-918/index.php) is a short tutorial with coding.
And This One (http://www.flashkit.com/tutorials/Games/SaveLoa-fwe4life-1037/index.php) has more detail and uses cookies, which are .txt files yes?
This One (http://www.flashkit.com/tutorials/Backend/mySQL_an-Jeffrey_-635/index.php) uses SQL and php to store data such as player position, inventory, etc. WIth good instructions, shouldn't be too hard to tweak.
Finally this one (http://www.flashkit.com/tutorials/Games/High-sco-Glen_Rho-657/index.php) is your basic highscore list, which I have used many time (after getting to work properly with php4+) Again, using INSERT and POST/GET to store info to a .sco file (can be renamed to .txt if needed)
if you look at the last one, download the zip file and replace the scores.php with this:
<?php
error_reporting(E_ALL);
function getPlayerScore() {
// get the winscore variable from the query string - make sure it's an integer
$score = (isset($_GET['winscore'])) ? (int) $_GET['winscore'] : 0;
// get the winname variable removing leading and trailing whitespace and the | character
$name = (isset($_GET['winname'])) ? trim(str_replace('|', ' ', $_GET['winname'])) : '';
// high score records are stored as score|name
return $score . '|' . $name;
}
function getAction() {
// an array containing the possible actions
$actions = array('VIEW', 'INSERT', 'CLEAR');
$action = (isset($_GET['action'])) ? trim(strtoupper($_GET['action'])) : 'VIEW';
// make sure the action is valid
if (!in_array($action, $actions)) {
$action = 'VIEW';
}
return $action;
}
function createFile($filename) {
// if a the file doesn't exist try to create it
if (!file_exists($filename)) {
$fp = fopen($filename, 'w');
if (!$fp) {
// the file couldn't be created
return false;
}
fclose($fp);
}
// if we got here the file is ok
return true;
}
function writeFlash($name, $value, $exit = false) {
// write out a variable name and value to be read by flash
echo '&' . $name . '=' . urlencode($value) . '&';
// if $exit has been set stop the script right here
if ($exit) {
exit();
}
}
function writeFile($filename, $str) {
// write a string to a file
$fp = fopen($filename, 'w');
fwrite($fp, $str);
fclose($fp);
}
function process() {
// define the location of the highscores file
$scoreFile = 'scores/highscores.sco';
// and define the number of results
$scoreSize = 10;
// get the action so we know what to do!
$action = getAction();
// try to create the highscores file if it doesn't exist already
if (!createFile($scoreFile)) {
// if the file didn't exist and can't be created return an error message
writeFlash('status', 'There was an error accessing the highscores list', true);
}
if ($action == 'CLEAR') {
// clear the scores file
writeFile($scoreFile, '');
// and send a message back to flash
writeFlash('status', 'The list has been cleared', true);
}
// read the scores in the file into an array
$fileScores = array_map('trim', file($scoreFile));
if ($action == 'INSERT') {
// if we've got a score to add to the end of it
$fileScores[] = getPlayerScore();
}
// an create a couple of empty arrays that we'll use for sorting our data
$scores = array();
$names = array();
foreach ($fileScores as $scoreData) {
// put the scores and names into the 2 arrays
list($scores[], $names[]) = explode('|', $scoreData);
}
// sort it! get $fileScores sorted so the scores are in descending order
array_multisort($scores, SORT_DESC, $names, SORT_ASC, $fileScores);
// if we have too many scores trim off the lowest ones
array_splice($fileScores, $scoreSize, count($fileScores) - $scoreSize);
// write the content of the fileScores array into the file (with each score|name separated by a newline)
writeFile($scoreFile, implode("\n", $fileScores));
// loop through the scores
foreach ($fileScores as $key => $scoreData) {
$tmp = explode('|', $scoreData);
// and write out a list of variables score0, name0, score1, name1 etc...
writeFlash('score' . $key, $tmp[0]);
writeFlash('name' . $key, $tmp[1]);
}
writeFlash('status', 'things are fine');
}
process();
?>
Of course, all of these are meant for games, but again, easily tweaked for your needs... that is, IF I understood what you wanted to do.
Again, using INSERT and POST/GET to store info to a .sco file(a hundred thousand Linux geeks team up to stop it ever becoming a recognised file format)
This INSERT would be what's being looked for, at a guess.
BLiZZaRD
06-19-2006, 08:06 PM
Does file format REALLY matter when you are writing/reading an interal file? It is just a name and declaration, yes?
I mean I could tell my Flash to get data from file.dogpoo, and as long as file.dogpoo exsits where I told it to look for it, it should read the info either way...
This, of course on a GRAND generic scale...
I mean I could tell my Flash to get data from file.dogpoo, and as long as file.dogpoo exsits where I told it to look for it, it should read the info either way...As I've said above: the extension doesn't matter, it's only an indicator of the format of the file and doesn't necessarily have any bearing on the contents.
I was talking about the massive SCO debate... don't worry, there's sure to be someone who'll appreciate it. :p
djr33
06-20-2006, 12:27 AM
I didn't read through everything above, but I will when I get a chance. Thanks.
However, you're giving me a php code, which is exactly what I can't use... this needs to be local.
Is that just an example of what could be a non-php script?
EDIT: ok... just looked a little more. cookies won't work because this needs to be distributed to people, and asking them to check their cookies for the file is a bit much for not-so-advanced computer users.
Also, cookies are cleared, so you'd need to manually find the cookie and change it to avoid it getting deleted the next time you clear your cookies from your browser.
Thanks again for the links, though... maybe one will prove useful in finding a local way to do it.
The top link is pure ActionScript. It also appears to have obtained its information from an internetsite site, so it must be good. :)
BLiZZaRD
06-20-2006, 12:40 AM
EDIT: ok... just looked a little more. cookies won't work because this needs to be distributed to people, and asking them to check their cookies for the file is a bit much for not-so-advanced computer users.
Also, cookies are cleared, so you'd need to manually find the cookie and change it to avoid it getting deleted the next time you clear your cookies from your browser.
Thanks again for the links, though... maybe one will prove useful in finding a local way to do it.
Well, cookies are just txt files, yes? and you can add actionscript to tell it where to create/store the info, be it in the cookies folder, or in a savefile folder created on the users desktop...
Twey is right though, I think the first is your best bet, using teh shared objects is all internal, so you wouldn't even need the extra txt file :D
djr33
06-20-2006, 12:46 AM
Ok, I will definitely look into this.
I'd actually prefer external files; I think. You think cookies would work? I could save them with whatever text I wanted, whereever I wanted?
The first one sounds cool... checking it out now.
But... when you close the flash file, where does it keep the information? Does it go away when closed (like you can reload all you want til it's closed, but then it's gone after reopening) or does it save it into the flash file itself, or does it save it in a cookie/external file?
It also appears to use cookies... but maybe as a way to save text files?
BLiZZaRD
06-20-2006, 01:02 AM
Yes, I think you could use cookies.
Take the last one for example.. sure it is a high score file.. but you can use the INSERT to insert what ever lines of info you wanted.
You also tell the file to be created and where. So instead of using:
_global.domain+file.name
you would just put something like:
C:/savefile/+filename
I am guessing that the others will work similarly as well.
Also, on the flashkit forum (http://www.flashkit.com/board/) They have a "newbies" section.
Disreguard that it is a newbies section, the moderator there AsktheGeezer is very talented with almost every aspect of Flash and I am sure he can get something working in a matter of minutes.
Good Luck with that!
djr33
06-20-2006, 05:59 PM
Well, thanks a lot.
i've looked at it some more, but I'm pretty busy; I'll test it out later today. I'm sure something will work.
:D
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.