AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Setting Strings (https://forums.alliedmods.net/showthread.php?t=339719)

MrPickles 09-27-2022 23:31

Setting Strings
 
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 );

fysiks 09-27-2022 23:51

Re: Setting Strings
 
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.

MrPickles 09-28-2022 00:36

Re: Setting Strings
 
Quote:

Originally Posted by fysiks (Post 2789844)
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

HamletEagle 09-28-2022 13:30

Re: Setting Strings
 
You are worring about things of no consequence. Use the second method because it is easier to maintain.


All times are GMT -4. The time now is 09:39.

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