Raised This Month: $51 Target: $400
 12% 

Client Prethink + Static variable


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
vitorrossi
Senior Member
Join Date: Apr 2012
Location: NY, USA
Old 11-28-2012 , 04:37   Client Prethink + Static variable
Reply With Quote #1

If I create a static variable such as

PHP Code:
public client_PreThink(id)
{
    static 
Float:fOrigin[];
    ...

What I am trying to understand is if the variable is inside PreThink as a whole or if there is a separate static variable created to every id. So should I use

PHP Code:
static Float:fOrigin[33][3]; 
or

PHP Code:
static Float:fOrigin[3]; 

Last edited by vitorrossi; 11-28-2012 at 04:37.
vitorrossi is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 11-28-2012 , 05:50   Re: Client Prethink + Static variable
Reply With Quote #2

static creates the variable one time.
Unless you need to store datas of others players, static Float:fOrigin[3]; should be used.
__________________
Arkshine is offline
vitorrossi
Senior Member
Join Date: Apr 2012
Location: NY, USA
Old 11-28-2012 , 13:59   Re: Client Prethink + Static variable
Reply With Quote #3

Well, I need to pev the origin of the id on client pre_think. Since every player thinks, then this static variable will be used for all players. Here is what I need.

PHP Code:
public client_PreThink(id)
{
    static 
Float:fOrigin[3];
    
pev(idpev_originfOrigin);
    ....

So which one should I use?
vitorrossi is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 11-28-2012 , 14:58   Re: Client Prethink + Static variable
Reply With Quote #4

Same answer.
__________________
Arkshine is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 11-28-2012 , 18:27   Re: Client Prethink + Static variable
Reply With Quote #5

Think of it as a global variable.
hleV is offline
vitorrossi
Senior Member
Join Date: Apr 2012
Location: NY, USA
Old 11-28-2012 , 20:21   Re: Client Prethink + Static variable
Reply With Quote #6

Ok, the answers were good but I am still a little confused. I am just gonna test it both ways and see what happens. Thanks for the help

Last edited by vitorrossi; 11-28-2012 at 20:22.
vitorrossi is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-28-2012 , 20:31   Re: Client Prethink + Static variable
Reply With Quote #7

Quote:
Originally Posted by vitorrossi View Post
Ok, the answers were good but I am still a little confused. I am just gonna test it both ways and see what happens. Thanks for the help
How would you do it if it was a global variable? . . . do everything the same way except you would declare it as static inside the function.
__________________
fysiks is offline
vitorrossi
Senior Member
Join Date: Apr 2012
Location: NY, USA
Old 11-29-2012 , 00:38   Re: Client Prethink + Static variable
Reply With Quote #8

Quote:
Originally Posted by Arkshine View Post
static creates the variable one time.
Unless you need to store datas of others players, static Float:fOrigin[3]; should be used.
Quote:
Originally Posted by fysiks View Post
How would you do it if it was a global variable? . . . do everything the same way except you would declare it as static inside the function.
I understand now what you both meant. It makes much more sense, thank you.

Last edited by vitorrossi; 11-29-2012 at 00:38.
vitorrossi is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 11-29-2012 , 01:26   Re: Client Prethink + Static variable
Reply With Quote #9

Consider it as :

new Float:fOrigin[3]

Differences are :

Amxx doesn't allocate memory each time PreThink is called.
Amxx doesn't release memory each time PreThink is exit.

So, when PreThink is called, variable is remaining with old values, that's something you don't bother in your case because you fill the variable each time PreThink is called.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod 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:45.


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