AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Passing multiple data with RequestFrame (https://forums.alliedmods.net/showthread.php?t=301718)

condolent 10-02-2017 09:55

Passing multiple data with RequestFrame
 
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?

sdz 10-02-2017 10:11

Re: Passing multiple data with RequestFrame
 
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);



condolent 10-02-2017 10:24

Re: Passing multiple data with RequestFrame
 
Quote:

Originally Posted by EasSidezz (Post 2552096)
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?

Bacardi 10-02-2017 15:08

Re: Passing multiple data with RequestFrame
 
@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 :D

hmmmmm 10-02-2017 20:03

Re: Passing multiple data with RequestFrame
 
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)'

condolent 10-03-2017 03:31

Re: Passing multiple data with RequestFrame
 
Quote:

Originally Posted by hmmmmm (Post 2552233)
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); 



WildCard65 10-03-2017 12:04

Re: Passing multiple data with RequestFrame
 
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)

Papero 10-03-2017 14:47

Re: Passing multiple data with RequestFrame
 
If I want to use an ArrayList it would be the same? Worst performace or it's even?

Neuro Toxin 10-03-2017 17:32

Re: Passing multiple data with RequestFrame
 
A datapack is the normal. You can parse any Handle you want at the end of the day.

Papero 10-03-2017 17:41

Re: Passing multiple data with RequestFrame
 
Quote:

Originally Posted by Neuro Toxin (Post 2552408)
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


All times are GMT -4. The time now is 20:38.

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