Jump to content



Recommended Posts

Ok smarties, I need a bit of help. I need to get the position of my player written to a text file. Similar to what the SAMP /save command does.


So I have this:


pPlayer->GetPlayerCoord(0.0f, 0.0f, 0.0f, &fX, &fY, &fZ);



Immediately after I do this, I'd like it to add the fX, fY, and fZ (coords) to a text file, along with the fPLZangle, possibly on a second line.


Can anyone point me to any starters on this? I don't need it to read back the data, though I am working on something that will eventually need this. But for now, writing the data to a text file would be groovy.

Link to comment
Share on other sites







FILE * pFile;pFile = fopen("myfile.txt","a");if (pFile) { fprintf(pFile, "---\n%.2f %.2f %.2f\n%.2f\n---\n", fX, fY, fZ, fPLZangle); fclose(pFile);}


Link to comment
Share on other sites

You realize of course, that you are now my hero of the day.


Hang on, I'll go find a crappy Metallica song for you, in tribute.


Thanks very much Mike, much appreciated. smile.gif


EDIT: Quick question, as it's not exactly covered in the links: the text file needs to be in the root of where the program is, right? Example: my asi file is in GTALCb2v7, so my text file goes in the folder, correct?


Now, regarding San An, my hook doesn't need to be in the SA directory to run. Does this mean my text file should be in the folder that my SA Studios exe is?

Edited by ceedj
Link to comment
Share on other sites

Using that example, it would write to the file to the current working directory (in the SA root directory (i think)).

You don't need to create the file or anything though;


"a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
Link to comment
Share on other sites

If you want a new line in a Windows text file, you need to use \r\n instead of just \n.

Prior to filing a bug against any of my code, please consider this response to common concerns.

Link to comment
Share on other sites

  • 1 month later...

Ok guys, I'm having a few more problems. I've spent a little over a week trying to come up with a solution to some of this, but I just can't get it to do what I want to.


What I'd like to do is have the program read a file, then apply what it reads to an animation. Here's what I have so far:



ScriptTest:if (FileCount == 0)rewind (pFile);goto ScriptTest2;ScriptTest2:SCRIPT_WAIT(0);if (KEYDOWN(0x31)) // 1{fscanf (pFile, "%d %d %f", &AnimFile, &AnimID, &AnimBlend); pPlayer->PlayerAnimation(AnimFile, AnimID, AnimBlend); FileCount = 1;} else { goto ScriptTest3;}ScriptTest3:if (KEYDOWN(0x51)) // Q{fclose (pFile);FileCount = 0; goto FreezeClock;} else { goto ScriptTest;}



My text file (so far) looks like this:



0 78 30.00 49 30.0



What it's doing is playing the last animation (0, 49, 30.0) but not going back to the first. What I'd like to have is a file that can have 1-20 entries, so that when I press a key (1 in this case), it goes to the next line and does THAT animation. I could do this with a set number of IF checks, but I'd like it to be flexible, and just use an EOF check somewhere in there. Ideally, I set this up for a few other keys, so I can go 1 (makes ped1 do something), 2 (makes ped2 do something) and alternate back and forth, but staying in this general loop. (it's kind of a poor-man's timeline, but the user would be able to script all of his actions, then just apply them using 1 or 2 keys instead of trying to remember every single key combo the hook uses). I escape with the Q key. The tutorials I've found are only usedul to the point of reading a whole file at once and displaying it onscreen. Can anyone help a bit with this?


EDIT: I'd also love to have a way to read pPlayer or pGrp1Ped1 from the file and apply that as well. This way I can include other opcodes to use in the "timeline" instead of just the animation opcode.




A small bit about SA too; I'd like to do something similar, but with the actual animation names in the file, like so:






Two issues I've been running into:


1) I need to have the quotes show up, because of the way it is structured:



theScript << BareElseIf << eli << AnimKey << 105 << And << eli << AnimCh << 1 << Then;

theScript << AT_animation << AnPed << "ENDCHAT_03" << "PED" << 4.0 << 0 << 0 << 0 << 0 << -1;

theScript << setli << AnimKey << 0;

theScript << jump << AnimCheck;


2) The << and >> operators are already in use as part of the hook, so I keep getting errors when trying to use the C++ strings. Any thoughts on this as well?


Thanks very much! notify.gif


EDIT: Please help! Pretty please?

Edited by ceedj
Link to comment
Share on other sites

  • 1 month later...

I'd suggest not using goto as it makes the code difficult to follow. Using loops is a better option.


Untested, but I would think this should work, It'll apply one line at a time, and when it reaches the end of the file it'll rewind to the start.


while( 1 ){if ( FileCount == 0 || feof( pFile ) ) rewind (pFile);SCRIPT_WAIT(0);if (KEYDOWN(0x31)) // 1{ fscanf (pFile, "%d %d %f", &AnimFile, &AnimID, &AnimBlend); pPlayer->PlayerAnimation(AnimFile, AnimID, AnimBlend); FileCount = 1;} else if (KEYDOWN(0x51)) // Q{ fclose (pFile); FileCount = 0; goto FreezeClock;}}


Link to comment
Share on other sites

Hey, thanks. I know, my code is CRAP (heh), but I did eventually figure this out by doing the reading in the animation section (without a rewind, the next keypress reads the next line) and having a "reload" function which just closes the file and opens it up again. This way you can make edits real time, save the text and reload the file if you want to substitute an animation.


But my real problem still lies with San An now, and getting it to read double quotes from the file into my string, and reference properly, i.e.:


Text file has the following: "GYMSHADOWBOX" "GYMNASIUM"


So that this:



theScript << BareElseIf << eli << AnimKey << 105 << And << eli << AnimCh << 1 t<< Then;theScript << AT_animation << AnPed << AnimID << AnimFile << 4.0 << 0 << 0 << 0 << 0 << -1;theScript << setli << AnimKey << 0;theScript << jump << AnimCheck;(with the help of this:void AnimStuff(){	pFile2 = fopen("SAStudios.txt","r");fscanf (pFile2, "%s %s", AnimID, AnimFile);fclose (pFile2);})



becomes this to the game:



theScript << BareElseIf << eli << AnimKey << 105 << And << eli << AnimCh << 1 << Then;theScript << AT_animation << "GYMSHADOWBOX" << "GYMNAISUM" << AnimFile << 4.0 << 0 << 0 << 0 << 0 << -1;theScript << setli << AnimKey << 0;theScript << jump << AnimCheck;



I would also be fine if the double quotes are already part of the code, leaving my text file as GYMSHADOWBOX GYMNASIUM


My file I/O seems to work with coords, so unless I'm missing something drastic, I don't think that's it.


Any other takers? wow.gif


EDIT: OMG! Figured it out! It was WHERE I was placing the code. I needed to put it inside the mission script section, near the beginning, Thanks to everyone who responded. smile.gif

Edited by ceedj
Link to comment
Share on other sites

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.