Author |
Message |
Raknarg
|
Posted: Thu Nov 24, 2011 5:03 pm Post subject: Finding a specific spot in a text file |
|
|
Hey guys. I know this is in the Turing Walkthrough, but I didn't quite understand it, so I'm hoping someone could clarify this for me...
Basically I'm starting and RPG (just to see if I can do it, and for fun ), and one of my problems is that I'm using a text file for the map, but I want to be able to change the states of a certain place on the map (for instance you would use the code chst+ for a chest that's closed and chst- for a chest that's opened). However, I'm not quite sure how to find a specific spot and then go about changing it. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
ProgrammingFun
|
Posted: Thu Nov 24, 2011 5:14 pm Post subject: RE:Finding a specific spot in a text file |
|
|
Possibly grid the map and assign each chest coordinates on that grid? That's what I did for a game I previously programmed last year. |
|
|
|
|
|
Raknarg
|
Posted: Thu Nov 24, 2011 5:19 pm Post subject: RE:Finding a specific spot in a text file |
|
|
Yeah thats what I'm doing, but I mean for the text file itself, because I need to make a way for the computer to remember if the chest was opened. |
|
|
|
|
|
Tony
|
Posted: Thu Nov 24, 2011 5:54 pm Post subject: RE:Finding a specific spot in a text file |
|
|
Just overwrite the entire file. Presumably save doesn't happen often.
Alternatively you could use seek and write in binary mode. I'm not sure if that will introduce complications when you start mixing it with text-mode creation/edits of the files. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Aange10
|
Posted: Thu Nov 24, 2011 6:49 pm Post subject: RE:Finding a specific spot in a text file |
|
|
I could be totally wrong, but I've seen configure files that would have something looking like
Quote:
-FireSpread = 15
-SpawnPoint = 100, 100, 100
-Speed = 300
-fly = true
-chest = closed
if it is optional for you to do something like that, you could simply 'load' each line and then go through where you store it and have the computer look past the = sign, to get the information it needs.
If that is an applicable way for you to do this. |
|
|
|
|
|
Raknarg
|
Posted: Fri Nov 25, 2011 6:39 pm Post subject: RE:Finding a specific spot in a text file |
|
|
Yeah, I think im giving up on putting the chest in the text, I'll probably just use a number array to keep track instead... thanks for the suggestions guys |
|
|
|
|
|
Aange10
|
Posted: Fri Nov 25, 2011 7:08 pm Post subject: RE:Finding a specific spot in a text file |
|
|
^-^ Yup, no problem.
I'm so unsure of myself because I'm not really quite sure what you are doing in the text file.
Anyways, the way I said is simple string manipulation/sorting.
Edit:
But to answer your thread question:
I don't know how to find a specific spot in a text file, but there are ways to organize your data and store it in the text file. The only way I know, however, of finding something specifically in a text file is storing the text file as an array, and then sorting through the array. |
|
|
|
|
|
Raknarg
|
Posted: Fri Nov 25, 2011 7:16 pm Post subject: RE:Finding a specific spot in a text file |
|
|
To answer your first question, Imagine a file like this:
grss grss
grss grss
In my program, that would just be read as four grass tiles and it would draw them to the screen. Now what if I have this:
grss chst+
grss grss
chst+ Could be considered as a closed chest. Now lets saw your character is on the top right, and activates the chest. An event occurs, and after that, the chest is forever open. However, I need to find a way to change that old text to this:
grss chst-
grss grss
Noe that would be easy for four tiles. What if I have a hundred tiles or more? I need to make an algorithm to find the spot the chest was in in the text file and then change the data.
And yes, I can sort through the array, but I need the information to be saved, and I dont want to have to rewrite an entire map every time if I dont hafve to. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Fri Nov 25, 2011 7:55 pm Post subject: Re: RE:Finding a specific spot in a text file |
|
|
Raknarg @ Fri Nov 25, 2011 7:16 pm wrote: I dont want to have to rewrite an entire map every time if I dont hafve to.
as was asked above, why not?
Ideally, yes -- that feels like work... suboptimal... wrong. But from a Software Engineering stand point -- how much of a performance hit is that? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Aange10
|
|
|
|
|
Aange10
|
Posted: Fri Nov 25, 2011 9:11 pm Post subject: RE:Finding a specific spot in a text file |
|
|
Also, could you not assign numbers to each chest? So I wouldnt be changing chst+ I'd be changing chst+1234 to chst-1234 |
|
|
|
|
|
Raknarg
|
Posted: Fri Nov 25, 2011 9:31 pm Post subject: RE:Finding a specific spot in a text file |
|
|
Theres no need for numbers. The chest is already placed at a certain spot, so it doesnt matter whether or not it's assigned a number. Thats the point of giving each spot its own code; I can pull apart the string and use the information to interact with the game |
|
|
|
|
|
Aange10
|
Posted: Fri Nov 25, 2011 9:37 pm Post subject: RE:Finding a specific spot in a text file |
|
|
Raknarg just wrote:
The chest is already placed at a certain spot
Raknarg earlier wrote:
I'm not quite sure how to find a specific spot
And your point is?
Raknarg wrote:
it doesnt matter whether or not it's assigned a number
wheres the problem? |
|
|
|
|
|
ProgrammingFun
|
Posted: Fri Nov 25, 2011 9:56 pm Post subject: Re: RE:Finding a specific spot in a text file |
|
|
Aange10 @ Fri Nov 25, 2011 9:37 pm wrote: wheres the problem?
|
|
|
|
|
|
Raknarg
|
Posted: Fri Nov 25, 2011 10:10 pm Post subject: RE:Finding a specific spot in a text file |
|
|
BECAUSE I need to be able to read one specific string. It doesnt matter what I put on that string, If I cant find any one particular spot and change it, then it's pointless. I'd just have to use a separate text file or something specifically for the chest. |
|
|
|
|
|
|