Raised This Month: $ Target: $400
 0% 

Solved ReadFileLine isn't giving me the right result.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ethorbit
Member
Join Date: Sep 2016
Location: Oregon
Old 12-28-2017 , 04:00   ReadFileLine isn't giving me the right result.
Reply With Quote #1

So I'm trying to read from "data/phoneburnia/zombie1.txt"
Inside this text file are 2 lines that consist of 3 seperate numbers:
Code:
208.837997 87.651397 -189.968750 
0.000000 359.000000 0.000000
Those are the position and the angles.
I want it to print it to the chat, but no matter what, it prints
Code:
0.000000 0.000000 0.000000
I do not understand why this is happening.

PHP Code:
public Action SpawnDesiredNPC(Handle timerentity)
{
    
decl String:targetname[128];
    
GetEntPropString(entityProp_Data"m_iName"targetnamesizeof(targetname));
        if(
StrEqual(targetname,"zombie1")){
        new 
Float:npcposition[3];
        new 
Float:npcangle[3];
        
GetEntPropVector(entityProp_Send"m_vecOrigin"npcposition);
        
GetEntPropVector(entityProp_Send"m_angRotation"npcangle);
        
decl String:path[PLATFORM_MAX_PATH];
        
CreateDirectory("addons/sourcemod/data/phoneburnia"3);
        
BuildPath(Path_SMpathsizeof(path), "data/phoneburnia/zombie1.txt");
        new 
Handle:fileHandle=OpenFile(path,"w"); 
        
WriteFileLine(fileHandle"%f %f %f"npcposition[0], npcposition[1], npcposition[2]);
        
WriteFileLine(fileHandle"%f %f %f"npcangle[0], npcangle[1], npcangle[2]);
        
CloseHandle(fileHandle);
        
CreateTimer(5.0ZombieSpawner1entity);
    }
}

public 
Action ZombieSpawner1(Handle timerentity)
{
        
decl String:path[PLATFORM_MAX_PATH];
        
BuildPath(Path_SMpathsizeof(path), "data/phoneburnia/zombie1.txt");
        new 
Handle:fileHandle2=OpenFile(path,"r"); 
        new 
String:lineBuffer[128];
        
ReadFileLine(fileHandle2lineBuffersizeof(lineBuffer));
        
PrintToServer("%f %f %f"lineBuffer[0], lineBuffer[1], lineBuffer[2]); 
        
CloseHandle(fileHandle2);

I'm sorry for frequently asking for help, but I'm limited to how long I am able to work on my server and these problems are overwhelming me. I would really appreciate if someone could help me with my (hopefully last) issue.

Last edited by Ethorbit; 12-28-2017 at 06:20.
Ethorbit is offline
blaacky
Senior Member
Join Date: Oct 2012
Old 12-28-2017 , 05:07   Re: ReadFileLine isn't giving me the right result.
Reply With Quote #2

You need to use ExplodeString on the lineBuffer variable to turn the string into 3 separate strings using a space as the delimiter. After that you have to do StringToFloat on the 3 strings you get after and those are the float values you can use and print out
__________________
blaacky is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 12-28-2017 , 05:24   Re: ReadFileLine isn't giving me the right result.
Reply With Quote #3

Once the numbers are saved to the file they're no longer floats, they are just a bunch of characters. So when you read from the file all you're getting is the string representation of those numbers and not the numbers themselves. You're storing these characters in a string called "lineBuffer", which is just an array of characters. So lineBuffer[0] would give you the first character and not the first number. Same with lineBuffer[1] and lineBuffer[2].

What you need to do to is first of all separate these 3 numbers, which can be easily done with ExplodeString, and then converting them to a float so they can be used by using StringToFloat with each number seperated.

Another alternative is to not convert the floats to string to begin with. If the file doesn't need to be readable, you can just store the raw bytes.
That implementation would look something like this:
PHP Code:
public Action SpawnDesiredNPC(Handle timerint entity

    
char targetname[128]; 
    
GetEntPropString(entityProp_Data"m_iName"targetnamesizeof(targetname)); 
    
    if(
StrEqual(targetname,"zombie1"))
    { 
        
float npcPosition[3];
        
GetEntPropVector(entityProp_Send"m_vecOrigin"npcPosition);
        
float npcAngle[3];
        
GetEntPropVector(entityProp_Send"m_angRotation"npcAngle); 
        
        
char path[PLATFORM_MAX_PATH]; 
        
CreateDirectory("addons/sourcemod/data/phoneburnia"3); 
        
BuildPath(Path_SMpathsizeof(path), "data/phoneburnia/zombie1.txt"); 
        
        
File fileHandle OpenFile(path"w");  
        
        
fileHandle.Write(npcPositionsizeof(npcPosition), 4); // 3 items in the array, each is 4 bytes 
        
fileHandle.Write(npcAnglesizeof(npcAngle), 4);
        
        
fileHandle.Close();
        
        
CreateTimer(5.0ZombieSpawner1entity);
    } 


public 
Action ZombieSpawner1(Handle timerint entity

    
char path[PLATFORM_MAX_PATH]; 
    
BuildPath(Path_SMpathsizeof(path), "data/phoneburnia/zombie1.txt"); 
    
    
float npcPosition[3];
    
float npcAngle[3];
    
    
File fileHandle OpenFile(path"r");
    
    
fileHandle.Read(npcPositionsizeof(npcPosition), 4// read 3 items, 4 bytes each, and store in npcPosition
    
PrintToServer("Pos: %f %f %f"npcPosition[0], npcPosition[1], npcPosition[2]);
    
fileHandle.Read(npcAnglesizeof(npcAngle), 4// read 3 items, 4 bytes each, and store in npcAngle
    
PrintToServer("Ang: %f %f %f"npcAngle[0], npcAngle[1], npcAngle[2]);
    
    
fileHandle.Close();


Last edited by hmmmmm; 12-28-2017 at 05:27.
hmmmmm is offline
Ethorbit
Member
Join Date: Sep 2016
Location: Oregon
Old 12-28-2017 , 06:19   Re: ReadFileLine isn't giving me the right result.
Reply With Quote #4

Quote:
Originally Posted by blaacky View Post
You need to use ExplodeString on the lineBuffer variable to turn the string into 3 separate strings using a space as the delimiter. After that you have to do StringToFloat on the 3 strings you get after and those are the float values you can use and print out
Thanks for the helpful information.

Quote:
Originally Posted by hmmmmm View Post
Once the numbers are saved to the file they're no longer floats, they are just a bunch of characters. So when you read from the file all you're getting is the string representation of those numbers and not the numbers themselves. You're storing these characters in a string called "lineBuffer", which is just an array of characters. So lineBuffer[0] would give you the first character and not the first number. Same with lineBuffer[1] and lineBuffer[2].
[/PHP]
And thanks for the info & providing the code too.

That made my whole night easier, it works exactly as intended I honestly cannot thank you guys enough.
Ethorbit is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:35.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode