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

Variables can be declared in multiple ways, but what's the difference?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gromit190
Junior Member
Join Date: Jun 2019
Old 06-11-2019 , 07:59   Variables can be declared in multiple ways, but what's the difference?
Reply With Quote #1

Hi,


I'm a little bit confused about how a variable is declared and instantiated.

From what I understand, variables can be declared in 3 different ways (example using a StringMap variable):

1. new Float:myFloat
2. decl Float:myFloat
3. Float myFloat

All 3 methods declare a new variable named "myFloat".
Method 1 declares and instantiates myFloat, meaning that there is no previous data in this variable (data is set to 0, for other types everything would be 0, false etc).
Method 2 declares myFloat, but there may be "leftover" data. So depending on what is previously in computer memory, there may be some data in the variable. This can only be done in a local scope (inside a function).
Method 3 ???

The difference between method 1 and 2 are okay I guess, but what's the difference between method 1 and 3?
Is the data set to 0 when using method 3?
If I want a global variable in my plugin, should I use method 1 or 3?


Code:
#include <sourcemod>

Float myGlobalVariable;

public void OnPluginStart()
{
    myGlobalVariable = 0.0;
}

Last edited by gromit190; 06-11-2019 at 07:59.
gromit190 is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 06-11-2019 , 08:13   Re: Variables can be declared in multiple ways, but what's the difference?
Reply With Quote #2

You have to use only one modern way:
PHP Code:
float myFloat
All another ways are deprecated syntax.
Before 1.7 was "old syntax" - Pawn syntax.
Since 1.7 SourcePawn have own modern C-like syntax - "Transitional syntax"
Wiki pages:
Introduction to SourcePawn 1.7
SourcePawn Transitional Syntax
Introduction to SourcePawn
Kailo is offline
gromit190
Junior Member
Join Date: Jun 2019
Old 06-11-2019 , 08:29   Re: Variables can be declared in multiple ways, but what's the difference?
Reply With Quote #3

I see, thanks!


I still don't quite see when a variable just reserves some memory, and when it actually clears that memory.


Say I wanted to declare a variable, but I don't want to use any CPU power to wipe pre-existing data. I just want to reserve memory and write to it later.

How is it possible? I guess the "old" way was to use "decl".
gromit190 is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 06-11-2019 , 14:15   Re: Variables can be declared in multiple ways, but what's the difference?
Reply With Quote #4

Use global variables to keep data and use them later.
Use local variables in functions to work with them only in that function.
Use static variables in functions to work with them only in that function AND keep data for the next call of that function.

PHP Code:

#include <sourcemod>

float myGlobalVariable;

public 
void OnPluginStart()
{
    
myGlobalVariable 0.0;

this is a global variable, you can use it in every function. the memory will be erased on plugin end

PHP Code:
public void OnPluginStart()
{
    
float myGlobalVariable 0.0;

this is a local variable, you can use it only in that function. the memory its erased.

PHP Code:
public void OnPluginStart()
{
    static 
float myGlobalVariable 0.0;
    print 
myGlobalVariable // after second call this will show 1.0, not 0.0
    
myGlobalVariable 1.0;

this is a static local variable, you can use it only in that function. the memory its still there
__________________
Ilusion9 is offline
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 06-11-2019 , 14:27   Re: Variables can be declared in multiple ways, but what's the difference?
Reply With Quote #5

You strongly recommended to not use "decl". "decl" was just experiment for optimization for big size strings and arrays, like char[2048]. Results of experiment: decl not needed.
You have to not think so much about speed or memory then programming with SourcePawn. Just don't think about it. Compiler will make all optimization for you that necessary.
"Zeroing" all memory helps to make language more safety and stable.
Kailo is offline
gromit190
Junior Member
Join Date: Jun 2019
Old 06-12-2019 , 02:53   Re: Variables can be declared in multiple ways, but what's the difference?
Reply With Quote #6

Much appreciated, thank you!
gromit190 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 18:03.


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