Help with Generator Function and Delay

Started by reaper, Feb 28, 2015, 09:51 AM

Previous topic - Next topic

reaper

Help me to figure out how to generate a number or a vector every few seconds.  For example

1
// 2 sec delay
2
// 2 sec delay
3
// 2 sec delay

or

Vector( x, y, z) from database row 1
// 2 sec delay
Vector( x, y, z) from row 2
// 2 sec delay
Vector( x, y, z) from row 3
// so on


And how to create a delay function where after the time given by user the rest of the lines should execute. For Example

delay(time)
{

}

player.Health = 100;
delay(1000) // it should wait for 1 sec and then execute the next lines
print( Healed );
...

I am just learning from other scripts and snippets so please make sure to give me some example so that i can understand properly.

reaper

For now i am doing it this way which is not a generator.

sql <- ConnectSQL( "scripts/db.sqlite" );
sno <- array ( 5 , 0 );


if(cmd == "move")
{
NewTimer("move", 100, 200);
}

function move()
{

QuerySQL( sql, "BEGIN TRANSACTION" );
 
  sno[1] ++;
 
      local x = QuerySQL( sql, "SELECT Xaxis FROM  Path WHERE sno = " + sno[1] );
      local xa = GetSQLColumnData( x, 0 );   
local xaxi = xa;
      FreeSQLQuery( x );
 
  local y = QuerySQL( sql, "SELECT Yaxis FROM  Path WHERE sno = " + sno[1] );
      local ya = GetSQLColumnData( y, 0 );
local yaxi = ya;
      FreeSQLQuery( y );
 
  local z = QuerySQL( sql, "SELECT Zaxis FROM  Path WHERE sno = " + sno[1] );
      local za = GetSQLColumnData( z, 0 );
local zaxi = za;
      FreeSQLQuery( z );
 
  QuerySQL( sql, "END TRANSACTION" );
   
  FindVehicle( 114 ).Pos = Vector( xaxi, yaxi, zaxi );
  FindVehicle( 114 ).EulerRotation = Vector( xang, yang, zang );
}

What i am trying to accomplish is something like this

function move()
{

QuerySQL( sql, "BEGIN TRANSACTION" );
 
  sno[1] ++;
 
      local x = QuerySQL( sql, "SELECT Xaxis FROM  Path WHERE sno = " + sno[1] );
      local xa = GetSQLColumnData( x, 0 );   
local xaxi = xa;
      FreeSQLQuery( x );
 
  local y = QuerySQL( sql, "SELECT Yaxis FROM  Path WHERE sno = " + sno[1] );
      local ya = GetSQLColumnData( y, 0 );
local yaxi = ya;
      FreeSQLQuery( y );
 
  local z = QuerySQL( sql, "SELECT Zaxis FROM  Path WHERE sno = " + sno[1] );
      local za = GetSQLColumnData( z, 0 );
local zaxi = za;
      FreeSQLQuery( z );
 
  QuerySQL( sql, "END TRANSACTION" );
     
// i want to generate a vector after a delay for n number of times. even with a for loop i need a delay and how am i supposed to create one?
}

if(cmd == "move")
{
FindVehicle( 114 ).Pos = /* Here i want to call the generated vector */;
}

So can anybody guide me out how to accomplish this??

Kratos_

#2
Quote from: reaper on Feb 28, 2015, 09:51 AMHelp me to figure out how to generate a number or a vector every few seconds.  For example
1
// 2 sec delay
2
// 2 sec delay
3
// 2 sec delay


This is simple stuff. Do this way :-

function count_down_1( player ) MessagePlayer("3", player);
function count_down_2( player ) MessagePlayer("2", player);
function count_down_3( player ) MessagePlayer("1", player);
function count_down_final( player ) MessagePlayer(" GO GO GO ", player);

NewTimer("count_down_1", 1000, 1, player);   // Will execute after 1 sec of count_down startup
NewTimer("count_down_2", 2000, 1, player);   // 2 sec after the reference instant




Quote from: reaper on Feb 28, 2015, 09:51 AMVector( x, y, z) from database row 1
// 2 sec delay
Vector( x, y, z) from row 2
// 2 sec delay
Vector( x, y, z) from row 3
// so on

Hmm... Dealing with SQL queries & that too with a timer of short iterating index of 2 seconds ?  This will lag definitely .  Anyway , you can create an empty array first and then push your vectors into it & then take those array indices . Check out this :-

checkpoint_handler <- [];                 // Creating an empty global array without family

function checkpoint_store()
{
local query_handler = QuerySQL( database_handler, "SELECT Xaxis, Yaxis, Zaxis FROM Path" );
while( GetSQLColumnData( query_handler, 0 ) )             // Till the end of the table
{
local x = GetSQLColumnData( query_handler, 0 ), y = GetSQLColumnData( query_handler, 1 ), z = GetSQLColumnData( query_handler, 2 );
checkpoint_handler.push( x );  // 0, 3, 6, 9,    Memory locations of the co-ordinates in the array where they are pushed
checkpoint_handler.push( y );  // 1, 4, 7, 10,
checkpoint_handler.push( z );  // 2, 5, 8, 11. etc
GetSQLNextRow( query_handler );                     
}
FreeSQLQuery( query_handler );           // Freeing up the SQL query
}


Now, you can make an easy algorithm for getting those array indices in the form (x, y, z) = ( n, n+1, n+2 )  .  Where , n = multiple of 3 but less than the size of the array after complete parsing of SQL Table .  Create a timer for doing it after an interval of 2 secs .




Quote from: reaper on Feb 28, 2015, 09:51 AMAnd how to create a delay function where after the time given by user the rest of the lines should execute. For Example
delay(time)
{
}
player.Health = 100;
delay(1000) // it should wait for 1 sec and then execute the next lines
print( Healed );
...
I am just learning from other scripts and snippets so please make sure to give me some example so that i can understand properly.



I couldn't get it . If player will be given the chance to choose the delay time of heal process then who want to heal himself in 100 seconds ??  Everyone will start giving the value to 1 mili-second for the iteration index of timer .  Although , its possible following this syntax  :-

NewTimer("function_need_to_be_executed", text, 1);   // text should be an integer & in milliseconds.
In the middle of chaos , lies opportunity.

reaper

Thanks for the reply Kratos_

Quote from: Kratos_ on Feb 28, 2015, 01:42 PMThis is simple stuff. Do this way :-

function count_down_1( player ) MessagePlayer("3", player);
function count_down_2( player ) MessagePlayer("2", player);
function count_down_3( player ) MessagePlayer("1", player);
function count_down_final( player ) MessagePlayer(" GO GO GO ", player);

NewTimer("count_down_1", 1000, 1, player);   // Will execute after 1 sec of count_down startup
NewTimer("count_down_2", 2000, 1, player);   // 2 sec after the reference instant

You got it wrong pal. What i was trying to say was that i dont want anything to happen for that time period. Like pausing at line 10 for a while say ( 5 sec ) and then resume from line 11. More precisely i need something like On delay Timer i.e After the given time is elapsed it should trigger ON.

Quote from: Kratos_ on Feb 28, 2015, 01:42 PMHmm... Dealing with SQL queries & that too with a timer of short iterating index of 2 seconds ?  This will lag definitely .  Anyway , you can create an empty array first and then push your vectors into it & then take those array indices . Check out this :-

checkpoint_handler <- [];                 // Creating an empty global array without family

function checkpoint_store()
{
local query_handler = QuerySQL( database_handler, "SELECT Xaxis, Yaxis, Zaxis FROM Path" );
while( GetSQLColumnData( query_handler, 0 ) )             // Till the end of the table
{
local x = GetSQLColumnData( query_handler, 0 ), y = GetSQLColumnData( query_handler, 1 ), z = GetSQLColumnData( query_handler, 2 );
checkpoint_handler.push( x );  // 0, 3, 6, 9,    Memory locations of the co-ordinates in the array where they are pushed
checkpoint_handler.push( y );  // 1, 4, 7, 10,
checkpoint_handler.push( z );  // 2, 5, 8, 11. etc
GetSQLNextRow( query_handler );                     
}
FreeSQLQuery( query_handler );           // Freeing up the SQL query
}


Now, you can make an easy algorithm for getting those array indices in the form (x, y, z) = ( n, n+1, n+2 )  .  Where , n = multiple of 3 but less than the size of the array after complete parsing of SQL Table .  Create a timer for doing it after an interval of 2 secs .
Thanks for making things clear here. This changed the way i was looking at it. I thought that i cant get to the next row so came up with that serial no thing ;D

Quote from: Kratos_ on Feb 28, 2015, 01:42 PMI couldn't get it . If player will be given the chance to choose the delay time of heal process then who want to heal himself in 100 seconds ??  Everyone will start giving the value to 1 mili-second for the iteration index of timer .  Although , its possible following this syntax  :-
NewTimer("function_need_to_be_executed", text, 1);   // text should be an integer & in milliseconds.

Sorry this was again about the delay function and i wasnt talking about healing at all, that was an example.

reaper

I have finally figured out a way to solve this problem

time <- 0;
DelayElapsed <- "False";
function delay( time )
{
local checkpoint = GetTickCount();
local timetaken = checkpoint + time;
while( GetTickCount() <= timetaken ) {}
DelayElapsed = "True";
}

Usage:
delay(2000);
if(DelayElapsed == "True")
{
// Next Lines........
}

Example:
if(cmd == "heal")
{
MessagePlayer("Healing ",player);
delay(5000)
if(DelayElapsed == "True")
{
player.Health=100.0;
MessagePlayer("You have Healed successfully ",player);
}
}

If there is a better way of implementing this, then please leave your suggestions here.

.

#5
Quote from: reaper on Mar 01, 2015, 09:35 AMIf there is a better way of implementing this, then please leave your suggestions here.

I see that you're looking for a healing function. There's a better one here. And it also uses a generator combined with a timer to delay and resume a function. Maybe you can find more information there.

EDIT: A co-routine not a generator :D My mistake :D
.

reaper

Quote from: S.L.C on Mar 01, 2015, 09:48 AMI see that you're looking for a healing function.
No i am not looking for a healing function, that was just an example for the delay function which i created.  I ll look into what you have suggested S.L.C.

Kratos_

#7
I made an attempt to compare your function with the NewTimer & here are the Run-Time results  :-

Timer_Set = 6000

6008  - NewTimer
6001  - Your Function

Your function is a bit faster than NewTimer . :)  Just one thing though . You used the condition with the bool variable DelayElapsed .
Technically , the while loop will enforce that program section to execute repeatatively until that specified time doesn't gets passed .
So , that condition DelayElapsed == "true" is not required . Your code will execute without that condition too . Except this , this is fine by my side .
In the middle of chaos , lies opportunity.