Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   % and timevar (https://forums.graalonline.com/forums/showthread.php?t=42922)

tlf288 02-20-2003 02:40 AM

% and timevar
 
Ok, I usually pick up on things really easily, but I just can't pick up on this subject.

First, explain to me how exactly timevar is usefull. How it works and it's purposes.

Second, how can the modulus (%) sign get the time? I have seen many scripts do this but I don't understand how it works.

RTelvecho 02-20-2003 03:35 AM

timevar uses the server time or something, its more ifficient than adding manually i guess...

% is not for getting time, its like until

this.var=(this.var+1)%5;
Means
Add 1 to this.var until its value is 4 then go back to 0 and start again.

HoudiniMan 02-20-2003 04:27 AM

since i've never really understood this either tell me if this is right...

say you had a fishing npc and you got 1 gralat for each 7 pounds of fish... could you use something like this where client.fish is how many pounds they've caught...?
NPC Code:

if (playerchats&&strequals(#c,sell fish)) { //or some such...
fishleft=strtofloat(client.fish)%7; //finds if there are fish left over
fishsold=strtofloat(client.fish)-#v(fishleft); //removes left over fish
playerrupees+=(fishsold/7)*1; //total divided by how many bunches you had, times price (1 is optional in this case)
client.fish=fishleft; //restores left over fish as total
}



it would find the remainder and store it as "fish left"
then take it away from the main number of fish and sell the bulk off...
then replace the bulk with what was left over originally...

so if you had 30 fish it would have remainder 2, take two away, sell the 28 for 4g, then replace your ammount with just 2 pounds theoretically...
something i just kinda thought up from reading your guys' posts

HoudiniMan 02-20-2003 04:42 AM

doesn't int() round to the nearest integer?
or does it always round down, just stripping off the decimels?
cause it you had 13 fish i wouldn't want it to round 1.8 up to 2 and give you any free fish

Falados 02-20-2003 05:16 PM

Quote:

Originally posted by HoudiniMan
doesn't int() round to the nearest integer?
or does it always round down, just stripping off the decimels?
cause it you had 13 fish i wouldn't want it to round 1.8 up to 2 and give you any free fish

int() is a conversion statement that converts floating points to integers, It just truncates the floating points disregaurding whether or not its >=.5 or not. If you want to round you could do something like...

var=int(var+0.5);

Python523 02-20-2003 11:55 PM

Quote:

Originally posted by Kaimetsu

And far more accurate/reliable. Timevar represents a precise global time measurement since the start of the server. It increments once every five seconds.

according to stefan timevar is the time since pay to play has come out, but the start time doesn't really matter since all you really need to know is that it increases once every 5 seconds so what you said is accurate enough

tlf288 02-21-2003 02:19 AM

What is timevar2 then?

and

What would this return?
NPC Code:
test = (mytimevar%(30*24))*2;


tlf288 02-21-2003 02:34 AM

Quote:

Originally posted by Kaimetsu


It would return the remainder of mytimevar divided by 720, multiplied by two.

I meant, What kind of time would it return? What time would you get that with.

PrinceDark 02-21-2003 04:16 AM

%
A simple example
NPC Code:

dir=(dir+2)%4;



So let's say you have a npc that has onwall checks and you want it to turn in the other direction when it is at a certain distance from the wall. The above code will make it turn in the opposite direction.

if dir==1. So if the npc has onwall detection while heading left, and it gets near the wall and performs this command it will change the direction to 3.

So to break it down.
dir=(dir+2)%4;
=(1+2)%4
=3 - int(3 / 4) * 4
=3 - int(.75) * 4
=3 - 0*4
=3 - 0

So in the end it will be dir=3

There's another way to rotate the player or npc's. I think it's dir=(dir+1)%4; and it rotates clockwise or counterclockwise. Something like that.

--
For timevar I have no clue. I never used it before and I'll be learning it now when people explain it in this thread I suppose.

tlf288 02-21-2003 04:21 AM

Quote:

Originally posted by PrinceDark
%
dir=(dir+2)%4;
=(1+2)%4
=3 - int(3 / 4) * 4
=3 - int(.75) * 4
=3 - 0*4
=3 - 0

I understand that. Thank you very much.

tlf288 02-21-2003 04:56 AM

Quote:

Originally posted by Kaimetsu
I wish people would stop spreading these stupid modulus rotation methods. They're extremely inefficient - they only look good because they're so short.
How would you rotate an npc to the opisite direction?

Com013 02-23-2003 09:50 PM

Quote:

Originally posted by Kaimetsu
dir+=2;
if(dir>3) dir-=4;

But aren't the npc attributes limited to a certain range? Then the dir wouldn't be able to get above 3. (I know, that would be no problem, you'd just use another variable).

And in some cases I'd use while(ndir > 3) instead of if(ndir > 3), just in case it is too big (Which is impossible at the given example).

But why is % slower? I think I've heard that modulo is quite a complex floating point operation, but we normaly have FPUs integrated in the CPUs and the FPU operation would still be faster than interpreting 3 additional Graal Script Commands, wouldn't it?

Falados 02-23-2003 10:04 PM

The point is, WHY would you create a floating point if your answer will never need it. Integer mathmatics takes less processor time that floating point.

I mean all the computer has to do is see if an integer is greater then a certain number and then if it is, set it to zero. but the MOD has to do

a-int(a/b)*b to do the same operation. Shorter in the code, but more complex than it needs to be.


All times are GMT +2. The time now is 08:23 PM.

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