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

Using CreateDataTimer


Post New Thread Reply   
 
Thread Tools Display Modes
Halt
Senior Member
Join Date: Jan 2015
Location: Black Mesa
Old 05-24-2017 , 22:45   Re: Using CreateDataTimer
Reply With Quote #21

Quote:
Originally Posted by WildCard65 View Post
Yeap, definitely your problem, I repeat, CreateDatatTimer CREATES A DataPack HANDLE FOR YOU TO FILL!

example:
PHP Code:
DataPack myPack;
CreateDataTimer(5.0MyTimermyPack);
myPack.WriteFloat(5.0); // Write a float to the datapack the timer WILL receive. 
I'm sorry but I'm having a hard time understanding. If my timer can only have one datapack argument then how do I pass another datapack to it?

Code:
 public Action:ArtilleryExplosionTimer(Handle:timer, Handle:datapack)
I tried
Code:
 public Action:ArtilleryExplosionTimer(Handle:timer, Handle:datapack, Handle:datapack2)
but only got a compiler error. And if I only call the command once then it gets that one value stores it in the datapack and sends it to the timer. Therefore I cannot add more data unless I run the command again.

Last edited by Halt; 05-24-2017 at 22:46.
Halt is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 05-25-2017 , 00:34   Re: Using CreateDataTimer
Reply With Quote #22

Why would you need 2 datapacks? Whole point of a datapack is to send in a pack of data (ie. The three floats representing position vector)

You got a perfectly good example from Squawong earlier, just do that twice for 2 timers

EDIT: Also don't comment out ResetPack, need that to reset position of datapack. Doesn't actually clear the data in it by default, bit confusingly named.

Last edited by hmmmmm; 05-25-2017 at 00:38.
hmmmmm is offline
Chaosxk
Veteran Member
Join Date: Aug 2010
Location: Westeros
Old 05-25-2017 , 00:56   Re: Using CreateDataTimer
Reply With Quote #23

CreateDataTimer is a wrapper for CreateTimer, perhaps understanding what it does might help you.

Code:
/**
 * Creates a timer associated with a new datapack, and returns the datapack.
 * @note The datapack is automatically freed when the timer ends.
 * @note The position of the datapack is not reset or changed for the timer function.
 *
 * @param interval			Interval from the current game time to execute the given function.
 * @param func				Function to execute once the given interval has elapsed.
 * @param datapack			The newly created datapack is passed though this by-reference 
 *							parameter to the timer callback function.
 * @param flags				Timer flags.
 * @return					Handle to the timer object.  You do not need to call CloseHandle().
 */
stock Handle CreateDataTimer(float interval, Timer func, Handle &datapack, int flags=0)
{
	datapack = new DataPack();
	flags |= TIMER_DATA_HNDL_CLOSE;
	return CreateTimer(interval, func, datapack, flags);
}
__________________
Chaosxk is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 05-25-2017 , 01:13   Re: Using CreateDataTimer
Reply With Quote #24

We can't really be more clear on how to use CreateDataTimer
PHP Code:
public void OnPluginStart()
{
    
/* Create some data for example that we want to pack */
    
float vec[3];
    
    
/* Create a variable that holds the datapack */
    
DataPack pack;
    
    
/* Create the datapack timer and give it the variable reference
     * so it can create the data pack for us */
    
CreateDataTimer(1.0Timer_CallbackpackTIMER_FLAG_NO_MAPCHANGE);
    
    
/* Write our data to the datapack */
    
pack.WriteFloat(vec[0]);
    
pack.WriteFloat(vec[1]);
    
pack.WriteFloat(vec[2]);
    
/* Reset pack position */
    
pack.Reset();
}

public 
Action Timer_Callback(Handle timerDataPack pack)
{
    
/* Decare var to store pack info */
    
float vec[3];
    
    
/* Read information from the pac */
    
vec[0] = pack.ReadFloat();
    
vec[1] = pack.ReadFloat();
    
vec[2] = pack.ReadFloat();


Last edited by headline; 05-25-2017 at 13:46.
headline is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 05-25-2017 , 08:11   Re: Using CreateDataTimer
Reply With Quote #25

Quote:
Originally Posted by Headline View Post
We can't really be more clear on how to use CreateDataTimer
PHP Code:
public void OnPluginStart()
{
    
/* Create some data for example that we want to pack */
    
float vec[3];
    
    
/* Create a variable that holds the datapack */
    
DataPack pack;
    
    
/* Create the datapack timer and give it the variable reference
     * so it can create the data pack for us */
    
CreateDataTimer(1.0Timer_CallbackpackTIMER_FLAG_NO_MAPCHANGE);
    
    
/* Write our data to the datapack */
    
pack.WriteFloat(vec[0]);
    
pack.WriteFloat(vec[1]);
    
pack.WriteFloat(vec[2]);
    
/* Reset pack position */
    
pack.Reset();
}

public 
Action Timer_Callback(Handle timerDataPack pack)
{
    
/* Decare var to store pack info */
    
float vec[3];
    
    
/* Read information from the pac */
    
vec[0] = pack.ReadFloat();
    
vec[1] = pack.ReadFloat();
    
vec[2] = pack.ReadFloat();
    
    
/* Free the memory */
    
delete pack;

Your example has a double handle free problem (CreateDataTimer tells CreateTimer that the timer itself is responsible for closing the handle upon timer's end)
__________________
WildCard65 is offline
Halt
Senior Member
Join Date: Jan 2015
Location: Black Mesa
Old 05-25-2017 , 12:23   Re: Using CreateDataTimer
Reply With Quote #26

Quote:
Originally Posted by hmmmmm View Post
Why would you need 2 datapacks? Whole point of a datapack is to send in a pack of data (ie. The three floats representing position vector)

You got a perfectly good example from Squawong earlier, just do that twice for 2 timers

EDIT: Also don't comment out ResetPack, need that to reset position of datapack. Doesn't actually clear the data in it by default, bit confusingly named.

But the CreateDataTimer only executes once when I use the command that calls it. I need it to execute 6 times, with 6 DIFFERENT vectors. So what you're telling me is just create 6 timers that all look identical minus the name? Wouldn't that be inefficient?

Last edited by Halt; 05-25-2017 at 12:24.
Halt is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 05-25-2017 , 13:47   Re: Using CreateDataTimer
Reply With Quote #27

Quote:
Originally Posted by WildCard65 View Post
Your example has a double handle free problem (CreateDataTimer tells CreateTimer that the timer itself is responsible for closing the handle upon timer's end)
Edited ty
headline is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 05-25-2017 , 16:46   Re: Using CreateDataTimer
Reply With Quote #28

Quote:
Originally Posted by Halt View Post
But the CreateDataTimer only executes once when I use the command that calls it. I need it to execute 6 times, with 6 DIFFERENT vectors. So what you're telling me is just create 6 timers that all look identical minus the name? Wouldn't that be inefficient?
Run this example on your server:
PHP Code:
public void OnPluginStart()
{
    
DataPack data;
    
CreateDataTimer(5.0Print_Datadata);
    
data.WriteFloat(5.0);
    
data.WriteCell(1);
    
data.WriteString("HELLO WORLD!");

    
CreateDataTimer(2.0Print_Datadata);
    
data.WriteFloat(55.0);
    
data.WriteCell(17);
    
data.WriteString("HELLO XD WORLD!");
    
data.Reset();

    
CreateDataTimer(3.0Print_Datadata);
    
data.WriteFloat(34567.0);
    
data.WriteCell(1678);
    
data.WriteString("ANOTHER RANDOM STRING!");
    
data.Reset();

    
CreateDataTimer(8.0Print_Datadata);
    
data.WriteFloat(678.0);
    
data.WriteCell(65532);
    
data.WriteString("FINALLY!");
    
data.Reset();
}

public 
Action Print_Data(Handle timerHandle data)
{
    
DataPack datapack view_as<DataPack>(data);

    
float fl datapack.ReadFloat();
    
int i datapack.ReadCell();
    
char str[32];
    
data.ReadString(strsizeof(str));

    
PrintToServer("Data: %f %d %s"flistr);

    return 
Plugin_Continue;

Output:
PHP Code:
// Data: 55.00000 17 HELLO XD WORLD!
// Data: 34567.00000 1678 ANOTHER RANDOM STRING!
// Data: 5.00000 1 HELLO WORLD!
// Data: 678.00000 65532 FINALLY! 
__________________

Last edited by WildCard65; 05-25-2017 at 16:48.
WildCard65 is offline
Halt
Senior Member
Join Date: Jan 2015
Location: Black Mesa
Old 05-26-2017 , 00:15   Re: Using CreateDataTimer
Reply With Quote #29

Spoiler


That example helped immensely. Reading that and the Sourcemod Wiki helped me get a better grasp on the subject. And my mod compiles clean now and the timer fires correctly. But my entity doesn't explode (or teleport). I suspect its because I'm setting the float vectors incorrectly?

Code:
	float ImpactVector[3];
	Handle datapack;
	GetAimPos(Client, ImpactVector);
	WritePackFloat(datapack, ImpactVector[0]);
	WritePackFloat(datapack, ImpactVector[1]);
	WritePackFloat(datapack, ImpactVector[2]);


//Later in the plugin
	float _ImpactVector[3];
	_ImpactVector[0] = ReadPackFloat(datapack);
	_ImpactVector[1] = ReadPackFloat(datapack);
	_ImpactVector[2] = ReadPackFloat(datapack);
Halt is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 05-26-2017 , 00:21   Re: Using CreateDataTimer
Reply With Quote #30

Quote:
Originally Posted by Halt View Post

That example helped immensely. Reading that and the Sourcemod Wiki helped me get a better grasp on the subject. And my mod compiles clean now and the timer fires correctly. But my entity doesn't explode (or teleport). I suspect its because I'm setting the float vectors incorrectly?

Code:
	float ImpactVector[3];
	Handle datapack;
	GetAimPos(Client, ImpactVector);
	WritePackFloat(datapack, ImpactVector[0]);
	WritePackFloat(datapack, ImpactVector[1]);
	WritePackFloat(datapack, ImpactVector[2]);


//Later in the plugin
	float _ImpactVector[3];
	_ImpactVector[0] = ReadPackFloat(datapack);
	_ImpactVector[1] = ReadPackFloat(datapack);
	_ImpactVector[2] = ReadPackFloat(datapack);
you have to show us the code where your entity is suppose to explode then
8guawong 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 19:45.


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