AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Multiple function arguments (https://forums.alliedmods.net/showthread.php?t=93875)

orglee 06-03-2009 15:17

Multiple function arguments
 
Hello. I'm wondering, is there a way to create function which amounts of arguments depends on how that function is called. For instance a function like format() that can take 3 arguments 4, 5 etc...

Arkshine 06-03-2009 15:38

Re: Multiple function arguments
 
Something like if I'm right and understand your question :

Code:
MyFunction( const Text[], any:... ) {     new NewText[ 128 ];     vformat( NewText, charsmax( NewText ), Text, 2 );         // }

eg : MyFunction( "Hello %s ! Today %d %c %d - %f", "world", 21, ':', 38, 2009.0 );

will look :
Code:

NewText = Hello world ! Today 21 : 38 - 2009.000000

orglee 06-03-2009 16:16

Re: Multiple function arguments
 
Thank you. And what if I don't know approximate length of a Text.

Can I determine it somehow and create array appropriate for that string? Lengths of strings I would be using with that function can vary on different conditions. If I would adjust array length to longest string it would be simply a waste of server resources.

edit>
Thats what I'm trying to do

Code:
public Handle:SQLQueryCreate(const sQuery[], any:...) {     new sText[1024]         vformat(sText, strlen(sQuery), sQuery, 2);         log_amx("[CLIMB][STATS][QUERY] %s", sText)         return SQL_PrepareQuery(g_SqlConnection, sText) }

Arkshine 06-03-2009 16:35

Re: Multiple function arguments
 
public Handle:SQLQueryCreate(

Since your return an Handle type, you have to tag your function with Handle.

Also, use sizeof not strlen. charsmax is macro from amxx 1.8.x which is : ( sizeof %1 - 1 )

orglee 06-03-2009 16:43

Re: Multiple function arguments
 
Yeah I figured it out myself a while after editing. ;P

And what about that array issue? As you see I've declared sText 1024 long and I'm really not sure it would be enough. I hope there is a way to create string with dynamic length.

Arkshine 06-03-2009 16:47

Re: Multiple function arguments
 
Not possible AFAIK. I'm sure you can determine a max.

orglee 06-03-2009 17:34

Re: Multiple function arguments
 
Well to bad, but I was about to change that function to normal SQL_PrepareQuery() anyway after I'll finish debuging. - Or not if you can tell me how to check if plugin is in debuging mode ( "plugin.amxx debug" in plugins.ini ) or not.

I still have two problems that are not especially connected to this topic, but those are probably caused by my stupid mistakes so I think they don't deserve another topic.

So here it goes:
1. Weird number is being inserted/fetched to/from database.
Code:
new iPlayerID new Handle:InsertQuery_1, Handle:InsertQuery_2, Handle:InsertQuery_3 InsertQuery_1 = SQLQueryCreate("INSERT INTO `stats_player`(`sp_steamid`, `sp_lastseen`) VALUES('%s', NOW())", sAuthid) if(!SQL_Execute(InsertQuery_1)) { SQLQueryError(InsertQuery_1, "Unable to insert new player"); } InsertQuery_2 = SQLQueryCreate("SELECT `sp_id` FROM `stats_player` WHERE `sp_steamid` = '%s'", sAuthid) if(!SQL_Execute(InsertQuery_2)) { SQLQueryError(InsertQuery_2, "Unable to retrive inserted player id"); } SQL_ReadResult(InsertQuery_2, 0, iPlayerID) InsertQuery_3 = SQLQueryCreate("INSERT INTO `stats_player_names`(`spn_name`, `spn_timesused`, `_sp_id`) VALUES('%s', 1, %i)", sPlayerName, iPlayerID) if(!SQL_Execute(InsertQuery_3)) { SQLQueryError(InsertQuery_3, "Unable to insert player nickname"); }
It creates two fallowing queries:
Code:

SELECT `sp_id` FROM `stats_player` WHERE `sp_steamid` = 'STEAM_0:1:12257932'
INSERT INTO `stats_player_names`(`spn_name`, `spn_timesused`, `_sp_id`) VALUES('ajt', 1, 1096810496)

I tried to use SQL_GetInsertId () and event "SELECT LAST_INSERT_ID()" but it wasnt working at all, thats why I'm using "SELECT"

Where first query is ok, second one is far from it as third value ( _sp_id ) I should get 14. What is going on?

2. To write players map time I want to use forwarding methods.
Code:
new iReturn, iForward = CreateMultiForward("hndl_ClimbCounterStop",ET_IGNORE,FP_CELL) if(iForward < 0) { log_amx("[CLIMB][ERROR] Unable to create forward."); } if(!ExecuteForward(iForward,iReturn,id-1,timer[id-1][TMR_BSTTME])) {     log_amx("[CLIMB][ERROR] Unable to execute forward.") }   DestroyForward(iForward)
Code:

[AMXX] Run time error 10: native error (native "ExecuteForward")
edit>
Damn wrong button - still writing
edit>>
I think I'm done writing... sorry...

Exolent[jNr] 06-03-2009 17:37

Re: Multiple function arguments
 
How did you create the iPlayerID variable?

orglee 06-03-2009 18:13

Re: Multiple function arguments
 
new iPlayerID
new Float:fPlayerID
new sPlayerID[12]

In first two cases result is the same ( 1096810496 )
When i tried retrieving id as string "SQL_ReadResult(InsertQuery_2, 0, sPlayerID, 11) i got 49 as id.

Exolent[jNr] 06-03-2009 18:24

Re: Multiple function arguments
 
Do you mind showing the full code for this function?
Also, are you using SQLx? Because I've never seen the SQLQueryCreate() function.


All times are GMT -4. The time now is 13:53.

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