PLEP Posted July 3, 2010 Share Posted July 3, 2010 Basicly, I'm trying to make a small script that would save my positions in a test.txt, but I can't get getElementPosition to print out as text in the file. Any ideas? function consoleCommandTest ( thePlayer, command ) local newFile = fileCreate("test.txt") if (newFile) then a = getPlayerFromName( "PLEP" ) if a then b = getElementPosition (a) if b then fileWrite(newFile, b) fileClose(newFile) end end end end addCommandHandler ( "mypos", consoleCommandTest ) Link to comment
Discord Moderators Zango Posted July 3, 2010 Discord Moderators Share Posted July 3, 2010 fileWrite takes string as arguments after theFile. You are trying to send an integer. To fix this, use tostring which will convert the integer into a string. Secondly, getElementPosition returns three floats. You cannot store this in one variable, so you need three or else you will only have your X position printed. function consoleCommandTest ( thePlayer, command ) local newFile = fileCreate("test.txt") if (newFile) then a = getPlayerFromName( "PLEP" ) if a then x, y, z = getElementPosition (a) if x and y and z then fileWrite(newFile, tostring(x), " ", tostring(y), " ", tostring(z)) fileClose(newFile) end end end end addCommandHandler ( "mypos", consoleCommandTest ) Link to comment
PLEP Posted July 3, 2010 Author Share Posted July 3, 2010 Thanks! Works perfectly. Now just left to find out how to add more coordinates in the file instead of overwriting existed over again. Link to comment
dzek (varez) Posted July 3, 2010 Share Posted July 3, 2010 isnt it better to use xml? Link to comment
PLEP Posted July 4, 2010 Author Share Posted July 4, 2010 Why would it be better? I need just it to be saved in notepad so I can quickly copy/paste coordinates into script. Link to comment
Lordy Posted July 4, 2010 Share Posted July 4, 2010 in that case use outputConsole and copy from there (f8, select text, ctrl-c), no hassle with any files Link to comment
dzek (varez) Posted July 4, 2010 Share Posted July 4, 2010 saving something to xml is faster than creating normal text-save reading from saved xml is even faster, cause you are just getting needed arguments, than splitting strings etc Link to comment
PLEP Posted July 4, 2010 Author Share Posted July 4, 2010 Well, yeah. But I'm trying to find out how all this stuff works with files to get atleast some scripting experience. There are no examples about xml in wiki also. However, now I made another function that would read my file till the end of it and write in another position (Without overwriting the first one). But when I type /mypos in MTA, it causes some excessive error spam in the server window, then I get network troubles. Looks like something's wrong with fileIsEOF and fileRead. I tried everything that would come up in my head, but nothing worked. function consoleAddPos ( thePlayer, command ) local testFile = fileOpen("Positions.txt") if (testFile) then local buffer while not fileIsEOF(testFile) do buffer = fileRead(testFile, 60) local c = getPlayerFromName("PLEP") if c then local x, y, z = getElementPosition (c) if x and y and z then fileWrite(testFile, " ", tostring(x), " ", tostring(y), " ", tostring(z), " ") -- Those " " in the beginning and ending are supposed to be spacebars between positions. fileClose(testFile) end end end end end addCommandHandler ( "mypos", consoleAddPos ) Link to comment
Lordy Posted July 4, 2010 Share Posted July 4, 2010 function myPosition(player,command) local file -- Just make the file variable local to the function scope. We don't have to assign anything to it though if fileExists("output.txt") then -- Check if the file exists file = fileOpen("output.txt") -- If it does, open it with read & write access else -- If it doesn't file = fileCreate("output.txt") -- create the file end fileSetPos(file,fileGetSize(file)) -- set the read/write position to the end of file. fileGetSize effectively returns the number of bytes from the start of file to the end local x,y,z = getElementPosition(player) -- get the position of the player fileWrite(file,("%s, %s, %s \n"):format(x,y,z)) -- now we write it into the file. Formatting strings is another way to add different strings up. Search for StringLibraryTutorial on lua wiki for more information. \n is a newline. It actually is string.format("%s, %s, %s \n",x,y,z), but it can be shortened to the form which I wrote before fileClose(file) -- Close the file. fileClose also automatically saves the file. end addCommandHandler("mypos",myPosition) Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now