Javascript - assign variable or array from outside file - arrays

This past year, I took a coding class on creating games using javascript. It is a basic game like Asteroids. When the game ends, it shows the top 5 high scores. The only problem is that when the program is restarted, the array that holds the scores is reset.
I want to store the high scores in a text file or spreadsheet. But, I cannot find a way to get my program to pull information from an outside file and assign it to either a variable, or I would rather put it into an array.
The second part is that when the game ends, it would need to send the updated array to the outside file if it is updated. Everything I look up involves HTML and CSS and we didn't learn this.
Is there any viable way to do this in Javascript?

It seems like the question is 'how do I read and write files from javascript'? The answer largely depends on where you're running the Javascript.
It doesn't sound like you're working in a browser, which makes it likely that you're using Node JS. If this is the case, you'll want to look at the File System API. Specifically, you'll want to create a filehandle by using fsPromises.open(). Once you have this reference to a location on your hard drive, you'll use filehandle.writeFile() to write a string directly to the file, and filehandle.readFile() to read a string directly from the same file.

Related

Overwrite a json-file in the resources folder with Kotlin

The context is a android application, written in Kotlin
I have this variable that my application reads from in order to set the applications default language. In order to change the default language of the application, I have to overwrite this variable in the json file.
I am able to read from it, utilizing:
val fileContent = getResources().openRawResource(R.raw.FILE_NAME)
which will return the content as an inputstream. However, it seems like I cant overwrite the contents of the file using this method.
I have also tried to access the file utilizing the file path, however, this results in a file-not-found exception. (Been trying several different combination for this one, but they all yield the same results.
The file is located as a resource. After searching a bit, I found this thread from 11 years ago, that says that "resources cannot be overwritten". Is this still true?
Overwrite resources
Any suggestions on how to solve the problem?

Store Large Array with strings and images in Swift

Hi i'm trying to store an array filled with objects so that it doesn't disappear if the app is closed completely.
The Problem:
if I use core data and convert the array to NSData then it works. But the app freezes while it's processing the array.
I've also tried the transformable datatype but i cant't get it to work.
And I can't use NSUserdefaults either because it doesn't support images.
Does anyone have an idea how i might solve this.
i'm quite a newbie to programming so this might be an entirely wrong approach.
First save the images in individual files with unique file names in an image directory in the Documents directory. Put the unique file names in the array, not the images.
Then depending on your needs either save the individual per image information in Core Data if quick random access is required. Or save the array in a file.
For 1500 strings of ~100 characters each saving in a single file is probably fine, I would start there and only move up to Core Data if there is a performance problem Core Data would resolve it.
As Ken Beck says: "Do the simplest thing that could possibly work." I don't believe that having 750 images in the array would really work if they were of any substantial size.
Do not use NSUserDefaults.
Core Data is overkill for simply saving an array to disk.
What kind of objects are you saving?
If you already have logic to convert the array to NSData why not just save the file to either your documents directory or caches directory (caches if it can be recreated if purged, documents if it is unique and contains user state info.)
Edit:
zaph raises some good points in his question. How big is this array (number of elements and total data size.) Is it reasonable to load it all into memory?
If you are looking for a random-access way to load one element at a time, then a database might be reasonable.
The specific solution depends on the particulars of your problem, so we need more info.

How to save a list of strings in arduino?

I want to create a list of names on my Arduino. I want to be able to add a name to the list, I want to be able to remove a certain name from the list and I want to be able to check if a certain name exists in the list.
As far as I understand I need to use something called EEPROM?
BTW. I might gotten something all wrong, please tell me.
EEPROM means programmable for multiple times. Not mean storage. If you want to store list while the device is running, it is possible. Define an array like standard way. You can search in that array, add names to array by using arduino console. These are depends on your code.
But if you want to store your array all the time (even device is closed) you need a storage device such as sd card or something like this.
It is possible to store data in the EEPROM, but depending on the Arduino model there isn't much space. You would be better off using an SD card shield.

How can I load a memory stream into LibVLC?

I want to play a media file from a memory stream using LibVLC like so:
//Ideally it would go like this:
LibVLC.MediaFromStream = new MemoryStream(File.ReadAllBytes(File_Path));
Of course this is a very oversimplified version of what I want but hopefully it conveys what I am looking for.
The reason being that I want there to be a good amount of portability for what I'm doing without having to track file locations and such. I'd rather have a massive clump of data in a single file that can be read from than have to track the locations of one or many more files.
I know this has something to do with the LibVLC IMEM Access module. However, looking at what information I've been able to find on that, I feel like I've been tossed from a plane and have just a few minutes to learn how to fly before I hit the ground.
See my answer to a similar question here:
https://stackoverflow.com/a/31316867/2202445
In summary, the API:
libvlc_media_t* libvlc_media_new_callbacks (libvlc_instance_t * instance,
libvlc_media_open_cb open_cb,
libvlc_media_read_cb read_cb,
libvlc_media_seek_cb seek_cb,
libvlc_media_close_cb close_cb,
void * opaque)
allows just this. The four callbacks must be implemented, although the documentation states the seek callback is not always necessary, see the libVlc documentation. I give an example of a partial implementation in the above answer.
There is no LibVLC API for imem, at least not presently.
You can however still use imem in your LibVLC application, but it's not straightforward...
If you do vlc -H | grep imem you will see something like this (this is just some of the options, there are others too):
--imem-get <string> Get function
--imem-release <string> Release function
--imem-cookie <string> Callback cookie string
--imem-data <string> Callback data
You can pass values for these switches either when you create your libvlc instance via libvlc_new(), or when you prepare media via libvlc_media_add_option().
Getting the needed values for these switches is a bit trickier, since you need to pass the actual in-memory address (pointer) to the callback functions you declare in your own application. You end up passing something like "--imem-get 812911313", for example.
There are downsides to doing it this way, e.g. you may not be able to seek backwards/forwards in the stream.
I've done this successfully in Java, but not C# (never tried).
An alternative to consider if you want to play the media data stored in a file, is to store your media in a zip or rar since vlc has plugins to play media from directly inside such archives.

NSTreeController how to save to file

Hi I am using an NSTreeController to control an NSOutlineView. This application loads bookmarks from file to application. As in the SourceView example in ADC:
http://developer.apple.com/mac/library/samplecode/SourceView/index.html
My questions is how do I save the bookmark to file once user makes the changes. Should I maintain the array/tree internally in my application and save before quitting or is there any easier methods?
You want to reverse the action taking place in the populateOutline method of MyWindowController.m. This method is reading the plist into one dictionary, reading a value from that dictionary, and using it to build the tree. Start with that method and follow the code to see how it is building the tree. It's using the BaseNode and ChildNode classes to build up the data model as a tree (I'm not sure why they didn't just use NSTreeNode). You want to reverse that procedure, ending up with an NSDictionary. You can then use writeToFile:atomically: to save the dictionary back to disk.
This can get as complex as you'd like to make it. For instance, the current code loads the dictionary file in a separate thread, so you could save in a separate thread, too. Or, you might want to save after every edit, again in a separate thread.

Resources