AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [Terrible Idea] Using the same function to set/get a string (https://forums.alliedmods.net/showthread.php?t=281224)

humbugtheman 04-05-2016 17:41

[Terrible Idea] Using the same function to set/get a string
 
You shouldn't do this as it's pointless and ugly, but I put this together because I was interested in the idea. The Name function can be used to set or get a string, depending on what parameter you pass to it.
PHP Code:

// string 'type'
#define LENGTH 255
#define BUFFER "a_string_nobody_would_ever_use"

char g_Name[LENGTH];

public 
void OnPluginStart(){
    
    
// We set g_Name with the Name function
    
Name("Hello world!");
    
    
// We get g_Name with the Name function
    
char NameBuffer[LENGTH] = BUFFER;
    
Name(NameBuffer);
    
}

void Name(char[] Name){
    
    
// 'get'
    
if (IsBuffer(Name)){
        
strcopy(NameLENGTHg_Name);
    
    
// 'set'
    
} else {
        
strcopy(g_NameLENGTHName);
    }
}

bool IsBuffer(const char[] s){
    return 
StrEqual(sBUFFER);



h3bus 04-06-2016 10:32

Re: [Terrible Idea] Using the same function to set/get a string
 
Well done, you made me puke!

Farbror Godis 04-06-2016 11:12

Re: [Terrible Idea] Using the same function to set/get a string
 
That's the worst piece of code on this forums

humbugtheman 04-06-2016 11:27

Re: [Terrible Idea] Using the same function to set/get a string
 
Where do I collect my award?

Chdata 04-06-2016 13:34

Re: [Terrible Idea] Using the same function to set/get a string
 
The thing you set to name is hardcoded...

humbugtheman 04-06-2016 14:03

Re: [Terrible Idea] Using the same function to set/get a string
 
Correct! It is temporarily set to a hardcoded value before being overriden by the Name function. I did warn you: "terrible idea". Just a little 'what if' concept that shouldn't actually be used.

Mitchell 04-06-2016 14:54

Re: [Terrible Idea] Using the same function to set/get a string
 
Why not just use a blank string

humbugtheman 04-06-2016 15:16

Re: [Terrible Idea] Using the same function to set/get a string
 
Because you might want to set the string to a blank string.

KissLick 04-07-2016 05:45

Re: [Terrible Idea] Using the same function to set/get a string
 
Why not pass string max length as always (default value = 0) and in the function check:

if the length is > 0 - Get
if the length is <= 0 - Set

PHP Code:

SetGetFunction(String:myString[], stringMaxLength 0)
{
    if (
stringMaxLength 0) { // get
        
strcopy(myStringstringMaxLengthSomeStringTakenFromSomewhere);
    } else { 
// set
        
strcopy(SomeStringTakenFromSomewheresizeof(SomeStringTakenFromSomewhere), myString);
    }


P.S.: If you totally want to do this sh*t...

humbugtheman 04-07-2016 05:54

Re: [Terrible Idea] Using the same function to set/get a string
 
Yeah that would be slightly less insane.


All times are GMT -4. The time now is 18:55.

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