timer/hooktimer bug or user problem ?
Displaying 1-20 of 26 total.
12 next
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
ExSea

hey.. i m wondering if any1 can help me out here... few months ago i announced that i was gona do a game using verge. well somethings wrong with the timer function or at least thats what i think it is.

when i run the game i have a battle system. on random events of 'while (timer<somenum)' verge hangs. Its like the first time its ok but the second time it hangs, or it keeps hanging or it keeps working awhile then hangs again.. something like that. soo... i went back to basics to try out something. using this code :

int x;
int y;

void autoexec() {
x=0;
y=0;
while (!b1) {
render();
rectfill(0,0,400,300,RGB(0,0,0),screen);
PrintString(0, 0, screen, 0, 'Hello World!'+str(x)+' ' +str(y));
Showpage();
hookkey(SCAN_UP,'Boom');
hookkey(SCAN_DOWN,'KABOOM');
hookkey(SCAN_SPACE,'noboom');
if (x>300) {
noboom();
timer=0;
while (timer<=100){
x-=4;
}
}
}
}

void Boom() {
hooktimer('Boomer');
}

void KABOOM() {
hooktimer('Kaboomer');
}



void Boomer() {
x+=1;
}

void kaboomer() {
y+=1;
}

void noboom(){
hooktimer('');
}

end of code, i tested the timer stuffs.
my expected result is by the 4rth second that UP has been pressed the display would be 'HelloWorld!-100 0'
instead i got 'HelloWorld!-3204055 0' on separate events the x value would not be consistant with previous values (i reckon its coz i didnt bother putting unpress functions or something).... can any one explain whats going on? am i doing anything wrong?
thanks in advance

Posted on 2005-08-01 13:37:09

CrazyAznGamer

Quote:Originally posted by Znigma


my expected result is by the 4rth second that UP has been pressed the display would be 'HelloWorld!-100 0'
instead i got 'HelloWorld!-3204055 0' on separate events the x value would not be consistant with previous values (i reckon its coz i didnt bother putting unpress functions or something).... can any one explain whats going on? am i doing anything wrong?
thanks in advance

Hmm...

int x;
int y;

void autoexec() {
x=0;
y=0;
while (!b1) {
render();
rectfill(0,0,400,300,RGB(0,0,0),screen);
PrintString(0, 0, screen, 0, 'Hello World!'+str(x)+' ' +str(y));
Showpage(); //seems normal right here.
hookkey(SCAN_UP,'Boom'); //Hmm... I think it seems ok.
hookkey(SCAN_DOWN,'KABOOM');
hookkey(SCAN_SPACE,'noboom');
if (x>300) {
noboom();
timer=0;
while (timer<=100){ //Ahh, a problem here.
x-=4; //x decreases by for every RETRACE,
//not timer tick...
}
}
}
}

void Boom() {
hooktimer('Boomer');
}

void KABOOM() {
hooktimer('Kaboomer');
}



void Boomer() {
x+=1;
}

void kaboomer() {
y+=1;
}

void noboom(){
hooktimer('');
}

Laugh and point at me if I'm wrong, veterans.

Posted on 2005-08-01 15:35:45

mcgrue

That looks to be the error, indeed. If you want something to happen at a specific time rate regardless of computer speed, you need to make your operation dependant on the amount of time that has elapsed since you started.

I could give a clearer explaination with lots of examples, but I'm a zombie at the moment. Maybe someone else will be kind enough to give a rundown on making timed events. Otherwise, I'll explain sometime in the next 24 hours.

Posted on 2005-08-01 16:05:06

ExSea

Quote:Originally posted by mcgrue


I could give a clearer explaination with lots of examples, but I'm a zombie at the moment.


znigma casts remedy on mcgrue.
effect: fail.
while (mcgrue is still a zombie{
cast remedy on mcgrue;
}
effect:fail.
effect:fail.
effect:fail.
effect:fail.
effect:fail.
SystemMessage: 'Not enough Mana'

guess i ll wait around 24 hours.. thanks.

Posted on 2005-08-01 16:42:22

Overkill

What are you doing? Honestly ;__;
You appear to be repeatedly HookKey()'ing functions that in turn HookTimer() functions when they're pressed. All to increment x and y. That's just very, VERY wrong.

Posted on 2005-08-01 17:04:19

ExSea

erm.. i tried putting the hookkeys before the looping,
still the same effect...
anyway... syntaxically (is that even a real word?) speaking,isnt hooking within a loop usable? that in turn the hook timer called function would just be overwritten.

Posted on 2005-08-01 17:40:08

RageCage

when ever you hook something, it means that you dont have to worry about it. So hooking within a loop defeats the purpose of a loop. personally, I perfer using a switch statement and int lastpressed to do my input control.

ex:
switch(lastpressed){
case SCAN_UP: do this!
}
lastpressed=0;


As for your crazy loop... the logic seems all over the place. Maybe if I knew what your goal is here I could help you better?

Posted on 2005-08-01 20:47:45

RageCage

defeats the purpose of a hook... not a loop... damn you grue! fix the edit post bug!

Posted on 2005-08-01 20:49:01

ExSea

i modified the autoexec part but the results are still the same..

void autoexec() {
x=0;
y=0;
hookkey(SCAN_UP,'Boom');
hookkey(SCAN_DOWN,'KABOOM');
hookkey(SCAN_SPACE,'noboom');

while (!b1) {
render();
rectfill(0,0,400,300,RGB(0,0,0),screen);
PrintString(0, 0, screen, 0, 'Hello World!'+str(x)+' ' +str(y));
Showpage();

if (x>300) {
noboom();
timer=0;
while (timer<=100){
x-=4;
}
}
}
}

The purpose of this was just to test the timer syntax, when UP is pressed the value of x will keep increasing by 1 per tick using the hooktimer function. when the value of x reaches 301, the hooktimer function will be set to nothing, thus stopping x from incrementing. therefore x's last value should be 301. the TIMER is set to 0, a loop is made so that x's value will be decreased by 4 per tick for 101 ticks. 301-(101*4) = -103. once this calculation is done it should just keep displaying the text as usual.
but the weird thing is my final x result would be in more then 5 digits. i dont know where i went wrong.

the rest of the functions are just to toy around overiding the hooktimer function.

i did this test because within the verge game i created whenever i have a:

timer=0;
while (timer<=duration){
render();
Drawsomestuffs;
showpage(); }

type of statement, verge randomly hangs up. the background music continues playing. to find out whats wrong, i displayed the timer value somewhere. usually the error happens when the timer value is more then the duration -which is supposedto quit the loop.

Posted on 2005-08-01 21:15:56

ExSea


while (timer<=100){ //Ahh, a problem here.
x-=4; //x decreases by for every RETRACE,
//not timer tick...


every retrace? i dont quite get it...

anyway, i tought in programming the last loop has to be completed before any other loops so i dont understand why the value of x went up that much....

can any1 clarify?
thanks

Posted on 2005-08-01 21:28:13

mcgrue

Well, I'm refreshed now, and as I see nobody's really tried to illuminate you on the difference between how code executes and timer ticks, I'll see if I can make it clear for you.

As I see it, the problem lays entirely in this section of your code:


while( timer <= 100 )
{
x-=4;
}


Now, the verge3 timer variable increases by one every 1/100th of a second (by default). Now, just because the timer increases by 100 every second, doesn't meant a loop like that one will execute 100 times in a second. No, that loop will execute a lot more, mainly depending on your computer's speed.

<useless_history>
See, computers come in all speeds these days. There was a time, before IBM's PC model came on the scene, that when you were making a program for a home computer, you knew that every computer that was going to run your program was exactly the same. Commodore 64s and Apple IIs and Amigas and so on: computers you couldn't (realisitically) upgrade and that had all standard parts. The Ancient Demosceners took advantage of this and could write programs that did really funky things with the hardware, like making sound chips do extra graphics processing. As an aside to this aside, console hobbyist programmers are pretty much the only ones that develop for standardized equipment these days in the amatuer world.
</useless_history>

Anyways, those days are gone. All of our PCs are different speeds now. Overkill, for instance, runs a Pentium I 266mhz machine, while I run at 1.3ghz, and vecna has a dual core 10ghz god-box rapidly approaching sentience known as 'Hahn'. Each of these computers will run that loop straight trhough a different number of times. Here's a simple sample system.vc fo you to try:


void autoexec()
{
int i;

timer = 0;

while( timer <= 100 )
{
i++;
}

Exit( 'The loop is now over. It executed ' +str(i)+ ' times.' );
}


Mine executed 542551 times in that one second. You see, all the (timer <= 100) is doing is saying 'do this for one second', not 'do this 100 times', nor 'do this 100 times over one second'. To do something 100 times over a second, which is a very very useful thing to do, you want to do something more like this:


void autoexec()
{
int i;

int last_tick;

timer = 0;

while( timer <= 100 )
{
//this clause only will execute once every tick...
if( last_tick != timer )
{
last_tick = timer; //...because we're making it only able to execute once a tick inside of it.

i++;
}
}

Exit( 'The loop is now over. It executed ' +str(i)+ ' times.' );
}


This illustrates the different concept, but is not quite yet the full solution you want. For instance, when I run this, it tells me it only happened 94 times! What gives about that, eh? Well, 94 executions in a second is a lot closer to the 100 you wanted than 542551 is, but 'close enough' is a bad game to play in programming. There are fundamentally two things wrong with this:

  1. The loop steals all control from verge while it executes.

  2. The code you want to happen 100 times in that second isn;t really a function of time.



I'll explain these two points in a bit if nobody else does. Any questions so far?

Posted on 2005-08-02 03:29:24 (last edited on 2005-08-02 03:30:17)

ExSea

erm.... i learnt something new today and am thankful for that.

still one question left...
is there any way to really 'capture' the time?
as you said close enough is good...

anyway.... thanks to your in depth explaination alot of bugs are gonna experience genocide today.... damn bugs..>_<

Posted on 2005-08-02 06:18:46

RageCage

capture time? I'm not sure I know what you mean by that... but I'll try anyway.

but before I do that, I just want point out that when using timer, you should never set it to zero. Instead use timestamps to record your place in timer and take the difference to find out how much time it's been since. ex: if(timer-timestamp > 100)

This will be very important when you have overlapping functions using the same timer variable.

anyway... as for capturing time... if I'm right in understanding what you mean... you have to do it with algebra. I'll use a fade function as an example.


void fade(int color, int duration, int direction){
int scrn=newimage(windowGetXRes(screen), windowGetYRes(screen));
grabRegion(0, 0, windowGetXRes(screen), windowGetYRes(screen), 0, 0, screen, scrn);
pauseentities(1);
int timeStamp=timer;
switch(direction){
case FADE_OUT:
while(timer-timeStamp<=duration){
rectfill(0, 0, windowGetXRes(screen), windowGetYRes(screen), color, screen);
BlitLucent(0,0, (timer-timeStamp)*100/duration, scrn, screen);
showpage();
}
case FADE_IN:
while(timer-timeStamp<=duration){
rectfill(0, 0, windowGetXRes(screen), windowGetYRes(screen), color, screen);
BlitLucent(0,0, (duration-(timer-timestamp))*100/duration, scrn, screen);
showpage();
}
}
pauseentities(0);
freeimage(scrn);
}

I picked this one because it demonstrates two directions or using the equation.

the important part to look at is the while loop.
while(timer-timeStamp<=duration){ obviously ensures that the loops stops when teh fade stops.

Then within the blitlucent, we have (timer-timeStamp)*100/duration
The variable being manipulated here is the amount of lucency to blit with. The max amount for that variable is 100 and that is why we use *100. If you have a graphing calculator you can graph that equation to have a further understanding of how it works... but its one of those things that I understand but cant begin to explain, at least not right now. Just know TIME*MAXVALUE/DURATION will work for making a variable decrease according to time for the given amount of time.

I have no idea how well I just explained that but I hope I didnt just confuse you more. =D

Posted on 2005-08-02 22:02:19

RageCage

You know what... I'm kinda tired but I think that fadeout equation is actually increasing in value not decreasing... once again... damn you greu!

Posted on 2005-08-02 22:04:26

RageCage

grue.

Posted on 2005-08-02 22:04:44

ExSea

thanks man .. i somehow understand whats youre trying to say... i m brushing up my game system now. its just one map with a battle engine

Posted on 2005-08-03 06:47:18

RageCage

Awsome! Glad I could help then!

Posted on 2005-08-03 10:43:52

ExSea

thanks to all the ppl who replied to this... in the end i managed to get accurate ticks by this:

void GetDelay()
{
timer = 0;
while (timer <1)
{
render();
rectfill(0,0,imagewidth(screen),imageheight(screen),RGB(0,0,0),screen);
PrintString(0, 0, screen, 0, 'Wasap!'+str(currentselection)+' '+str(delay));
Showpage();
}
delay += 1;
}

since looping timer functions is just to do it for an amount of time i let the timer drawout whatever it needs for 1/100 sec. at the end of the loop i use delay variable to indicate the amount of time passed per sec. from there i ll use the delay var. ^_^

Posted on 2005-08-04 04:26:36

Overkill

That'll most definitely have issues on my PC. I bet by the time it gets to that loop, the timer'll already be greater than 1.

Posted on 2005-08-04 08:50:46

ExSea

thats a fast pc u got there >_<

Posted on 2005-08-04 10:44:42


Displaying 1-20 of 26 total.
12 next
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.