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.


All times are GMT +2. The time now is 09:48 PM.

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