Raised This Month: $32 Target: $400
 8% 

WriteFileLine - Exception reported: invalid handle 0 (error: 4)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
JPTRON
Junior Member
Join Date: Jun 2020
Old 06-06-2020 , 05:06   WriteFileLine - Exception reported: invalid handle 0 (error: 4)
Reply With Quote #1

Hello,

I'm trying to code my own KillReward plugin and I want to register the user points inside individuals .txt files.

Code:
#pragma semicolon 1

#define DEBUG

#define PLUGIN_AUTHOR "JPTRON"
#define PLUGIN_VERSION "1.00"

#include <sourcemod>
#include <sdktools>
#include <files>
#include <string>

#pragma newdecls required

public Plugin myinfo = 
{
	name = "KillReward",
	author = PLUGIN_AUTHOR,
	description = "Receive points when killing someone.",
	version = PLUGIN_VERSION,
	url = ""
};

public void OnPluginStart()
{
	HookEvent("player_death", Event_PlayerDeath);
}


public Action Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast) {

    int victim = event.GetInt("userid");
    int attacker = event.GetInt("attacker");
    char str[40], attackerStr[40];
    
	 IntToString(attacker, attackerStr, sizeof(attackerStr));
	Format(str, sizeof(str), "/players/%s.txt", attackerStr);

    
    if(victim == attacker || attacker == 0){	
  		return;
  	}


	Handle attackerFile = OpenFile(str, "r+");
    
  	if(!attackerFile){
  		WriteFileLine(attackerFile, "10");
 	}
  
    CloseHandle(attackerFile);     
    
    attackerFile = OpenFile(str, "w+");
    
    int attackerPoints;
    
    attackerPoints = ReadFileLine(attackerFile, attackerStr, sizeof(attackerStr));
    
    attackerPoints += 10;
    
    IntToString(attackerPoints, attackerStr, sizeof(attackerStr));
    
    WriteFileLine(attackerFile, attackerStr);
    
    CloseHandle(attackerFile); 
    
}
Code:
L 06/06/2020 - 09:40:57: "JPTRON<2><STEAM_1:0:151314112><CT>" [1514 -126 -148] killed "Matt<11><BOT><TERRORIST>" [1575 -116 -84] with "taser"
L 06/06/2020 - 09:40:57: [SM] Exception reported: invalid handle 0 (error: 4)
L 06/06/2020 - 09:40:57: [SM] Blaming: points.smx
L 06/06/2020 - 09:40:57: [SM] Call stack trace:
L 06/06/2020 - 09:40:57: [SM]   [0] WriteFileLine
L 06/06/2020 - 09:40:57: [SM]   [1] Line 53, C:\Users\joaopf\AppData\Roaming\spedit\sourcepawn\scripts\points.sp::Event_PlayerDeath

Last edited by JPTRON; 06-06-2020 at 05:07.
JPTRON is offline
Kellan123
AlliedModders Donor
Join Date: Aug 2012
Old 06-06-2020 , 05:20   Re: WriteFileLine - Exception reported: invalid handle 0 (error: 4)
Reply With Quote #2

PHP Code:
if(attackerFile == INVALID_HANDLE) return; 
is missing

Last edited by Kellan123; 06-06-2020 at 05:20.
Kellan123 is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-06-2020 , 07:25   Re: WriteFileLine - Exception reported: invalid handle 0 (error: 4)
Reply With Quote #3

attacker is the CLIENT INDEX of the person who made the kill.

A client index is a slot of an entity that is reserved for online players only, and once a client leaves, his client index can be reused by the next player to join. Your code will not save how you want it to.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
JPTRON
Junior Member
Join Date: Jun 2020
Old 06-06-2020 , 08:30   Re: WriteFileLine - Exception reported: invalid handle 0 (error: 4)
Reply With Quote #4

Quote:
Originally Posted by eyal282 View Post
attacker is the CLIENT INDEX of the person who made the kill.

A client index is a slot of an entity that is reserved for online players only, and once a client leaves, his client index can be reused by the next player to join. Your code will not save how you want it to.
Yhea, you're right.

I changed my code to this:

PHP Code:
public Action Event_PlayerDeath(Event event, const char[] namebool dontBroadcast) {

    
int victim GetClientOfUserId(event.GetInt("userid"));
    
int attacker GetClientOfUserId(event.GetInt("attacker"));
    
int assist GetClientOfUserId(event.GetInt("assister"));
    
char str[40], str2[40], attackerStr[40], assistStr[40], victimName[40], attackerName[40], assistName[40];
    
    
GetClientName(victimvictimNamesizeof(victimName));
    
GetClientName(attackerattackerNamesizeof(attackerName));
    
GetClientName(assistassistNamesizeof(assistName));
    
    
IntToString(attackerattackerStrsizeof(attackerStr));
    
Format(strsizeof(str), "/players/%s.txt"attackerStr);
    
    
    if(
victim == attacker || attacker == 0){    
          return;
      }

    
Handle attackerFile OpenFile(str"r");   

    if(
attackerFile == INVALID_HANDLE) return;  
       
      if(!
attackerFile){
          
CloseHandle(attackerFile);
          
attackerFile OpenFile(str"w");
          
WriteFileLine(attackerFile"0");
     }
       
    
CloseHandle(attackerFile);      
    
attackerFile OpenFile(str"w+");   
    
    
int attackerPoints;      
    
attackerPoints ReadFileLine(attackerFileattackerStrsizeof(attackerStr));    
    
attackerPoints += 10;
    
    
IntToString(attackerPointsattackerStrsizeof(attackerStr));
       
    
WriteFileLine(attackerFileattackerStr);
       
    
CloseHandle(attackerFile); 
    
    
    
PrintToChatAll("#%s killed #%i | Points: #%i"attackerNamevictimNameattackerPoints);

    if(
assist){
        
IntToString(assistassistStrsizeof(assistStr));
        
Format(str2sizeof(str2), "/players/%s.txt"assistStr);
        
        
Handle assistFile OpenFile(str2"r");
        
        if(
assistFile == INVALID_HANDLE) return;
        
        if(!
assistFile){
        
CloseHandle(assistFile);
        
assistFile OpenFile(str2"w");
          
WriteFileLine(assistFile"0");          
         }
         
         
CloseHandle(assistFile);        
         
assistFile OpenFile(str2"w+");
         
         
int assistPoints;        
         
assistPoints ReadFileLine(assistFileassistStrsizeof(assistStr));         
         
assistPoints += 2;
         
         
IntToString(assistPointsassistStrsizeof(assistStr));
         
WriteFileLine(assistPointsassistStr);
         
CloseHandle(assistFile); 
         
         
PrintToChatAll("#%s helped killing #%s | Points: #%i"assistNamevictimNameassistPoints);
    }

No errors, but don't create any file and don't print the message :/
JPTRON is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 06-06-2020 , 09:18   Re: WriteFileLine - Exception reported: invalid handle 0 (error: 4)
Reply With Quote #5

"w+"
__________________
Silvers is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 06-06-2020 , 11:15   Re: WriteFileLine - Exception reported: invalid handle 0 (error: 4)
Reply With Quote #6

This system with files is going to be a mess, you should use Client Preferences to store client information across connections.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.

Last edited by DJ Tsunami; 06-06-2020 at 11:45.
DJ Tsunami is offline
JPTRON
Junior Member
Join Date: Jun 2020
Old 06-07-2020 , 01:53   Re: WriteFileLine - Exception reported: invalid handle 0 (error: 4)
Reply With Quote #7

Quote:
Originally Posted by DJ Tsunami View Post
This system with files is going to be a mess, you should use Client Preferences to store client information across connections.
Thank you very much dude!! I didn't think about using cookies... So, again, I changed my code to this and it's working:

PHP Code:
Handle g_hPointCookie;
int messagePoints;

public 
void OnPluginStart()
{
    
HookEvent("player_death"Event_PlayerDeath);
    
g_hPointCookie RegClientCookie("give_points""Give Points"CookieAccess_Protected);
}


public 
Action Event_PlayerDeath(Event event, const char[] namebool dontBroadcast) {

    
int victim GetClientOfUserId(event.GetInt("userid"));
    
int attacker GetClientOfUserId(event.GetInt("attacker"));
    
int assist GetClientOfUserId(event.GetInt("assister"));
    
char victimName[40], attackerName[40], assistName[40];
    
    
GetClientName(victimvictimNamesizeof(victimName));
    
GetClientName(attackerattackerNamesizeof(attackerName));
    
GetClientName(assistassistNamesizeof(assistName));
    
  
    if(
victim == attacker || attacker == 0){    
          return;
      }
    
    
GivePoints(attacker10);

    
PrintToChatAll("%s killed %s | Points: %i"attackerNamevictimNamemessagePoints);

    if(
assist){
        
GivePoints(assist2);
         
PrintToChatAll("%s helped killing %s | Points: %i"assistNamevictimNamemessagePoints);
    }    
}

public 
void GivePoints(int targetIDint points) {
    
    if (
AreClientCookiesCached(targetID))
    {
    
        
char sCookieValue[12];
        
GetClientCookie(targetIDg_hPointCookiesCookieValuesizeof(sCookieValue));
        
int Points StringToInt(sCookieValue);
        
Points += points;
        
         
messagePoints Points;
 
        
IntToString(PointssCookieValuesizeof(sCookieValue));
 
        
SetClientCookie(targetIDg_hPointCookiesCookieValue);
    }

Love you guys ;)
JPTRON is offline
Reply


Thread Tools
Display Modes

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:25.


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