AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [ Solved ] While ? (https://forums.alliedmods.net/showthread.php?t=185105)

Aooka 05-12-2012 18:01

[ Solved ] While ?
 
Hello everyone,

Today, i have a new question for you. It's about the loop while :D
So my question is very simple :

Have you got any exemple of code with the loop while and with if it's possible a variable ... :)

Ex1ne 05-12-2012 18:21

Re: While ?
 
Well, here's an example code used for my time played plugin, the variable is here an example.
I hope you learn something with this.
PHP Code:

#include <amxmodx>
#include <amxmisc>

new seconds 530 // random time in seconds our "client" has spent on the server

public plugin_init() 
{
    
register_clcmd"say /time""zaa" )
}
public 
zaa(id)
{
    new 
hoursminutes // integer for hours & minutes
    
    
while( seconds >= 60 // This will continue to pass through untill variable: seconds, is under 60.
    
{
        
minutes++
        
seconds -= 60 
    
}
    
    
    while( 
minutes >= 60 // Same as above, only seconds -> minutes
    
{
        
hours++
        
minutes -= 60
    
}
    
client_print(idprint_chat"You have played %i hours, %i minutes & %i seconds"hoursminutesseconds// print it



Aooka 05-12-2012 18:28

Re: While ?
 
Oh thanks a lot ! I did not expect :D

Aooka 05-12-2012 18:50

Re: While ?
 
Hum the include <amxmisc> is useless i think ^^

fysiks 05-13-2012 02:24

Re: While ?
 
There are examples of while loops all over the internet.

Also, the code above can be done without any loops.

Ex1ne 05-13-2012 08:35

Re: While ?
 
Quote:

Originally Posted by fysiks (Post 1707923)
There are examples of while loops all over the internet.

Also, the code above can be done without any loops.

How? O__o

<VeCo> 05-13-2012 08:48

Re: While ?
 
PHP Code:

seconds 60 // returns seconds
seconds 60 // returns minutes
seconds 60 60 // returns hours 


fysiks 05-13-2012 16:37

Re: While ?
 
Quote:

Originally Posted by <VeCo> (Post 1708100)
PHP Code:

seconds 60 // returns seconds
seconds 60 // returns minutes
seconds 60 60 // returns hours 


If the time is actually 1 hour 15 minutes and 32 seconds your code will return 1 hour 75 minutes and 32 seconds. That is OK if you are not going to only use minutes and seconds but not if you want to use all three.

This is the correct method when wanting to use days, hours, minutes, and seconds (days can easily be omitted):

PHP Code:

        iDays iTimeLeft 86400
        iHours 
iTimeLeft 86400 3600
        iMinutes 
iTimeLeft 3600 60
        iSeconds 
iTimeLeft 60 



All times are GMT -4. The time now is 00:26.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.