Raised This Month: $32 Target: $400
 8% 

Store 3 values


Post New Thread Reply   
 
Thread Tools Display Modes
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 10-17-2018 , 19:12   Re: Store 3 values
Reply With Quote #11

Quote:
Originally Posted by Spirit_12 View Post
Another alternative?
2 arrays.
__________________
asherkin is offline
Spirit_12
Veteran Member
Join Date: Dec 2012
Location: Toronto, CA
Old 10-18-2018 , 03:24   Re: Store 3 values
Reply With Quote #12

Well I was hoping you could come up with a better option.
__________________
Spirit_12 is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 10-18-2018 , 04:17   Re: Store 3 values
Reply With Quote #13

You could go with a StringMap to store all the elements you want and even wrap it in a sexy MethodMap. Here's an example I stole from Headline:
PHP Code:
methodmap PlayerData StringMap
{
    public 
PlayerData(int dataint data2float data3)
    {
        
StringMap strMap = new StringMap();

        
strMap.SetValue("data"data);
        
strMap.SetValue("data2"data2);
        
strMap.SetValue("data3"data3);

        return 
view_as<PlayerData>(strMap);
    }

    
property int Data
    
{
        public 
get()
        {
            return 
view_as<int>(this.GetValue("data"));
        }

        public 
set(int val)
        {
            
this.SetValue("data"val);
        }
    }

    
property int Data2
    
{
        public 
get()
        {
            return 
view_as<int>(this.GetValue("data2"));
        }

        public 
set(int val)
        {
            
this.SetValue("data2"val);
        }
    }

    
property float Data3
    
{
        public 
get()
        {
            return 
view_as<float>(this.GetValue("data3"));
        }

        public 
set(float val)
        {
            
this.SetValue("data3"val);
        }
    }


Last edited by hmmmmm; 10-18-2018 at 04:18.
hmmmmm is offline
sdz
Senior Member
Join Date: Feb 2012
Old 10-18-2018 , 19:13   Re: Store 3 values
Reply With Quote #14

or you know a keyvalues object
sdz is offline
Spirit_12
Veteran Member
Join Date: Dec 2012
Location: Toronto, CA
Old 10-19-2018 , 02:45   Re: Store 3 values
Reply With Quote #15

Wouldn’t creating 2 arrays be faster than keyvalues? This function will be called multiple times in a short span, so I’m trying to keep it as light as possible.
__________________
Spirit_12 is offline
fragnichtnach
AlliedModders Donor
Join Date: Oct 2008
Old 10-19-2018 , 03:03   Re: Store 3 values
Reply With Quote #16

Quiet expensive but that would work as well:
Store everything in a string with a delimiter and split and convert the strings afterwards. All you need is a string without an additional array.

edit: nevermind, just read your last post about keeping it as light as possible :-)

Last edited by fragnichtnach; 10-19-2018 at 03:04.
fragnichtnach is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 10-19-2018 , 14:50   Re: Store 3 values
Reply With Quote #17

I prefer that code can be read easily, not too complex structures... so add more arrays if must

Last edited by Bacardi; 10-19-2018 at 14:51.
Bacardi is offline
shanapu
Veteran Member
Join Date: Apr 2015
Location: .de
Old 10-19-2018 , 16:46   Re: Store 3 values
Reply With Quote #18

Quote:
Originally Posted by asherkin View Post
Enumstructs are discouraged and are not available in modern syntax.
boom, that's a punch in the stomach. I had just fallen in love with enums.
What are the reasons you discourage from enums except for the modern syntax part? Is there any performance or 'technical' issues?
Would you recommend to rewrite plugins with intense use of complex enumstructs?

Quote:
Originally Posted by hmmmmm View Post
You could go with a StringMap to store all the elements you want and even wrap it in a sexy MethodMap. Here's an example I stole from Headline:
Spoiler
I used sourcemods methodmap since introduction on may parts, but never wrote my own mm before.
It needed some time to get into mm, but now I understand why you call them sexy and why there was such a "hype" with the introduction.
Seems I found a new love.

But there is an issue with your (& headlines) example or do I still not understand the whole thing?
If I'm correct this part(s) won't work:
PHP Code:
        public get()
        {
            return 
view_as<int>(this.GetValue("data"));
        } 
even with view_as<int> GetValue returns a bool. And it has 2 parameter, key & value.
So this part should be:
PHP Code:
        public get()
        {
            
int val;
            
this.GetValue("data"val);
            return 
val;
        } 
complete:
PHP Code:
methodmap PlayerData StringMap
{
    public 
PlayerData(int dataint data2float data3)
    {
        
StringMap strMap = new StringMap();

        
strMap.SetValue("data"data);
        
strMap.SetValue("data2"data2);
        
strMap.SetValue("data3"data3);

        return 
view_as<PlayerData>(strMap);
    }

    
property int Data
    
{
        public 
get()
        {
            
int val;
            
this.GetValue("data"val);
            return 
val;
        }

        public 
set(int val)
        {
            
this.SetValue("data"val);
        }
    }

    
property int Data2
    
{
        public 
get()
        {
            
int val;
            
this.GetValue("data2"val);
            return 
val;
        }

        public 
set(int val)
        {
            
this.SetValue("data2"val);
        }
    }

    
property float Data3
    
{
        public 
get()
        {
            
float val;
            
this.GetValue("data3"val);
            return 
val;
        }

        public 
set(float val)
        {
            
this.SetValue("data3"val);
        }
    }

right?!?
__________________
coding & free software
shanapu is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 10-19-2018 , 17:42   Re: Store 3 values
Reply With Quote #19

Using Dynamic's class builder....

https://console.aus-tg.com/index.php...tedynamicclass

Code:
#if defined _dynamic_class_myclass_
  #endinput
#endif
#define _dynamic_class_myclass_

methodmap MyClass < Dynamic
{
	public MyClass()
	{
		// First we make a new dymanic object
		Dynamic myclass = Dynamic(64, 0);

		// Next we will define all the members
		// -> We do this to force the offsets to always be in the same location
		//    over multiple instances of the same class.
		myclass.SetInt("Client", 0);
		myclass.SetBool("BoolValue", false);
		myclass.SetString("StringValue", "", 64);
		return view_as<MyClass>(myclass);
	}

	// Note that I use static offsets to access members.
	// -> This improves performance by caching member offsets
	// -> This is why we force the members in during the contructor
	// -> Failure to force members in the constructor will cause corruption

	property int Client
	{
		public get()
		{
			static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.GetMemberOffset("Client");
				if (offset == INVALID_DYNAMIC_OFFSET)
					SetFailState("A serious error occured in Dynamic methodmap @ MyClass.Client!");
			}
			return this.GetIntByOffset(offset);
		}
		public set(int value)
		{
			static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.GetMemberOffset("Client");
				if (offset == INVALID_DYNAMIC_OFFSET)
				{
					offset = this.SetInt("Client", value);
					return;
				}
			}
			this.SetIntByOffset(offset, value);
		}
	}

	property bool BoolValue
	{
		public get()
		{
			static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.GetMemberOffset("BoolValue");
				if (offset == INVALID_DYNAMIC_OFFSET)
					SetFailState("A serious error occured in Dynamic methodmap @ MyClass.BoolValue!");
			}
			return this.GetBoolByOffset(offset);
		}
		public set(bool value)
		{
			static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.GetMemberOffset("BoolValue");
				if (offset == INVALID_DYNAMIC_OFFSET)
				{
					offset = this.SetBool("BoolValue", value);
					return;
				}
			}
			this.SetBoolByOffset(offset, value);
		}
	}

	public bool GetStringValue(char[] buffer, int length)
	{
		static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
		if (offset == INVALID_DYNAMIC_OFFSET)
		{
			offset = this.GetMemberOffset("StringValue");
			if (offset == INVALID_DYNAMIC_OFFSET)
				SetFailState("A serious error occured in Dynamic methodmap @ MyClass.StringValue!");
		}
		this.GetStringByOffset(offset, buffer, length);
		return true;
	}

	public void SetStringValue(const char[] buffer)
	{
		static DynamicOffset offset = INVALID_DYNAMIC_OFFSET;
		if (offset == INVALID_DYNAMIC_OFFSET)
		{
			offset = this.GetMemberOffset("StringValue");
			if (offset == INVALID_DYNAMIC_OFFSET)
			{
				offset = this.SetString("StringValue", buffer);
				return;
			}
		}
		this.SetStringByOffset(offset, buffer);
	}
}
__________________
Neuro Toxin is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 10-19-2018 , 19:07   Re: Store 3 values
Reply With Quote #20

Quote:
Originally Posted by shanapu View Post
right?!?
You are right, thanks for correcting the mistake.
hmmmmm 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 00:00.


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