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

VFormat for natives


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-31-2018 , 07:44   VFormat for natives
Reply With Quote #1

https://sm.alliedmods.net/new-api/fu...atNativeString

I don't understand how to execute this on such piece of code:

Code:
native Core_PrintToChatEyal(const String:Message[], any:...);

public int Native_PrintToChatEyal(Handle plugin, int numParams)
{
	new String:Message[192], String:buffer[192];
	
	GetNativeString(1, Message, sizeof(Message));
	
	// FormatNativeString and buffer will be formatted.

	PrintToChatEyal(buffer);
}
I doubt you'll need the stock PrintToChatEyal or the CreateNative, if you need tell me ( Though I doubt it ).
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334

Last edited by eyal282; 08-31-2018 at 07:47.
eyal282 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 08-31-2018 , 08:39   Re: VFormat for natives
Reply With Quote #2

Code:
public int Native_PrintToChatEyal(Handle plugin, int numParams)
{
	char buffer[192];
	int bufferLength = 0;
	int error = FormatNativeString(0, 1, 2, sizeof(bufer), bufferLength, buffer);
	if (error != SP_ERROR_NONE) {
		ThrowNativeError(error, "failed to format param 1???");
	}

	PrintToChatEyal("%s", buffer);
}
__________________
asherkin is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-31-2018 , 14:37   Re: VFormat for natives
Reply With Quote #3

Quote:
Originally Posted by asherkin View Post
Code:
public int Native_PrintToChatEyal(Handle plugin, int numParams)
{
	char buffer[192];
	int bufferLength = 0;
	int error = FormatNativeString(0, 1, 2, sizeof(bufer), bufferLength, buffer);
	if (error != SP_ERROR_NONE) {
		ThrowNativeError(error, "failed to format param 1???");
	}

	PrintToChatEyal("%s", buffer);
}
Can you briefly explain what each argument means? IDK why I can't put 1+1 for it.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 08-31-2018 , 20:49   Re: VFormat for natives
Reply With Quote #4

Quote:
Originally Posted by eyal282 View Post
Can you briefly explain what each argument means? IDK why I can't put 1+1 for it.
The documentation explains it pretty well, but for your particular case:
  • out_param being 0 means it writes the formatted string out to the char[] referenced in out_string (the char[] buffer) as opposed to a char[] buffer passed in through the native
  • fmt_param and vararg_param being 1 and 2 respectively refer to the args in the native definition for Core_PrintToChatEyal.
  • out_len is the length of the char[] (buffer is spelled incorrectly in the code here)
  • written will store the formatted output's length (here it'll be stored to bufferLength)
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 08-31-2018 at 20:49.
nosoop is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 09-01-2018 , 15:24   Re: VFormat for natives
Reply With Quote #5

Quote:
Originally Posted by nosoop View Post
The documentation explains it pretty well, but for your particular case:
  • out_param being 0 means it writes the formatted string out to the char[] referenced in out_string (the char[] buffer) as opposed to a char[] buffer passed in through the native
  • fmt_param and vararg_param being 1 and 2 respectively refer to the args in the native definition for Core_PrintToChatEyal.
  • out_len is the length of the char[] (buffer is spelled incorrectly in the code here)
  • written will store the formatted output's length (here it'll be stored to bufferLength)
I wanna know if I grasped it correctly.

out_param is unneeded ( and for now I shouldn't care what it is )

fmt_parameter = The parameter number of the string that contains the "%s" and "%i" and etc.
vararg_param = The parameter number of "any:..."
out_len = sizeof(Buffer);
out_string = Buffer
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 09-01-2018 , 18:57   Re: VFormat for natives
Reply With Quote #6

Quote:
Originally Posted by eyal282 View Post
I wanna know if I grasped it correctly.

out_param is unneeded ( and for now I shouldn't care what it is )

fmt_parameter = The parameter number of the string that contains the "%s" and "%i" and etc.
vararg_param = The parameter number of "any:..."
out_len = sizeof(Buffer);
out_string = Buffer
  • You do have to care that the out_param is 0 here.
  • Yes on the rest.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 09-02-2018 , 07:00   Re: VFormat for natives
Reply With Quote #7

Quote:
Originally Posted by nosoop View Post
  • You do have to care that the out_param is 0 here.
  • Yes on the rest.
I'm still confused on what out_param does
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 09-02-2018 , 07:04   Re: VFormat for natives
Reply With Quote #8

Writes the output to a native parameter rather than a local variable.
__________________
asherkin is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 09-02-2018 , 07:09   Re: VFormat for natives
Reply With Quote #9

Quote:
Originally Posted by eyal282 View Post
I'm still confused on what out_param does
I can't think of a proper example so here's a simple one

Code:
native FormatSomeMessage(char[] buffer, int maxlen, const char[] format, any ...);

public int Native_FormatSomeMessage(Handle plugin, int numParams) {
    int maxlen = GetNativeCell(2);
    int written;
    
    // out_param being 1 means the formatted string gets written out to the "buffer" char[] that was passed in
    int result = FormatNativeString(1, 3, 4, maxlen, written);
    if (result != SP_ERROR_NONE) {
        ThrowNativeError("FormatNativeString failed with error");
    }
    return written;
}
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 09-02-2018 at 07:10.
nosoop is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 09-02-2018 , 13:54   Re: VFormat for natives
Reply With Quote #10

Quote:
Originally Posted by nosoop View Post
I can't think of a proper example so here's a simple one

Code:
native FormatSomeMessage(char[] buffer, int maxlen, const char[] format, any ...);

public int Native_FormatSomeMessage(Handle plugin, int numParams) {
    int maxlen = GetNativeCell(2);
    int written;
    
    // out_param being 1 means the formatted string gets written out to the "buffer" char[] that was passed in
    int result = FormatNativeString(1, 3, 4, maxlen, written);
    if (result != SP_ERROR_NONE) {
        ThrowNativeError("FormatNativeString failed with error");
    }
    return written;
}
I want to do this basically, unsure why they made it so complex...:

Code:
native FormatSomeMessage(const char[] format, any ...);

public int Native_FormatSomeMessage(Handle plugin, int numParams) {

    new String:buffer[256];
    VFormatNative(1, 2, buffer, sizeof(buffer)); // 1 is format, 2 is any:...
    PrintToChatEyal(buffer);
    }
    return written;
}
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 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 04:20.


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