AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Making a "wait" function (https://forums.alliedmods.net/showthread.php?t=189080)

Desikac 07-03-2012 09:18

Making a "wait" function
 
Is there any way to make a function that will pause the code for a specified amount of time?

For example:
PHP Code:

public plugin_init()
    
register_clcmd("say /test""test")

public 
test(id) {
    new 
name[32]
    
get_user_name(idname31)
    
    
wait(2.0//wait func
    
client_print(0print_chat"Your name is: %s"name//This should execute after 2 seconds


I tried to make an include that adds this function:
PHP Code:

#if defined _engine_included
    
#else 
    #include <engine>
#endif

new created
new Float:ttimer

public TimerThink(iEnt)
{
        
ttimer+= 0.1
        entity_set_float
(iEntEV_FL_nextthinkget_gametime() + 0.1)
}

wait(Float:wtime) {
    if(!
created) {
        
register_think("timer","TimerThink")
        new 
iEnt create_entity("info_target")
        
entity_set_string(iEntEV_SZ_classname"timer")
        
entity_set_float(iEntEV_FL_nextthinkget_gametime() + 0.1)
    }
    
ttimer 0.0
    
if(wtime <= 0)
        return -
1

    
while(wtime ttimer) { }
    return 
1


But it crashes the server.
When I put an "infinite" loop in c++ to count it goes on forever so I thought it might work here too :D
Is there any way to make this? Like, for example, call a function that needs 0.1s to execute 20 times for a 2 second wait? :D

YamiKaitou 07-03-2012 09:29

Re: Making a "wait" function
 
set_task

Desikac 07-03-2012 09:37

Re: Making a "wait" function
 
Quote:

Originally Posted by YamiKaitou (Post 1742115)
set_task

PHP Code:

while(set_task(2.0) > wtime) { } 

:D:mrgreen:

I know that there is set_task but I want to know if it's possible to do it like this.

Arkshine 07-03-2012 09:42

Re: Making a "wait" function
 
http://www.amxmodx.org/funcwiki.php?...task&go=search set_task is the timer.

YamiKaitou 07-03-2012 09:42

Re: Making a "wait" function
 
set_task calls another function. Entity thinks call another function. You cannot add a pause into a current function without a while loop.

Desikac 07-03-2012 09:52

Re: Making a "wait" function
 
Quote:

Originally Posted by YamiKaitou (Post 1742134)
You cannot add a pause into a current function without a while loop.

And how can I add it WITH a while loop?

YamiKaitou 07-03-2012 09:56

Re: Making a "wait" function
 
PHP Code:

new 0;
while (
50000); 

Replace 50000 with the correct number that equals the amount of time you want to wait.

Liverwiz 07-03-2012 10:23

Re: Making a "wait" function
 
Quote:

Originally Posted by YamiKaitou (Post 1742150)
PHP Code:

new 0;
while (
50000); 

Replace 50000 with the correct number that equals the amount of time you want to wait.

oh dear god no. Use set_task

YamiKaitou 07-03-2012 10:27

Re: Making a "wait" function
 
Quote:

Originally Posted by Liverwiz (Post 1742184)
oh dear god no. Use set_task

Hey, he asked me how to. I, by no means, recommend the while loop method

Liverwiz 07-03-2012 10:28

Re: Making a "wait" function
 
Quote:

Originally Posted by YamiKaitou (Post 1742191)
Hey, he asked me how to. I, by no means, recommend the while loop method

Oh i know you know. My skin just crawled when i saw that....


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

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