Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / How to import data from a text file?

Author
Message
Alex_2015
7
Years of Service
User Offline
Joined: 13th Sep 2017
Location: Somewhere in Putin's Russia
Posted: 7th Apr 2018 22:31
Let's say, I have a string entry or an integer entry in a text file. These entries are in human readable format, so when I try to read them from AppGameKit app with ReadLine and/or ReadString or ReadInteger commands, I suffer a fail.
Is there any way to make that text file data readable for AppGameKit in order to use ReadLine and/or ReadString, ReadInteger commands to fill an array?
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 7th Apr 2018 22:49
ReadLine() should work, are you getting an error and do you have an example text file that isn't working?
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
Alex_2015
7
Years of Service
User Offline
Joined: 13th Sep 2017
Location: Somewhere in Putin's Russia
Posted: 8th Apr 2018 01:46
ReadLine() works with lines previously created by WriteLine().
But what I would like to accomplish is to be able to edit a text file outside of AppGameKit app and then read the data back into AppGameKit app.
If we, for example, take a *.txt file created with notepad with a single word in it (let it be "Hello"), then read it with ReadLine() we will get some nonsense instead of that word.
I guess, that is because AppGameKit writes to files in binnary or in hexadecimal or in some other inhumane format. Hopefully there is any way / tool to convert to/from that format.
Amon
10
Years of Service
User Offline
Joined: 30th May 2014
Location: Shropshire, United Kingdom
Posted: 8th Apr 2018 03:28 Edited at: 8th Apr 2018 03:34
This is the code I use to read numbers from a text file into an array. The array holds the info for my tilemap level.

I've attached the level.txt file to this post. If the number is a 1 then it plots a tile. If the number is 2 it is the starting position of the player character.

I hope this can help you realize your code.

Imaginations' greatest gift is the knowledge you supply it.

Attachments

Login to view attachments
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 8th Apr 2018 09:21 Edited at: 8th Apr 2018 09:22
> ReadLine() works with lines previously created by WriteLine().
> But what I would like to accomplish is to be able to edit a text file outside of AppGameKit app and then read the data back into AppGameKit app.
> If we, for example, take a *.txt file created with notepad with a single word in it (let it be "Hello"), then read it with ReadLine() we will get some nonsense instead of that word.
> I guess, that is because AppGameKit writes to files in binnary or in hexadecimal or in some other inhumane format. Hopefully there is any way / tool to convert to/from that format.

---

WriteLine() will write out text in human readable format. Likewise, ReadLine() will read a line of (human readable) text that is terminated by a new line. So if you have a file with just a single word for instance, you must have a new line after it. Similarily, if having multiple lines of text, the last line need be empty.

In other languages you often just import an entire file into a byte-buffer, and then convert that to a string. Not so in AGK. Those newlines are muy importante!

Here's a simple example of reading a string from file:



(I don't believe in using $ or # to signify variable type, so I declare them explicitly and without any silly special characters. I do believe in Error handling though... )
Alex_2015
7
Years of Service
User Offline
Joined: 13th Sep 2017
Location: Somewhere in Putin's Russia
Posted: 8th Apr 2018 21:00
Thank You, guys!
Now I see that the problem was indeed in those new lines.

Login to post a reply

Server time is: 2024-11-21 16:50:18
Your offset time is: 2024-11-21 16:50:18