Raised This Month: $23 Target: $400
 5% 

[INC] Data String Parameter


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 07-29-2016 , 19:15   [INC] Data String Parameter
Reply With Quote #1

I got tired of having to create data packs manually or convert between strings and handles to be able to pass a string through a data parameter so I made these stocks.

Note: If there's a better method of doing this that already exists, let me know as I would LOVE to know it.
Note2: I know you can convert a handle to a string which you can also do but this seems less like a disaster.

Code:
#if defined _data_string_parameter_included_
  #endinput
#endif
#define _data_string_parameter_included_ "1.0"

stock Handle AnyString(const char[] sBuffer)
{
	if (strlen(sBuffer) < 1)
	{
		return null;
	}
	
	Handle hPack = CreateDataPack();
	WritePackString(hPack, sBuffer);
	
	return hPack;
}

stock bool ReadAnyString(Handle hPack, char[] sBuffer, int size)
{
	if (hPack == null)
	{
		return false;
	}
	
	ResetPack(hPack);
	ReadPackString(hPack, sBuffer, size);
	CloseHandle(hPack);
	return true;
}
Example:
Code:
#include <sourcemod>
#include <data_string_parameter>

#pragma semicolon 1
#pragma newdecls required

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

public void OnPluginStart()
{
	char sBuffer[12];
	strcopy(sBuffer, sizeof(sBuffer), "Test");
	SQL_TConnect(OnSQLConnect, "default", AnyString(sBuffer));
}

public void OnSQLConnect(Handle owner, Handle hndl, const char[] error, any data)
{
	char sBuffer[12];
	ReadAnyString(data, sBuffer, sizeof(sBuffer));
	
	PrintToServer(sBuffer);
}
Attached Files
File Type: inc data_string_parameter.inc (501 Bytes, 240 views)
__________________
(soon to be) Retired Sourcemod Coder
My [Dev] Website | Discord: Drixevel#5049

Last edited by Drixevel; 07-31-2016 at 13:38.
Drixevel is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 07-31-2016 , 01:19   Re: [INC] Data String Parameter
Reply With Quote #2

Your example looks broken

Why would anyone ever need this? You'd simply pass const char sBuffer into the function call. If you need multiple, pass the amount and use [][]. This seems a little insane

PHP Code:
//Somewhere in the vast emptiness of a plugins source code
char sBuffer[12];
strcopy(sBuffersizeof(sBuffer), "Carrot");
FunctionCall(sBuffer)

void FunctionCall(const char sBuffer[])
{
    
//Do stuff

KyleS is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 07-31-2016 , 04:39   Re: [INC] Data String Parameter
Reply With Quote #3

Because you can't pass an array into a non-array parameter.
Fyren is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 07-31-2016 , 13:35   Re: [INC] Data String Parameter
Reply With Quote #4

Quote:
Originally Posted by KyleS View Post
Your example looks broken

Why would anyone ever need this? You'd simply pass const char sBuffer into the function call. If you need multiple, pass the amount and use [][]. This seems a little insane

PHP Code:
//Somewhere in the vast emptiness of a plugins source code
char sBuffer[12];
strcopy(sBuffersizeof(sBuffer), "Carrot");
FunctionCall(sBuffer)

void FunctionCall(const char sBuffer[])
{
    
//Do stuff

Code:
public void OnPluginStart()
{
	char sBuffer[12];
	strcopy(sBuffer, sizeof(sBuffer), "Test");
	SQL_TConnect(OnSQLConnect, "default", sBuffer);
}

public void OnSQLConnect(Handle owner, Handle hndl, const char[] error, const char[] sBuffer)
{
	PrintToServer(sBuffer);
}
Example using SQL_TConnect since it has the 'any data' or 'any ...' parameters to pass through cells. That doesn't work because the prototype is different so it outputs an error.

This works:
Code:
#include <sourcemod>
#include <data_string_parameter>

#pragma semicolon 1
#pragma newdecls required

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

public void OnPluginStart()
{
	char sBuffer[12];
	strcopy(sBuffer, sizeof(sBuffer), "Test");
	SQL_TConnect(OnSQLConnect, "default", AnyString(sBuffer));
}

public void OnSQLConnect(Handle owner, Handle hndl, const char[] error, any data)
{
	char sBuffer[12];
	ReadAnyString(data, sBuffer, sizeof(sBuffer));
	
	PrintToServer(sBuffer);
}
Quote:
Originally Posted by Fyren View Post
Because you can't pass an array into a non-array parameter.
Sadly.
__________________
(soon to be) Retired Sourcemod Coder
My [Dev] Website | Discord: Drixevel#5049

Last edited by Drixevel; 07-31-2016 at 13:41.
Drixevel is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 07-31-2016 , 15:13   Re: [INC] Data String Parameter
Reply With Quote #5

Check out DString, it should accomplish what you are doing with a few other bugs features.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
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 16:59.


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