Raised This Month: $204 Target: $400
 51% 

Simple Player logger plugin (problem with enums)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
cezarislt
New Member
Join Date: Jul 2017
Old 07-08-2017 , 07:30   Simple Player logger plugin (problem with enums)
Reply With Quote #1

Hi AM, I'm new to SourceMod scripting but have been programming for over 5yr. This lower level language is trowing me of coming from JavaScript. Anyway wring a basic script that will store user data (will eventual come from http API, but basic first; the data structure part is not working or compiling for me. here the code its pretty self explanatory:

Code:
#pragma newdecls required

enum e_PlayerStats {
	SteamID[64],
	float:Coins,
	Kills
	//int deaths,
	//int assists,
	//int points,
};

e_PlayerStats g_PlayerStats[MAXPLAYERS + 1];

public bool isClientsDataStored(int client, const char[] steamid)
{
	char[] g_steamid = g_PlayerStats[client][SteamID]; // E: expected token: "-string-", but found "-identifier-"
	
	if( StrEqual(g_steamid , steamid) ){
		return true	
	} else {
		return false;
	}
}

stock e_PlayerStats getClientsData(int client){
	return g_PlayerStats[client];
}

public void storeNewClientData(char[] steamid, int client, float coins){
	g_PlayerStats[client][SteamID] = steamid; //E: expression has no effect, E: expected token : ";" but found "["
	g_PlayerStats[client][Coins] = coins;
}

Any help on this would be great.

Last edited by cezarislt; 07-08-2017 at 07:37.
cezarislt is offline
good_live
AlliedModders Donor
Join Date: Oct 2013
Old 07-08-2017 , 08:24   Re: Simple Player logger plugin (problem with enums)
Reply With Quote #2

enum != datastructure
good_live is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 07-08-2017 , 09:32   Re: Simple Player logger plugin (problem with enums)
Reply With Quote #3

Although you can use enums to emulate structs, your given code won't work even fixing up your enum use. You can't assign arrays like that since they don't have explicitly set (and equal) sizes.

You might want to start out with the tutorial articles on the wiki or looking through existing code if you already have a grasp on programming in general.

Last edited by Fyren; 07-08-2017 at 09:32.
Fyren is offline
sdz
Senior Member
Join Date: Feb 2012
Location: United States
Old 07-08-2017 , 21:15   Re: Simple Player logger plugin (problem with enums)
Reply With Quote #4

another person going through the enum phase of this scripting language

Look into using Dynamic Objects and Properties
__________________
lol
sdz is offline
Kxnrl
AlliedModders Donor
Join Date: May 2015
Old 07-08-2017 , 22:23   Re: Simple Player logger plugin (problem with enums)
Reply With Quote #5

Code:
#pragma newdecls required

enum e_PlayerStats {
	String:SteamID[64],
	Float:Coins,
	Kills
	//int deaths,
	//int assists,
	//int points,
};

e_PlayerStats g_PlayerStats[MAXPLAYERS + 1][e_PlayerStats];

public bool isClientsDataStored(int client, const char[] steamid)
{
    //char[] g_steamid = g_PlayerStats[client][SteamID]; // E: expected token: "-string-", but found "-identifier-"
    
	//if( StrEqual(g_PlayerStats[client][SteamID] , steamid) ){
	//	return true	
	//} else {
	//	return false;
	//}
    
    return StrEqual(g_PlayerStats[client][SteamID] , steamid);
}

//stock e_PlayerStats getClientsData(int client){
//	return g_PlayerStats[client];
//}

public void storeNewClientData(char[] steamid, int client, float coins){
	//g_PlayerStats[client][SteamID] = steamid; //E: expression has no effect, E: expected token : ";" but found "["
    strcopy(g_PlayerStats[client][SteamID], 64, steamid);
	g_PlayerStats[client][Coins] = coins;
}
__________________
Kxnrl is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 07-09-2017 , 07:30   Re: Simple Player logger plugin (problem with enums)
Reply With Quote #6

Quote:
Originally Posted by EasSidezz View Post
another person going through the enum phase of this scripting language

Look into using Dynamic Objects and Properties

Methodmap built using the class builder.

Save this to `include/Dynamic/methodmaps/PlayerStats.inc`

Code:
#if defined _dynamic_class_playerstats_
  #endinput
#endif
#define _dynamic_class_playerstats_

methodmap PlayerStats < Dynamic
{
	public PlayerStats()
	{
		// First we make a new dymanic object
		Dynamic myclass = Dynamic(64, 0);

		// Next we will define all the members
		// -> We do this to force the offsets to always be in the same location
		//    over multiple instances of the same class.
		myclass.SetString("SteamID", "", 64);
		myclass.SetFloat("Coins", 0.0);
		myclass.SetInt("Kills", 0);
		return view_as<PlayerStats>(myclass);
	}

	// Note that I use static offsets to access members.
	// -> This improves performance by caching member offsets
	// -> This is why we force the members in during the contructor
	// -> Failure to force members in the constructor will cause corruption

	public bool GetSteamID(char[] buffer, int length)
	{
		static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
		if (offset == INVALID_DYNAMIC_OFFSET)
		{
			offset = this.GetMemberOffset("SteamID");
			if (offset == INVALID_DYNAMIC_OFFSET)
				SetFailState("A serious error occured in Dynamic!");
		}
		this.GetStringByOffset(offset, buffer, length);
		return true;
	}

	public void SetSteamID(const char[] buffer)
	{
		static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
		if (offset == INVALID_DYNAMIC_OFFSET)
		{
			offset = this.GetMemberOffset("SteamID");
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.SetString("SteamID", buffer);
				return;
			}
		}
		this.SetStringByOffset(offset, buffer);
	}

	property float Coins
	{
		public get()
		{
			static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.GetMemberOffset("Coins");
				if (offset == INVALID_DYNAMIC_OFFSET)
					SetFailState("A serious error occured in Dynamic!");
			}
			return this.GetFloatByOffset(offset);
		}
		public set(float value)
		{
			static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.GetMemberOffset("Coins");
				if (offset == INVALID_DYNAMIC_OFFSET)
				{
					offset = this.SetFloat("Coins", value);
					return;
				}
			}
			this.SetFloatByOffset(offset, value);
		}
	}

	property int Kills
	{
		public get()
		{
			static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.GetMemberOffset("Kills");
				if (offset == INVALID_DYNAMIC_OFFSET)
					SetFailState("A serious error occured in Dynamic!");
			}
			return this.GetIntByOffset(offset);
		}
		public set(int value)
		{
			static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.GetMemberOffset("Kills");
				if (offset == INVALID_DYNAMIC_OFFSET)
				{
					offset = this.SetInt("Kills", value);
					return;
				}
			}
			this.SetIntByOffset(offset, value);
		}
	}
}
Your code converted for Dynamic.

Code:
#include <Dynamic>
#include <Dynamic/methodmaps/PlayerStats>
#pragma newdecls required

public void OnPlayerConnected(int client);
{
	Dynamic settings = Dynamic.GetPlayerSettings(client);
	PlayerStats stats = PlayerStats();
	settings.SetDynamic("PlayerStats", stats);
}

public bool isClientsDataStored(int client, const char[] steamid)
{
	PlayerStats stats = getClientsData(client);
	
	if(stats.CompareString("SteamID", steamid))
		return true;
	else
		return false;
}

stock PlayerStats getClientsData(int client)
{
	return Dynamic.GetPlayerSettings(client).GetDynamic("PlayerStats");
}

public void storeNewClientData(char[] steamid, int client, float coins)
{
	PlayerStats stats = getClientsData(client);
	stats.SetSteamID(steamid);
	stats.Coins = coins;
}
__________________
Neuro Toxin 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 07:32.


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