|
0 registered members (),
938
guests, and 4
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
packed exe : writing to external txt file?
#226254
09/08/08 17:38
09/08/08 17:38
|
Joined: Feb 2007
Posts: 146 UK
robertbruce
OP
Member
|
OP
Member
Joined: Feb 2007
Posts: 146
UK
|
Hello,
I have created a small project which I would like to write characters to an external txt file.
I can achieve this using a published .cd folder with the following code:
var fHandle; action player_position { while(key_p == 0) { wait(1); } fHandle = file_open_write("data.txt"); file_var_write(fHandle,int(my.x)); file_str_write(fHandle,"\n"); file_var_write(fhandle,int(my.y)); file_str_write(fHandle,"\n"); file_var_write(fHandle,int(my.z)); file_close(fHandle); }
The project gives the coordinates of the player and writes them to the txt file.
However, when the project is packed as a single .exe this does not work. I have tried using the following code
var fHandle; action player_position{ while(key_pg == 0) { wait(1); } fHandle = file_open_write("data.txt"); file_var_write(fHandle,int(my.x)); file_str_write(fHandle,"\n"); file_var_write(fhandle,int(my.y)); file_str_write(fHandle,"\n"); file_var_write(fHandle,int(my.z)); file_close(fHandle); }
and received the following error:
invalid pointer in player_position: file_var_write(fHandle,int(my.x))
What I would like to have is a single .exe gamestudio project with the ability to write to a txt file which is in the same folder as the exe.
eg;
myproject.exe data.txt
I've searched through the posts regarding read commands and txt files cannot find anything as yet.
Would appreciate any help.
Thanks,
Rob
|
|
|
Re: packed exe : writing to external txt file?
[Re: robertbruce]
#226591
09/10/08 13:48
09/10/08 13:48
|
Joined: Sep 2002
Posts: 1,065 Germany, Jena, Thüringen
3D_Train_Driver
Serious User
|
Serious User
Joined: Sep 2002
Posts: 1,065
Germany, Jena, Thüringen
|
Hi! The second one won`t work. But I guess it`s a pointer thing. Before writing my.x or the like you should assign the my pointer to an entity for example the player. I suggest:
string name_writefile[256];
string file_writefile = "data.txt";
entity* the_player;
var player_position;
var_nsave fhandle;
//any other function that created the player
function create_player(){
the_player = ent_create("playermodel.mdl",player_position,null);
}
//now follows your function
action player_position{
while(key_pg == 0){wait(1);}
my = the_player; //!!!!!!!
str_cpy(name_writefile,work_dir); //copy the work_directory in to the filename first
str_cat(name_writefile,file_writefile); //then add the real file name
fhandle = file_open_write(name_writefile);
if(fhandle){ //if the file has been opened for writing
file_var_write(fhandle,int(my.x));file_asc_write(fhandle,10);
file_var_write(fhandle,int(my.y));file_asc_write(fhandle,10);
file_var_write(fhandle,int(my.z));file_asc_write(fhandle,10);
file_close(fhandle);
}
else{ //in case the file couldn`t be created or found
return;
}
}
The thing with the work directory is to be shure that you`re always in the right directory but normally you don`t need to. But if you used Nacasi to pack your game into it`s going to be difficult. Then you have to write the whole file name including the path. For example ("C:\\folder1\\folder2\\data.txt") file_asc_write(fhandle,10); is the same like file_str_write(fhandle,"\n"); Hope I could help.
Last edited by 3D_Train_Driver; 09/10/08 13:56.
|
|
|
|