Raised This Month: $ Target: $400
 0% 

Type casting in functions


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
orglee
Member
Join Date: Dec 2005
Old 08-22-2009 , 10:44   Type casting in functions
Reply With Quote #1

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?
orglee is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-22-2009 , 10:48   Re: Type casting in functions
Reply With Quote #2

Float:myVar[3]={0.0, 0.0, 0.0}
__________________
Arkshine is offline
orglee
Member
Join Date: Dec 2005
Old 08-22-2009 , 11:17   Re: Type casting in functions
Reply With Quote #3

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
orglee is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-22-2009 , 11:35   Re: Type casting in functions
Reply With Quote #4

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.
__________________
Arkshine is offline
orglee
Member
Join Date: Dec 2005
Old 08-22-2009 , 12:06   Re: Type casting in functions
Reply With Quote #5

I have to use public to I could use this function as native
orglee is offline
Jon
Veteran Member
Join Date: Dec 2007
Old 08-22-2009 , 12:11   Re: Type casting in functions
Reply With Quote #6

Quote:
Originally Posted by orglee View Post
I have to use public
Why?
Jon is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 08-22-2009 , 13:31   Re: Type casting in functions
Reply With Quote #7

Quote:
Originally Posted by Jon View Post
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.
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
Jon
Veteran Member
Join Date: Dec 2007
Old 08-22-2009 , 17:09   Re: Type casting in functions
Reply With Quote #8

Quote:
Originally Posted by ot_207 View Post
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.
Jon is offline
orglee
Member
Join Date: Dec 2005
Old 08-22-2009 , 13:14   Re: Type casting in functions
Reply With Quote #9

PHP Code:
plugin_natives()
{
  
register_library(...
  
register_native("cp_teleport"...

Thats the reason.
orglee is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-22-2009 , 13:15   Re: Type casting in functions
Reply With Quote #10

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..
__________________

Last edited by Arkshine; 08-22-2009 at 13:17.
Arkshine 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 15:06.


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