AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Can you pass a string to a function like this? (https://forums.alliedmods.net/showthread.php?t=26786)

Geesu 04-09-2006 09:42

Can you pass a string to a function like this?
 
Basically if you have this function :

public myfunc( blah, blah[], ... )
{

}

Can you pass something like:

myfunc( 12, "hello %s %s", "world", "!" );

Is this at all possible? I just don't want to keep doing a format() before a function call...

Thanks,
Josh

Hawk552 04-09-2006 10:16

Not as far as I know. The way string formatting works is by using format in whatever function allows you to do it. For example, client_print allows you to format strings because it has format coded inside it.

If you somehow coded format into your function, you might be able to, but otherwise I don't think so.

Geesu 04-09-2006 10:21

Yea I was worried that it would have to be a module function :/

Hawk552 04-09-2006 10:29

If you know the exact number of args, you could probably do this, although I don't know how safe it is:

Code:
my_func1() {      new string[64] = "Hello %s, and die"      new name[] = "Hawk552"      my_func2(string,63,name) } my_func2(str[],len,format1[]) {      format(string,len,string,format1) }

Brad 04-09-2006 11:17

Yes it's possible. I'll try to find the functions in a bit and then post back.

Brad 04-09-2006 11:40

Ah yes... Found it.

I use this while debugging my plugins. The part that you're most interested in is my use of the format_args function. It essentially acts as the format function and, as such, returns a formatted string.

Let me know if you have any questions.

Code:
stock debug_log(const textID[] = "", const text[] = "", {Float,_}:...) {       new debugger = get_cvar_num("badcamper_debug");     if (debugger || debugger == -1)     {         // format the text as needed         new formattedText[1024];         format_args(formattedText, 1023, 1);         // if there's a text identifier, add it         if (textID[0])             format(formattedText, 1023, "[%s] %s", textID, formattedText);         // log text to file         log_to_file("_badcamper.log", formattedText);         // if there's someone to show text to, do so         if (debugger && is_user_connected(debugger))             client_print(debugger, print_chat, formattedText);     }     // not needed but gets rid of stupid compiler error     if (text[0] == 0) return; }

Twilight Suzuka 04-09-2006 12:40

There are also functions for getting the unknown arguements, and setting them, as they are all passed by reference.

Geesu 04-09-2006 15:45

Thanks guys, Brad I think that's exactly what I need...

:)

Geesu 04-20-2006 01:27

Guys I'm getting total garbage...

This is how I call it:

Code:
            WC3_Status_Text( id, 3.5, HUDMESSAGE_POS_INFO, "%L", id, "SERPENT_WARD", p_data[id][P_SERPENTCOUNT] );

This is the function:

Code:
// Function will print a message in the center of the screen WC3_Status_Text( id, Float:fDuration, Float:iYPos, const text[] = "", {Float,_}:... ) {     new szFormattedText[128];     format_args( szFormattedText, 127, 3 );     // Check for Counter-Strike or Condition Zero     if ( g_MOD == GAME_CSTRIKE || g_MOD == GAME_CZERO )     {         set_hudmessage( 255, 208, 0, HUDMESSAGE_POS_CENTER, iYPos, 0, 6.0, fDuration, 0.1, 0.5, -1 );         show_hudmessage( id, szFormattedText );     }     // Check for Day of Defeat     else if ( g_MOD == GAME_DOD )     {         Create_HudText( id, szFormattedText, 1 );     }         // Gets rid of compiler warning     if ( text[0] == 0 )     {         return;     } }

Can I not do the %L stuff w/format_args ??

BAILOPAN 04-20-2006 01:43

NOTE: format_args() is deprecated. It was never ported to the new format() system. It's also a really messy native that I think is basically unused.

I'll try to add something new and pretty, like:
Code:
void myfunc(text[], maxlen, const fmt[], {Float,_}:...) {    vformat(text, maxlen, fmt, 4) }

Also, I think your 3 should be a 5 (not entirely sure, maybe a 4).


All times are GMT -4. The time now is 16:42.

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