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.

AppGameKit Classic Chat / How do I load a variable from a .txt file?

Author
Message
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 15th Sep 2012 06:24
I want to use a .txt file to hold the information for a variable that gets called in the program. This way I change the information without having to hard code it and compile it.

I looked into the loadSubImage and atlas but that is really setup for images and not information.

So how can I load a variable from a .txt file?

Thanks.
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 15th Sep 2012 06:36
Your going to need to use a fair amount of File commands found here:

http://www.appgamekit.com/documentation/Reference/File.htm

Firstly you'll have to decide what variables your going to be reading & writing.

Here is an example of how I write my high scores out in a Tier 2 style



Notice I do the following
1: Check if the file exists
2: In this case if so I delete it because Im about to write it out again
3: Then I open the new file to write
4: I run a short loop to write the integer values to the file
5: I close the file out

Similar process to read a file



So here I:
1: Check if the file exists
2: Open the file to read
3: read in the values
4: Close the file


Only difference is Tier1/Tier2 commands .. the Tier2 usually has agk:: before it ; so take that out would be similar syntax to Tier1

Hope that helps
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 15th Sep 2012 07:05
Thanks, I setup a small experiment but ran into 2 problems.

First. I get an error "Both operands are required for non-uniary operations at line 11" line 11 is the if (getFileExists ("test.txt")==1)


Also I am not 100% sure how to write the available in the txt file.

Not sure if it should be score, 10 or score = 10or score:10

Thanks again.
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 15th Sep 2012 11:10
Kobaltic he is using tier 2 and you are using tier 1

Her is my code for loading and saving my map file in my platformer.

State is simply wath the function should do.
file io is the file name to save or load.
Both are strings "text here".

Map_File( "load" , "mymap.lvl" )



Android 2.3 Gingerbread , ZTE Skate , 480x800 , 800 mhz cpu , Samsung Galaxy Y , 240x320 , 832 mhz cpu
Android 4.0 Sandwich , Dmtech 3g 9738B , 1024x768 , 9.7 inches , cortex A8 1.2 cpu , 1 gb ram.
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 15th Sep 2012 11:33
Here's another example for saving and loading in Tier 1:




Demo 2 is out! Click the image for more.
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 15th Sep 2012 18:33
Thanks everyone. I got it working. The only issue seems to be that the information in test.txt is not human readable. Is there a way to do this so I change the values in test.txt and have them change in the program?
Auger
12
Years of Service
User Offline
Joined: 21st Aug 2011
Location: Out There
Posted: 15th Sep 2012 23:02 Edited at: 15th Sep 2012 23:39
You'll need to convert your variables to a string and then save the strings. Then just read back the string and convert it to a number


Use WriteString(File, str(YourValue))
instead of WriteInteger()

Str() converts a number to a string.


use YourValue = Val( ReadString(File))
to read your string and then convert it back to an integer

Val() converts a string to an Integer

Now you'll be able to open the file in say notepad and change the text numbers as you like.
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 16th Sep 2012 06:27
Thanks I can now read the numbers in the .txt file. Is there a way to make it so I can read the variable and not just the number that relates to it. My concern is that six months from now I won't have a clue as to which number is which variable. I would like to see something like score = 10 or something close to that.
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 16th Sep 2012 06:51
You could try creating a 'map' construct using a UDT.

In C++:


In AGK(a bit more involved, and this is pseudo-code):

Note: I can only guarantee that the C++ code will compile, the T1 code might not compile. Also note that the system in T1 will be slow compared to regular variable access, so reduce all calls to the provided function as much as possible.

Auger
12
Years of Service
User Offline
Joined: 21st Aug 2011
Location: Out There
Posted: 16th Sep 2012 07:21
@ Kobaltic

An easy way would be just to save out your variable names also.

WriteString(File,"Score")
WriteString(File,Str(score))

Then in your text file it will look like this

Score
10
Rich Dersheimer
AGK Developer
14
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 16th Sep 2012 10:03 Edited at: 16th Sep 2012 10:03
Also, AppGameKit allows you to save a file without checking if it exists and deleting it first, unlike DBPro.

Quote: "OpenToWrite( ID, szFile, append )

If the file does not exist it will be created, if the file does exist it will be overwritten if append is 0 or appended to if append is 1."


Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 18th Sep 2012 19:05
Things are moving along. The problem I have now is that I can't get AppGameKit to identify what is what in the file. When I use more than one word and/or variable it simply starts with the first one and then moves on. I need it to actually look through the list find the correct word and retrieve the variable associated with it. If you look at a typical .cfg file that is the results I want.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 18th Sep 2012 19:20
It sounds like you want a file something like:


And then a command that will look in the file for a line with the right tag and get the value, something like:


If you are using Tier 1, as it appear you are, there are no such commands. You can write your own functions to parse a file and return values, or store them in arrays (but you cannot reference the arrays with string values).

The file read functions all read sequentially. There aren't even any commands for restarting at the beginning of a file. Definitely no direct access commands.

If you were to use Tier 2, there are probably lots of pre-built classes for doing what you want.

Cheers,
Ancient Lady
AGK Community Tester
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 18th Sep 2012 19:47
Thanks Ancient Lady. I need to do this to stay compliant with OGL. So I guess I will move to tier2 and dust off the c++. At least I will get a debugger but I will loose the broadcast

I guess I will have to learn how to compile into apk files pretty quickly.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 18th Sep 2012 19:57 Edited at: 18th Sep 2012 20:12
For the apk bits, you will need to do the setup for Android.

None of the Tier 2 templates are up to date with the current version of AppGameKit (this is a known and much complained about issue). So, remember to download the newest Tier 2 Android template from the 'My Products' TGC page (labeled 'Android 1076 Tier 2 Template'). You will need to update the make file to include any additional .cpp files. This is the set of code I use to build my Android apps, based on my VS projects (I have my Windows AppGameKit installed in C:\TGC\AGK and used just android-ndk as the dir name, without the version):


For C++ Tier 2, I've attached a working template. You will probably need to change some directories in the properties (I use simple, but non-standard, directories for my VS Express 2010).

I'll do another post with the files for the Xcode4 Tier 2 template. It has my tweaks to allow you to catch when an app has returned from the background and to properly identify the landscape orientations.

Cheers,
Ancient Lady
AGK Community Tester

Attachments

Login to view attachments
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 18th Sep 2012 20:04
Here are the replacement files for the Tier 2 Xcode template. Simply unzip into your Xcode project. You should notice that there are some minor differences in the template.cpp/.h files from the VS template previously posted. The windows version has a method to allow you to properly close the app in different platforms (if they allow it).

I'd recommend using the Windows version as the base.

When you are set to do the Xcode, just copy the template.cpp/.h files and any others that you add. If you add any .cpp files, you will need to remember to include them in your project (I use a subdirectory in the project to keep them separate) and to set the 'File Type' to 'Objective-C++ source' in the 'File Type' drop down in the far right column, when you select the file. This avoids some annoying errors during compile/link.

Cheers,
Ancient Lady
AGK Community Tester

Attachments

Login to view attachments
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 19th Sep 2012 06:17
The new string token functions make reading data from files much easier.

You can store your data in the way AL suggested above;

Read each line as a string and use getStringToken() with "=" as the delimiter, to split the data, eg.

You can then use a select structure to deal with the results

This can be extended by further splitting the data further if required - for example;

could use

Initially I didn't think I'd use this command that much, but lately I've really taken a shine to it.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 19th Sep 2012 12:51
@Marl: That's a much better way than my way! I had to code the token bits by hand to get the same result!

I live for video games! (And beers, and football, and cars!)
See what I live for here: [url]http:\\www.TeamDefiant.co.uk[/url]
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 19th Sep 2012 16:59
Marl, good stuff. I had forgotten the newish string token functions.

Cheers,
Ancient Lady
AGK Community Tester
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 19th Sep 2012 18:54
Thanks for all the information everyone. It looks promising.

Login to post a reply

Server time is: 2024-05-04 07:32:55
Your offset time is: 2024-05-04 07:32:55