Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Sleep() drops scope! (https://forums.graalonline.com/forums/showthread.php?t=87354)

Tigairius 08-10-2009 12:04 AM

Sleep() drops scope!
 
I know this has been reported before, but I couldn't find it, so I'm going to report it here again. It's definitely something to fix in the NPC-Server.

I have a level NPC that joins to a class, inside of the class I have:
PHP Code:

function onPlayerEnters() {
  
testFunc();
}

function 
testFunc() {
  for (
temp.i=1;temp.i<4;temp.i++){
    
sendrpgmessage(temp.i);
    
sleep(1);
  }


It sends the RPG message to me as:
HTML Code:

1
If I remove the sleep from the for loop, it will return:
HTML Code:

1
2
3

Which is correct.

Now, if I do:
PHP Code:

function onPlayerEnters() {
  
testFunc();
}

function 
testFunc() {
  for (
temp.i=1;temp.i<4;temp.i++){
    
findplayer("Tigairius").sendrpgmessage(temp.i);
    
sleep(1);
  }


It returns:
HTML Code:

1
2
3

No problem! (Thanks Ziro for helping me figure that out.) Sleep is causing the function to lose scope of the player. I spent hours trying to figure this out. :cry: Time I could have spent progressing further in the script!

It definitely needs to be fixed please!!!

cbk1994 08-10-2009 12:49 AM

Agreed.

Frankie 08-10-2009 12:59 AM

this used to piss me off all the time and I could never figure out how to fix it D:

Switch 08-10-2009 02:27 AM

Definitely needs to be fixed. I had a system that, since I had no clue about what I was doing wrong, had to not use a loop, which sucked because only a number was changing.
Also, maybe sleep could not be interrupted (or keep going) when using another function?

LoneAngelIbesu 08-10-2009 02:31 AM

The first example seems to work fine for me. I'm using a WNPC. Would that make a difference?

Inverness 08-10-2009 04:00 AM

Yea I noticed this a long time ago myself. And I don't remember reporting it either; my attitude towards the lack of updates is probably why.

xXziroXx 08-10-2009 10:42 AM

Quote:

Originally Posted by Tigairius (Post 1513962)
I spent hours trying to figure this out. :cry:

Should've come to me sooner! :p

Quote:

Originally Posted by LoneAngelIbesu (Post 1513984)
The first example seems to work fine for me. I'm using a WNPC. Would that make a difference?

I assume you were doing it on serverside, obviously it would never loose the player scope clientside.

Skyld 08-10-2009 12:35 PM

The loss of scope when using sleep has been a problem with the script engine since day one, I am not sure how easy it is to fix if it is some kind of delayed scheduling. Does it also occur while using waitfor?

cbk1994 08-10-2009 01:18 PM

Quote:

Originally Posted by Skyld (Post 1514016)
Does it also occur while using waitfor?

I'm fairly certain it does not.

FreezeBurnX 08-10-2009 01:23 PM

You could use scheduleEvent() alternatively, couldn't you?

cbk1994 08-10-2009 01:32 PM

Quote:

Originally Posted by FreezeBurnX (Post 1514022)
You could use scheduleEvent() alternatively, couldn't you?

Yes, but scheduleevent is inconvenient because it is harder to keep track of loops.

FreezeBurnX 08-10-2009 01:47 PM

Maybe there's a way to store the reference to the player? I don't know exactly how it is done on graal, but something amongst the lines...
PHP Code:

function onPlayerEnters(eventPlayer) {
  
testFunc(eventPlayer);
}

function 
testFunc(player) {
  for (
temp.i=1;temp.i<4;temp.i++){
    
player.sendrpgmessage(temp.i);
    
sleep(1);
  }



Admins 08-10-2009 03:20 PM

Yes you can also do temp.pl = player and then use temp.pl.
Currently triggered events are keeping care of the player scope (scheduleevent, trigger, etc.) but not functions that suspend the script executing (sleep, waitfor). This has always been like that, but might be good to fix it someday.

TESTRETIS 08-10-2009 05:25 PM

I almost find this scary. Sure, there are workarounds, but it's a bit frightening how it loses scope like that.

Tigairius 08-10-2009 06:03 PM

Quote:

Originally Posted by FreezeBurnX (Post 1514028)
Maybe there's a way to store the reference to the player? I don't know exactly how it is done on graal, but something amongst the lines...
PHP Code:

function onPlayerEnters(eventPlayer) {
  
testFunc(eventPlayer);
}

function 
testFunc(player) {
  for (
temp.i=1;temp.i<4;temp.i++){
    
player.sendrpgmessage(temp.i);
    
sleep(1);
  }



Yes, that's basically what I did, but it can cause a lot of problems for people who aren't aware of this problem.

WhiteDragon 08-11-2009 02:52 AM

Well, at least this way people will be dissuaded from using sleep, which increases the chance of weird and even more confusing things happening to scripters, like race conditions.

That said, a bug should never be a feature, so fix it please. :p

DustyPorViva 08-11-2009 02:54 AM

On a slightly somewhat perhaps related note, I have often had problems where temp.vars lose their value in a for loop.

cbk1994 08-11-2009 02:56 AM

Quote:

Originally Posted by DustyPorViva (Post 1514098)
On a slightly somewhat perhaps related note, I have often had problems where temp.vars lose their value in a for loop.

Are you declaring them inside the loop?

PHP Code:

temp.0;

for (
temp.item : array) {
  echo(
b);
  
++;


has always worked for me

WhiteDragon 08-11-2009 02:58 AM

Quote:

Originally Posted by DustyPorViva (Post 1514098)
On a slightly somewhat perhaps related note, I have often had problems where temp.vars lose their value in a for loop.

Is the script getting suspended in the middle (i.e., sleep, waitfor), or is it just a straight-up for loop?

I have literally no technical data on how GScript works, such as threading, etc. or how scripts execute based on what they are waiting for (i.e., do all of them lock if one is waiting on I/O), so I can only speculate.

Even if you aren't suspending the script, it's possible that another script runs and pushes some variables out of the memory, or perhaps the garbage collector is going haywire for some reason.

DustyPorViva 08-11-2009 02:59 AM

It's not exactly that simple of a scenario. I can't do so right now, but I will pull up an example of a script where I have had the problem.

DustyPorViva 08-11-2009 04:06 AM

Here is a script I've had the problem with... it may have to do with function temp vars or something:
PHP Code:

function HitWall(k,speed,wall) {
  
temp.foo k;
  for (
temp.i=0;i<speed;i+=1/16) {
    if (
onwall2(check[k][0]+vecx(k)*i,check[k][1]+vecy(k)*i,check[k][2],check[k][3])) {
      if (
k in {0,2}) {
        
player.+= vecy(k)*i;
        
player.int(player.y+.5);
      } else {
        
player.chat k;
        
player.+= vecx(k)*i;
        
player.int(player.x)+.5;
      }
      break;
    }
  }


var foo handles the value proper, var k does not. Player chat will return 0.

I've had the problem more than once, as well.

devilsknite1 08-03-2012 12:03 AM

Reviving this thread after a good 3 years in hopes of reminding Stefan that this is still an issue. >.>

Pandar 08-03-2012 12:42 AM

Quote:

Originally Posted by devilsknite1 (Post 1700777)
Reviving this thread after a good 3 years in hopes of reminding Stefan that this is still an issue. >.>

This, this, forever more, holy ****, it's one of the most ANNOYING things in GS2.

Emera 08-03-2012 12:48 PM

I've had it a couple of times. I agree it ****ing annoying.

Emera 11-29-2013 08:10 PM

Bump. Are you every going to get around to fixing this Stefan? It's doing my head in.

fowlplay4 12-14-2013 11:59 PM

Quote:

Originally Posted by Emera (Post 1724056)
Bump. Are you every going to get around to fixing this Stefan? It's doing my head in.

Store the player object before calling sleep.

I.e.

PHP Code:

temp.pl player;
sleep(5);
echo(
temp.pl.account); 

This same issue occurs when loading TServerPlayer objects as well.


All times are GMT +2. The time now is 10:13 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.