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

Setting Strings


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 09-27-2022 , 23:31   Setting Strings
Reply With Quote #1

Hello, I have a question, I need to place many names, which would be faster/efficient, declare the names in the same function, or create a variable with the list of names and then call them, here are differents ways

First Way:

PHP Code:
stock SetNameType__int_EntityType )
{
    switch(
Type)
    {
           case 
1:
           {
               
entity_set_string(__int_Entity,EV_SZ_classname,"Rogue");
           }
           case 
2:
           {
               
entity_set_string(__int_Entity,EV_SZ_classname,"Warrior");
           }
           case 
3:
           {
               
entity_set_string(__int_Entity,EV_SZ_classname,"Wizzard");
           } 
// And a few more
      
}

Second Way:

PHP Code:

new const Names[][] = // Global
{
       
"Warrior",
       
"Rogue",
       
"Wizzard"
}
stock SetNameType__int_EntityType )
{        
    switch(
Type)
    {
           case 
1:
           {
              
entity_set_string(__int_Entity,EV_SZ_classname,Names[0]);
           }
           case 
2:
           {
             
entity_set_string(__int_Entity,EV_SZ_classname,Names[1]);
           }
           case 
3:
           {
             
entity_set_string(__int_Entity,EV_SZ_classname,Names[2]);
           } 
// And a few more
      
}

i can set it directly like this:

PHP Code:

new Names[][] = // Global
{
       
"Warrior",
       
"Rogue",
       
"Wizzard"
}
stock SetNameType__int_EntityType )
{        
         
entity_set_string(__int_Entity,EV_SZ_classname,Names[Type]);

But i need to use other switch for other purpose, but, what is better, set the string directly, or with a global variable( Way 1 or 2 );

Last edited by MrPickles; 09-27-2022 at 23:35. Reason: Grammatic
MrPickles is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-27-2022 , 23:51   Re: Setting Strings
Reply With Quote #2

I would normally do it the last way. I doubt there would be any significant performance difference but if the list gets long, it can make the last method much easier to update and maintain.

Even if you do need a switch for other things, you can simply use the switch for the things that absolutely need it.
__________________
fysiks is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 09-28-2022 , 00:36   Re: Setting Strings
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
I would normally do it the last way. I doubt there would be any significant performance difference but if the list gets long, it can make the last method much easier to update and maintain.

Even if you do need a switch for other things, you can simply use the switch for the things that absolutely need it.
Here are some others to consider:

The function will be called several times in short periods
The list will be much longer (at least 20)

based on this, it would be better to put the string directly, or call the variable that contains the saved string
MrPickles is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 09-28-2022 , 13:30   Re: Setting Strings
Reply With Quote #4

You are worring about things of no consequence. Use the second method because it is easier to maintain.
__________________
HamletEagle is offline
Reply


Thread Tools
Display Modes

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 01:57.


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