AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   get Day / Night (https://forums.alliedmods.net/showthread.php?t=253633)

ghost95v 12-25-2014 15:22

get Day / Night
 
Hello, im wondering if is this code good to get Night hours ? and does this can use to much CPU usage?

PHP Code:


public plugin_init()
{
    static 
Hour[64], bool:Night;

    
get_time("%H"Hour63);
    
    if (
Hour[0] == '0')
        
Night true;

    else
        
Night false


HamletEagle 12-25-2014 15:29

Re: get Day / Night
 
How this could cause CPU usage ? plugin_init is called one time and also you don't need to use static. Also the array size is too big, 6 is enough for a full time string. Also your code won't work 100%, because you are not sure that plugin_init will get called(by map change, server (re)start) exactly at 00:00

ghost95v 12-25-2014 15:45

Re: get Day / Night
 
1- thank you for the advice.
2- the code works great, ( tested aloot ), sure it will be called always after a map change if the time contains 00: xx / 01: xx / 02: xx etc....
3- cant understand why array size enought for that ?

HamletEagle 12-26-2014 05:13

Re: get Day / Night
 
Sorry, I assumed that you only want "00:xx" hour. If I remember well %H give you only the hour without minutes so it will be a string like ab. 64 cells are too much for such little string.

klippy 12-26-2014 07:15

Re: get Day / Night
 
You might want to use this function instead:
PHP Code:

time(&hour=0, &minute=0, &second=0); 

so you would have number of hours as an integer instead of a string, so comparing it to other values would be easier.
PHP Code:

new iHours;
time(iHours);
// iHours now contains number of hours!
// ...
if(iHours >= 22 && iHours 7)
{
    
// Nighttime is from 10 PM to 7 AM



ghost95v 12-26-2014 07:21

Re: get Day / Night
 
Quote:

Originally Posted by HamletEagle (Post 2240141)
Sorry, I assumed that you only want "00:xx" hour. If I remember well %H give you only the hour without minutes so it will be a string like ab. 64 cells are too much for such little string.

Yes... and for the array size your'e right, its enought only "6", size 63 can be useful only if we do this below

PHP Code:

get_time("!g%m/%d/%Y - %H:%M:%S"Hour63



All times are GMT -4. The time now is 15:22.

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