Raised This Month: $ Target: $400
 0% 

Can you pass a string to a function like this?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Geesu
Veteran Member
Join Date: Mar 2004
Location: Cincinnati, OH
Old 04-09-2006 , 09:42   Can you pass a string to a function like this?
Reply With Quote #1

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
__________________
Need war3ft help? DO NOT PM ME... Check the forums
Geesu is offline
Send a message via AIM to Geesu Send a message via MSN to Geesu
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 04-09-2006 , 10:16  
Reply With Quote #2

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.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Geesu
Veteran Member
Join Date: Mar 2004
Location: Cincinnati, OH
Old 04-09-2006 , 10:21  
Reply With Quote #3

Yea I was worried that it would have to be a module function :/
__________________
Need war3ft help? DO NOT PM ME... Check the forums
Geesu is offline
Send a message via AIM to Geesu Send a message via MSN to Geesu
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 04-09-2006 , 10:29  
Reply With Quote #4

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) }
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 04-09-2006 , 11:17  
Reply With Quote #5

Yes it's possible. I'll try to find the functions in a bit and then post back.
Brad is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 04-09-2006 , 11:40  
Reply With Quote #6

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; }
Brad is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 04-09-2006 , 12:40  
Reply With Quote #7

There are also functions for getting the unknown arguements, and setting them, as they are all passed by reference.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
Geesu
Veteran Member
Join Date: Mar 2004
Location: Cincinnati, OH
Old 04-09-2006 , 15:45  
Reply With Quote #8

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

__________________
Need war3ft help? DO NOT PM ME... Check the forums
Geesu is offline
Send a message via AIM to Geesu Send a message via MSN to Geesu
Geesu
Veteran Member
Join Date: Mar 2004
Location: Cincinnati, OH
Old 04-20-2006 , 01:27  
Reply With Quote #9

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 ??
__________________
Need war3ft help? DO NOT PM ME... Check the forums
Geesu is offline
Send a message via AIM to Geesu Send a message via MSN to Geesu
BAILOPAN
Join Date: Jan 2004
Old 04-20-2006 , 01:43  
Reply With Quote #10

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).
__________________
egg
BAILOPAN 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 16:42.


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