Raised This Month: $51 Target: $400
 12% 

Need help with looping a function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Destine
Junior Member
Join Date: Apr 2016
Old 04-30-2016 , 18:13   Need help with looping a function
Reply With Quote #1

Hello there everyone, I just getting into Sourcemod scripting so I apologize for my lack of knowledge. I've been trying to execute a function every 50 seconds that makes the player say "test" but the function never gets executed.

This is my code:
PHP Code:
public OnPluginStart() 

        
HookEvent("player_spawned"OnSpawn); 

  
public 
Action:OnSpawn(Handle:event, const String:name[], bool:dontBroadcast

        new 
client GetClientOfUserId(GetEventInt(event"userid"));
        while(
true)
        {
            
CreateTimer(50.0Timer_Testclient);
        }
}

public 
Action:Timer_Test(Handle:timerany:client)
{
        
FakeClientCommandEx(client"say test");

Any ideas on how to fix this?
Destine is offline
xines
Veteran Member
Join Date: Aug 2013
Location: Denmark
Old 04-30-2016 , 18:21   Re: Need help with looping a function
Reply With Quote #2

Like this?

PHP Code:
public void OnPluginStart()
{
    
HookEvent("player_spawn"OnSpawn);
}

public 
Action OnSpawn(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
CreateTimer(50.0Timer_TestclientTIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public 
Action Timer_Test(Handle timerany client)
{
    if(
IsClientInGame(i))
    {
        
FakeClientCommandEx(client"say test");
    }
    
    return 
Plugin_Continue;

__________________
xines is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 04-30-2016 , 18:24   Re: Need help with looping a function
Reply With Quote #3

Note: NEVER PASS A RAW CLIENT INDEX TO A TIMER.

PHP Code:
public void OnClientPutInServer(int client)
{
    
CreateTimer(50.0Timer_TestGetClientUserId(client), TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public 
void OnMapStart()
{
    for (
int i 1<= MaxClients; ++i)
    {
        if (
IsClientInGame(i))
            
CreateTimer(50.0Timer_TestGetClientUserId(i), TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
    }
}

public 
Action Timer_Test(Handle timerany data)
{
    
int client GetClientOfUserId(data);

    if (!
IsClientInGame(client))
        return 
Plugin_Stop;

    
FakeClientCommandEx(client"say test");
    return 
Plugin_Continue;


Last edited by WildCard65; 04-30-2016 at 18:30.
WildCard65 is offline
Destine
Junior Member
Join Date: Apr 2016
Old 04-30-2016 , 18:25   Re: Need help with looping a function
Reply With Quote #4

Quote:
Originally Posted by xines View Post
Like this?

PHP Code:
public void OnPluginStart()
{
    
HookEvent("player_spawn"OnSpawn);
}

public 
Action OnSpawn(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
CreateTimer(50.0Timer_TestclientTIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public 
Action Timer_Test(Handle timerany client)
{
    if(
IsClientInGame(i))
    {
        
FakeClientCommandEx(client"say test");
    }
    
    return 
Plugin_Continue;

Unidentified variable i, I'm guessing thats client?
Destine is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 04-30-2016 , 18:26   Re: Need help with looping a function
Reply With Quote #5

Quote:
Originally Posted by Destine View Post
Unidentified variable i, I'm guessing thats client?
Ya, but it's a bad idea to pass raw client indexes to timers as it may not be the same client when timer fires.

Edit: Update code I posted in reply #3

Last edited by WildCard65; 04-30-2016 at 18:29.
WildCard65 is offline
xines
Veteran Member
Join Date: Aug 2013
Location: Denmark
Old 04-30-2016 , 18:30   Re: Need help with looping a function
Reply With Quote #6

Quote:
Originally Posted by WildCard65 View Post
Note: NEVER PASS A RAW CLIENT INDEX TO A TIMER.
True, i'm posting to fast what can i say lol
__________________

Last edited by xines; 04-30-2016 at 18:36.
xines is offline
Destine
Junior Member
Join Date: Apr 2016
Old 04-30-2016 , 18:34   Re: Need help with looping a function
Reply With Quote #7

Quote:
Originally Posted by WildCard65 View Post
Ya, but it's a bad idea to pass raw client indexes to timers as it may not be the same client when timer fires.

Edit: Update code I posted in reply #3
Quick question, what would I set my HookEvent to? Like which function?
Destine is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 04-30-2016 , 18:37   Re: Need help with looping a function
Reply With Quote #8

Quote:
Originally Posted by Destine View Post
Quick question, what would I set my HookEvent to? Like which function?
The code I posted removed the hooking of player_spawn event as that would end up causing multiple timers to be create and none existing ones being killed which would result in multiple triggers of FakeClientCommandEx on the same client.
WildCard65 is offline
Destine
Junior Member
Join Date: Apr 2016
Old 04-30-2016 , 18:42   Re: Need help with looping a function
Reply With Quote #9

Quote:
Originally Posted by WildCard65 View Post
The code I posted removed the hooking of player_spawn event as that would end up causing multiple timers to be create and none existing ones being killed which would result in multiple triggers of FakeClientCommandEx on the same client.
So what do I put into my OnPluginStart() function?
Destine is offline
xines
Veteran Member
Join Date: Aug 2013
Location: Denmark
Old 04-30-2016 , 18:50   Re: Need help with looping a function
Reply With Quote #10

Quote:
Originally Posted by Destine View Post
So what do I put into my OnPluginStart() function?
Its not needed for the plugin to function.
__________________
xines is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:09.


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