ceedj Posted April 13, 2007 Share Posted April 13, 2007 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); pPlayer->GetPlayerZ(&fPLZangle); 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 https://gtaforums.com/topic/274461-c-fun/ Share on other sites More sharing options...
Mike Posted April 13, 2007 Share Posted April 13, 2007 http://www.cplusplus.com/reference/clibrar...tdio/fopen.html http://www.cplusplus.com/reference/clibrar...io/fprintf.html http://www.cplusplus.com/reference/clibrar...dio/fclose.html 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 https://gtaforums.com/topic/274461-c-fun/#findComment-4177876 Share on other sites More sharing options...
ceedj Posted April 14, 2007 Author Share Posted April 14, 2007 (edited) 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. 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 April 14, 2007 by ceedj Link to comment https://gtaforums.com/topic/274461-c-fun/#findComment-4178081 Share on other sites More sharing options...
Mike Posted April 14, 2007 Share Posted April 14, 2007 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 https://gtaforums.com/topic/274461-c-fun/#findComment-4178236 Share on other sites More sharing options...
K^2 Posted April 15, 2007 Share Posted April 15, 2007 If you want a new line in a Windows text file, you need to use \r\n instead of just \n. Link to comment https://gtaforums.com/topic/274461-c-fun/#findComment-4182422 Share on other sites More sharing options...
ceedj Posted May 16, 2007 Author Share Posted May 16, 2007 (edited) 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: "GYMSHADOWBOX" "GYMNASIUM" "FIGHTD_3" "FIGHT_D" "FIGHTD_M" "FIGHT_D" "FIGHTA_G" "PED" "FIGHTC_IDLE" "FIGHT_C" "BITCHSLAP" "MISC" 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! EDIT: Please help! Pretty please? Edited May 17, 2007 by ceedj Link to comment https://gtaforums.com/topic/274461-c-fun/#findComment-4239945 Share on other sites More sharing options...
Waterbottle Posted June 27, 2007 Share Posted June 27, 2007 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 https://gtaforums.com/topic/274461-c-fun/#findComment-4326580 Share on other sites More sharing options...
ceedj Posted June 28, 2007 Author Share Posted June 28, 2007 (edited) 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? 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. Edited June 28, 2007 by ceedj Link to comment https://gtaforums.com/topic/274461-c-fun/#findComment-4331506 Share on other sites More sharing options...
Recommended Posts