(solved)Write to existing file

Started by FanOfHumor, Wed 27/07/2022 15:10:07

Previous topic - Next topic

FanOfHumor

Ok so I never really figured this out and when I tried it recently I couldn't figure out how to say which file to open.I would like to be able to open from game-windows directory if possible but I see your not able to write to the file and only able to read it.
Code: ags

{
	if(mouse.IsButtonDown(eMouseLeft))
	{
		if(mouse.Mode==eModemcar)
		{
			car=Overlay.CreateRoomGraphical(mouse.x, mouse.y, 2);
		}	
		if(mouse.Mode==eModemblupi)
		{
			other[othernum]=Overlay.CreateRoomGraphical(mouse.x, mouse.y, 4);
			File* newfile=File.Open("scene031",eFileWrite);
			
			newfile.WriteString("TESTING TESTING TESTING");
			newfile.Close();
			
		}	
	}	
}

Where should I place the txt file?

Crimson Wizard

#1
Explicitly writing to the game directory is forbidden, as the game may be run from a readonly location, or location where user does not have write permissions.

How and where you can open files is explained in the manual:
https://github.com/adventuregamestudio/ags-manual/wiki/File#fileopen

I very much recommend to use location tags to tell engine clearly where you want to open the file ($INSTALLDIR$, $SAVEGAMEDIR$ and $APPDATADIR$).

If you need to have some file distributed along with your game's exe, to which you also want to write as the game runs, then you could have default file provided with the game, but write into a separate file in $SAVEGAMEDIR$ (for example). When reading, first check if a new file exists in $SAVEGAMEDIR$, and if not then read the default from $INSTALLDIR$.

FanOfHumor

Where is the savegamedir and where is the Installdir and appdata dir?

Crimson Wizard

#3
Quote from: Pajama Sam on Wed 27/07/2022 15:57:14
Where is the savegamedir and where is the Installdir and appdata dir?

The $INSTALLDIR$ is always the location of game's exe.

As for $SAVEGAMEDIR$ and $APPDATADIR$, their actual location depends on operating system, and may also be overridden by player's config. Players may change these locations to any custom path if the like, after which they are themselves responsible for the game to work correctly.

For example, on Windows by default $SAVEGAMEDIR$ is located in "%USERPROFILE%/Saved Games/<GameName>", and the $APPDATADIR$ is located in "C:/ProgramData/Adventure Game Studio/<GameName>".

FanOfHumor

Can you write to the file in installdir?

Crimson Wizard

#5
Quote from: Pajama Sam on Wed 27/07/2022 16:21:39
Can you write to the file in installdir?

Have you read the article I linked in my reply above?

No, you cannot, you may write in $SAVEGAMEDIR$ or $APPDATADIR$. Also, if you want to overwrite a file that comes with the game, this is what I proposed in my first reply:
Quote
If you need to have some file distributed along with your game's exe, to which you also want to write as the game runs, then you could have default file provided with the game, but write into a separate file in $SAVEGAMEDIR$ (for example). When reading, first check if a new file exists in $SAVEGAMEDIR$, and if not then read the default from $INSTALLDIR$.


FanOfHumor


SMF spam blocked by CleanTalk