Raised This Month: $12 Target: $400
 3% 

New Coder- some syntax and basic errors


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hedgemon
Junior Member
Join Date: May 2017
Old 06-14-2017 , 13:58   New Coder- some syntax and basic errors
Reply With Quote #1

Hi! I'm new to coding so if you notice any mistakes or room for better coding habits please let me know! I am also mixing some old + new syntax still.

Code:
#pragma semicolon 1
#pragma newdecls required

#define DEBUG

#define PLUGIN_AUTHOR ""
#define PLUGIN_VERSION "0.00"

#include <sourcemod>
#include <sdktools>
#include <cstrike>

public Plugin myinfo = 
{
	name = "",
	author = PLUGIN_AUTHOR,
	description = "",
	version = PLUGIN_VERSION,
	url = ""
};

static char KVPath[PLATFORM_MAX_PATH];

public void OnPluginStart()//Store playername + steamid
{
	CreateDirectory("addons/sourcemod/data/TTT", 3);
	BuildPath(Path_SM, KVPath, sizeof(KVPath), "data/TTT/playerinfo.txt");
}

public void OnClientPutInServer(int client)
{
	SavePlayerInfo(client);
}

public void SavePlayerInfo(int client)
{
	Handle DB = CreateKeyValues("PlayerInfo");
	FileToKeyValues(DB, KVPath);
	char SID[32];
	GetClientAuthString(client, SID, sizeof((SID));
	
	if (KvJumpToKey(DB, SID, true))
	{
		char name[MAX_NAME_LENGTH];
		char temp_name[MAX_NAME_LENGTH];
		GetClientName(client, name, sizeof(name));
		KvGetString(DB, "name", temp_name, sizeof(temp_name), "NULL");
		int connections = KvGetNum(DB, "connections", 0);
		int karma = KvGetNum(DB, "karma", 125);
		
		if StrEqual(temp_name, "NULL") && connections = 0)
		{
			PrintToChatAll("Welcome %s to Server for the first time!", name);
		}
		else
		{
			PrintToChatAll("Welcome back %s! Last known here as %s. They have connected %d times.", name, temp_name, connections);
		}
		connections++;
		KvSetNum(DB, "connections", connections);
		KvSetString(DB, "name", name);
		KvRewind(DB);
		KeyValuesToFile(DB, KVPath);
		CloseHandle(DB);
	
		
	}
}
Hedgemon is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 06-14-2017 , 14:03   Re: New Coder- some syntax and basic errors
Reply With Quote #2

It's not recommended to use KV to save player information as it uses the file system to store the kv file.
Mitchell is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 06-14-2017 , 14:49   Re: New Coder- some syntax and basic errors
Reply With Quote #3

yeah use a sql db
Miu is offline
Hedgemon
Junior Member
Join Date: May 2017
Old 06-14-2017 , 14:51   Re: New Coder- some syntax and basic errors
Reply With Quote #4

What is new syntax for GetClientAuthString, i plan on learning more on sql soon!

Last edited by Hedgemon; 06-14-2017 at 14:52.
Hedgemon is offline
B3none
AlliedModders Donor
Join Date: Oct 2016
Location: United Kingdom
Old 06-14-2017 , 14:55   Re: New Coder- some syntax and basic errors
Reply With Quote #5

https://sm.alliedmods.net/new-api/cl...etClientAuthId

Take a good look around the API, I often find myself looking at this
__________________
B3none is offline
Hedgemon
Junior Member
Join Date: May 2017
Old 06-14-2017 , 15:00   Re: New Coder- some syntax and basic errors
Reply With Quote #6

Updated code still need to figure out sql but i do wish to be able to compile this. issue is this line
if StrEqual(temp_name, "NULL") && connections = 0)

Last edited by Hedgemon; 06-14-2017 at 15:56.
Hedgemon is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 06-14-2017 , 15:02   Re: New Coder- some syntax and basic errors
Reply With Quote #7

Also you shouldn't be hardcoding the directory to /addons/sourcemod use BuildPath instead.

if StrEqual(temp_name, "NULL") && connections = 0)

to

if StrEqual(temp_name, "NULL") && connections == 0)

Last edited by xerox8521; 06-14-2017 at 15:02.
xerox8521 is offline
Hedgemon
Junior Member
Join Date: May 2017
Old 06-14-2017 , 15:25   Re: New Coder- some syntax and basic errors
Reply With Quote #8

Error is on the same line

if StrEqual((temp_name, "NULL") && connections == 0)

KeyValues!!.sp
D:\DSCSGO\Editor\My scripts\KeyValues!!.sp(50) : error 033: array must be indexed (variable "-unknown-")


I also removed karma because I wont be calling it in this function

Code:
#pragma semicolon 1
#pragma newdecls required

#define DEBUG

#define PLUGIN_AUTHOR ""
#define PLUGIN_VERSION "0.00"

#include <sourcemod>
#include <sdktools>
#include <cstrike>

public Plugin myinfo = 
{
	name = "",
	author = PLUGIN_AUTHOR,
	description = "",
	version = PLUGIN_VERSION,
	url = ""
};

static char KVPath[PLATFORM_MAX_PATH];

public void OnPluginStart()//Store playername + steamid
{
	CreateDirectory("addons/sourcemod/data/TTT", 3);
	BuildPath(Path_SM, KVPath, sizeof(KVPath), "data/TTT/playerinfo.txt");
}

public void OnClientPutInServer(int client)
{
	SavePlayerInfo(client);
}

public void SavePlayerInfo(int client)
{
	Handle DB = CreateKeyValues("PlayerInfo");
	FileToKeyValues(DB, KVPath);
	char SID[32];
	GetClientAuthId(client, AuthId_Steam2, SID, sizeof(SID));
	
	if (KvJumpToKey(DB, SID, true))
	{
		char name[MAX_NAME_LENGTH];
		char temp_name[MAX_NAME_LENGTH];
		GetClientName(client, name, sizeof(name));
		KvGetString(DB, "name", temp_name, sizeof(temp_name), "NULL");
		int connections = KvGetNum(DB, "connections", 0);

		if StrEqual((temp_name, "NULL") && connections == 0)
		{
			PrintToChatAll("Welcome %s to  Server for the first time!", name);
		}
		else
		{
			PrintToChatAll("Welcome back %s! Last known here as %s. They have connected %d times.", name, temp_name, connections);
		}
		connections++;
		KvSetNum(DB, "connections", connections);
		KvSetString(DB, "name", name);
		KvRewind(DB);
		KeyValuesToFile(DB, KVPath);
		CloseHandle(DB);
	
		
	}
}

Last edited by Hedgemon; 06-14-2017 at 16:36.
Hedgemon is offline
B3none
AlliedModders Donor
Join Date: Oct 2016
Location: United Kingdom
Old 06-14-2017 , 15:46   Re: New Coder- some syntax and basic errors
Reply With Quote #9

I also recommend https://spider.limetech.io/ as a SourcePawn IDE
__________________
B3none is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 06-14-2017 , 17:16   Re: New Coder- some syntax and basic errors
Reply With Quote #10

Quote:
Originally Posted by Hedgemon View Post
PHP Code:
if StrEqual((temp_name"NULL") && connections == 0
PHP Code:
if (StrEqual(temp_name"NULL") && connections == 0
The parentheses were wrong.

Last edited by Fyren; 06-14-2017 at 17:16.
Fyren 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 13:20.


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