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

Passing multiple data with RequestFrame


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 10-02-2017 , 09:55   Passing multiple data with RequestFrame
Reply With Quote #1

Hi,
I need to pass multiple data with RequestFrame.
Like this:
PHP Code:
RequestFrame(MyFunctionclientweapon);

public 
void MyFunction(int clientint weapon) {
   
PrintToChat(client"Weapon index: %d"weapon);

But it doesn't seem like it's possible to pass 2 different data values through. Any ideas on how I can work around this?
__________________
condolent is offline
sdz
Senior Member
Join Date: Feb 2012
Old 10-02-2017 , 10:11   Re: Passing multiple data with RequestFrame
Reply With Quote #2

use a DataPack

the reason this works is because the prototype calls for function, and an argument of type "any"
i liken this down to a typeless, welfare style of delegates

Example for your situation:
PHP Code:
void SetupData()
{
    
DataPack pack = new DataPack();
    
pack.WriteCell(client);
    
pack.WriteCell(EntIndexToEntRef(weapon));
    
pack.Reset();
    
RequestFrame(MyFunctionpack);
}

public 
void MyFunction(any thing
{
    
DataPack data view_as<DataPack>(thing);
    
int client data.ReadCell();
    
int weapon EntRefToEntIndex(data.ReadCell());
    
PrintToChat(client"Weapon index: %d"weapon);


Last edited by sdz; 10-02-2017 at 10:17.
sdz is offline
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 10-02-2017 , 10:24   Re: Passing multiple data with RequestFrame
Reply With Quote #3

Quote:
Originally Posted by EasSidezz View Post
use a DataPack

the reason this works is because the prototype calls for function, and an argument of type "any"
i liken this down to a typeless, welfare style of delegates
I've never used datapacks before and I find the wiki article quite confusing.
How can I write an int into a specific cell in a datapack and then retreive that one?

Not sure if my question is understandable, I'm kind of bad at explaining stuff..


Your updated post is explaining this pretty well!

So if I understand this correctly, using ReadCell(), it retreives the cells in the same order as they were written?
__________________

Last edited by condolent; 10-02-2017 at 10:27.
condolent is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 10-02-2017 , 15:08   Re: Passing multiple data with RequestFrame
Reply With Quote #4

@EasSidezz post #2
Now in your example, it create everytime new Datapack and not close old ones.
There would be open Handles (sm_dump_handles myfile.txt)

*edit
But I learn also something new
__________________
Do not Private Message @me

Last edited by Bacardi; 10-02-2017 at 15:09.
Bacardi is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 10-02-2017 , 20:03   Re: Passing multiple data with RequestFrame
Reply With Quote #5

The DataPack has a position property (https://sm.alliedmods.net/new-api/da...aPack/Position) that decides where it is exactly. When you read/write something to it, the position advances.

Usually it's used by writing what you need in one function, passing it into the callback and then doing DataPack.Reset() which sets that position back to 0. The info is then read back in the order it was written.

As Bacardi said, since a DataPack is a handle you need to close it once you're done using it. i.e. after you've read the data out of it.
Can be done by doing 'delete datapack;'

Another thing I'd do differently is tag the callback function parameter as a DataPack so you don't have to do that view_as<DataPack> thing.
So 'MyFunction(DataPack thing)' instead of 'MyFunction(any thing)'

Last edited by hmmmmm; 10-02-2017 at 20:13.
hmmmmm is offline
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 10-03-2017 , 03:31   Re: Passing multiple data with RequestFrame
Reply With Quote #6

Quote:
Originally Posted by hmmmmm View Post
Another thing I'd do differently is tag the callback function parameter as a DataPack so you don't have to do that view_as<DataPack> thing.
So 'MyFunction(DataPack thing)' instead of 'MyFunction(any thing)'
Yeah, that's what I actually did in the end. This is the code I ended up with, which works for my needs!

PHP Code:
public void SomeEvent(int clientint weapon) {
    
DataPack data = new DataPack();
    
    
data.WriteCell(client);
    
data.WriteCell(weapon);
    
data.Reset();
    
    
RequestFrame(MyFunctiondata);
}

public 
void MyFunction(DataPack data) { 
    
int client data.ReadCell();
    
int weapon data.ReadCell();
    
    
data.Close();
    
    
PrintToChat(client"Weapon index: %d"weapon); 

__________________
condolent is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 10-03-2017 , 12:04   Re: Passing multiple data with RequestFrame
Reply With Quote #7

Rule of thumb with handling anything considered "async" like RequestFrame (not enforced, but HIGHLY RECOMMENDED)

Never pass RAW entity/client indexes (except 0 or -1) as callback data, instead use either UserID/Client Serial (for client indexes) and entity references for entity indexes (0 or greater then MaxClients value)
__________________
WildCard65 is offline
Papero
Veteran Member
Join Date: Aug 2016
Location: Italy
Old 10-03-2017 , 14:47   Re: Passing multiple data with RequestFrame
Reply With Quote #8

If I want to use an ArrayList it would be the same? Worst performace or it's even?
__________________
My Plugins
SPCode


Steam: hexer504
Telegram: Hexah
Discord: Hexah#6903

If you like my work you can donate here!
Papero is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 10-03-2017 , 17:32   Re: Passing multiple data with RequestFrame
Reply With Quote #9

A datapack is the normal. You can parse any Handle you want at the end of the day.
__________________
Neuro Toxin is offline
Papero
Veteran Member
Join Date: Aug 2016
Location: Italy
Old 10-03-2017 , 17:41   Re: Passing multiple data with RequestFrame
Reply With Quote #10

Quote:
Originally Posted by Neuro Toxin View Post
A datapack is the normal. You can parse any Handle you want at the end of the day.
Sometimes ago I used ArrayList since data pack doesn't support arrays
__________________
My Plugins
SPCode


Steam: hexer504
Telegram: Hexah
Discord: Hexah#6903

If you like my work you can donate here!
Papero 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 11:47.


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