AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Type casting in functions (https://forums.alliedmods.net/showthread.php?t=101269)

orglee 08-22-2009 10:44

Type casting in functions
 
Hello. I would like to create function with default value.
PHP Code:

(stock/public) myFunction(Float:myVar={0.00.00.0})
{
  
// Function body


It will work BUT only if I'll declare it as 'stock' instead of 'public'. I want it to be a public to use it as native function ( usable in other plugins ) Is there any solution to do this without copying this 'stock' function into every plugin I'm gonna use it in?

Arkshine 08-22-2009 10:48

Re: Type casting in functions
 
Float:myVar[3]={0.0, 0.0, 0.0}

orglee 08-22-2009 11:17

Re: Type casting in functions
 
Oh darn I forgot to put it in my example.

PHP Code:

(stock/public) myFunction(Float:myVar[3]={0.00.00.0})
{
  
// Function body
}

public 
cp_teleport(iPlayerFloat:fPosition[3], Float:fAngles[3]={0.00.00.0})
{
    
set_pev(iPlayerpev_velocityFloat:{0.00.00.0});
    
set_pev(iPlayerpev_originfPosition);
    
    if(
fAngles[0] != 0.0 && fAngles[1] != 0.0 && fAngles[2] != 0.0)
    {
        
entity_set_vector(iPlayerEV_VEC_anglesfAngles);
        
entity_set_int(iPlayerEV_INT_fixangle1);
        
DuckDelay(iPlayer);
    }


Problem still remains.

Cause when function is public parser shows me fallowing error.

Quote:

Error: Function argument may not have a default value (variable "fAngles") on line 496

Arkshine 08-22-2009 11:35

Re: Type casting in functions
 
Because if you put 'public', it means that you function is a callback from a forward, meaning with specific and defined params. If your function is not a callback, you don't have to use public but keeping it private.

orglee 08-22-2009 12:06

Re: Type casting in functions
 
I have to use public to I could use this function as native

Jon 08-22-2009 12:11

Re: Type casting in functions
 
Quote:

Originally Posted by orglee (Post 909156)
I have to use public

Why?

orglee 08-22-2009 13:14

Re: Type casting in functions
 
PHP Code:

plugin_natives()
{
  
register_library(...
  
register_native("cp_teleport"...


Thats the reason.

Arkshine 08-22-2009 13:15

Re: Type casting in functions
 
Don't use the same name for natives name and the callback.

register_native ( "cp_teleport", "_cp_teleport" ); for example. or just native_cp_teleport.

public _cp_teleport ()
{

}

And since it's a native, the params are not passed like that. If I remember you have to use get_param() etc..

ot_207 08-22-2009 13:31

Re: Type casting in functions
 
Quote:

Originally Posted by Jon (Post 909161)
Why?

Because public means that the function can be accessed. If you make it a stock it will be as a private function and when you will use the native you will end up with a crash or a amxx error.

Jon 08-22-2009 17:09

Re: Type casting in functions
 
Quote:

Originally Posted by ot_207 (Post 909258)
Because public means that the function can be accessed. If you make it a stock it will be as a private function and when you will use the native you will end up with a crash or a amxx error.

I know...

I just wanted to know why he wanted to use a public type function. His message was bad english too.


All times are GMT -4. The time now is 15:06.

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