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

Please help with this


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joac1144
Senior Member
Join Date: Dec 2012
Location: Copenhagen, Denmark
Old 03-11-2014 , 14:09   Please help with this
Reply With Quote #1

Hello,

I'm trying to make a plugin that creates a command which only should be able to activate one time pr. round.
Example:
I type !randombuff when the round begins, after that, I shouldn't be able to use the command again that round.

I have no idea how I can do that, so please come with an example

(I have already made the command, I only need the "one time per round" thing)
__________________
joac1144 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-11-2014 , 14:42   Re: Please help with this
Reply With Quote #2

Well, it will go something like this:

PHP Code:
// outside any functions
new bool:g_bCommandUsed[MAXPLAYERS+1];

//in OnPluginStart, if you haven't already hooked it.
    
HookEvent("round_start"Event_RoundStart);
    
HookEventEx("teamplay_round_start"Event_RoundStart); // TF2-only

// and the actual round start event hook
public Event_RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
    for (new 
1<= MaxClientsi++)
    {
        
g_bCommandUsed[i] = false;
    }
}

// When the command is used
    
if (g_bCommandUsed[client])
    {
        
PrintToChat(client"You already used this command once this round.");
        return 
Plugin_Handled;
    }

    
g_bCommandUsed[client] = true
You can find a real-world example of this in the TF2 PropHunt Redux plugin with its propreroll feature, although it only binds the TF2-specific round start.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 03-11-2014 at 14:53.
Powerlord is offline
joac1144
Senior Member
Join Date: Dec 2012
Location: Copenhagen, Denmark
Old 03-11-2014 , 15:53   Re: Please help with this
Reply With Quote #3

Quote:
Originally Posted by Powerlord View Post
Well, it will go something like this:

PHP Code:
// outside any functions
new bool:g_bCommandUsed[MAXPLAYERS+1];

//in OnPluginStart, if you haven't already hooked it.
    
HookEvent("round_start"Event_RoundStart);
    
HookEventEx("teamplay_round_start"Event_RoundStart); // TF2-only

// and the actual round start event hook
public Event_RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
    for (new 
1<= MaxClientsi++)
    {
        
g_bCommandUsed[i] = false;
    }
}

// When the command is used
    
if (g_bCommandUsed[client])
    {
        
PrintToChat(client"You already used this command once this round.");
        return 
Plugin_Handled;
    }

    
g_bCommandUsed[client] = true
You can find a real-world example of this in the TF2 PropHunt Redux plugin with its propreroll feature, although it only binds the TF2-specific round start.
Thank you for the fast reply!
But it doesn't work properly;
the second time I am using the command in a round, it prints the message, but the command still works.
Here's the FULL sourcecode:
Spoiler
__________________

Last edited by joac1144; 03-11-2014 at 16:30.
joac1144 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 03-11-2014 , 16:22   Re: Please help with this
Reply With Quote #4

It would probably work if you'd use the snippet he gave you. you're not even hooking round_start, you're hooking player_spawn, which is fired every player spawn, plus the loop.
Mitchell is offline
joac1144
Senior Member
Join Date: Dec 2012
Location: Copenhagen, Denmark
Old 03-11-2014 , 16:29   Re: Please help with this
Reply With Quote #5

Quote:
Originally Posted by Mitchell View Post
It would probably work if you'd use the snippet he gave you. you're not even hooking round_start, you're hooking player_spawn, which is fired every player spawn, plus the loop.
Woops!
I misread it...
But after changing it to what PowerLord actually said, it still does the same
I have updated my code to the new one
__________________

Last edited by joac1144; 03-11-2014 at 16:30.
joac1144 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 03-11-2014 , 16:38   Re: Please help with this
Reply With Quote #6

you put the code that is supposed to stop the command from executing after the code of the command...
PHP Code:
    if (g_bCommandUsed[client])
    {
        
CPrintToChat(client"{green}[RandomBuff] {default}You already have used Randombuff this round!");
        return 
Plugin_Handled;
    }
    
    
g_bCommandUsed[client] = true
should be before anything happens in the code.

edited code:
PHP Code:
#include <sourcemod>
#include <morecolors>
#include <sdktools>

new bool:g_bCommandUsed[MAXPLAYERS+1];

public 
Plugin:myinfo =
{
    
name "Random buffs",
    
author "Born",
    
description "Gives you a random buff",
    
version "1.0",
    
url ""
};

public 
OnPluginStart()
{
    
RegConsoleCmd("sm_randombuff"RandomBuffCmd"Roll the dice!");
    
HookEvent("player_spawn"PlayerSpawnEvent); 
    
HookEvent("round_start"RoundStartEvent);
}

public 
Action:RandomBuffCmd(clientargs)
{
    if (
g_bCommandUsed[client])
    {
        
CPrintToChat(client"{green}[RandomBuff] {default}You already have used Randombuff this round!");
        return 
Plugin_Handled;
    }
    
    
g_bCommandUsed[client] = true;
    
ApplyRandomBuff(client);
    return 
Plugin_Handled;
}

public 
ApplyRandomBuff(client)
{
    switch(
GetRandomInt(15))
    {
        case 
1:
        {
            
SetEntityHealth(clientGetClientHealth(client) + GetRandomInt(1030));
            
CPrintToChat(client"{green}[RandomBuff] {default}You got extra health!");
        }
        
        case 
2:
        {
            
SetEntityGravity(client0.7);
            
CPrintToChat(client"{green}[RandomBuff] {default}You got lower gravity!");
        }
        
        case 
3:
        {
            
SetEntProp(clientProp_Send"m_iAccount"GetEntProp(clientProp_Send"m_iAccount") + GetRandomInt(10005000)); 
            
CPrintToChat(client"{green}[RandomBuff] {default}You got extra money!");
        }
        
        case 
4:
        {
            
SetEntPropFloat(clientProp_Data"m_flLaggedMovementValue"1.4);
            
CPrintToChat(client"{green}[RandomBuff] {default}You got extra speed!");
        }
        
        case 
5:
        {
            switch(
GetRandomInt(14))
            {
                case 
1:
                {
                    
GivePlayerItem(client"weapon_ak47");
                    
CPrintToChat(client"{green}[RandomBuff] {default}You got an AK47!");
                }
        
                case 
2:
                {
                    
GivePlayerItem(client"weapon_m4a1");
                    
CPrintToChat(client"{green}[RandomBuff] {default}You got a M4A1!");
                }
        
                case 
3:
                {
                    
GivePlayerItem(client"weapon_awp");
                    
CPrintToChat(client"{green}[RandomBuff] {default}You got an AWP!");
                }
        
                case 
4:
                {
                    
GivePlayerItem(client"weapon_scout");
                    
CPrintToChat(client"{green}[RandomBuff] {default}You got a Scout!");
                }
            }
        }
    }
}

public 
PlayerSpawnEvent(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));

    
SetEntityGravity(client1.0);
}

public 
RoundStartEvent(Handle:event, const String:name[], bool:dontBroadcast)
{
    for (new 
1<= MaxClientsi++)
    {
        
g_bCommandUsed[i] = false;
    }


Last edited by Mitchell; 03-11-2014 at 16:41.
Mitchell is offline
joac1144
Senior Member
Join Date: Dec 2012
Location: Copenhagen, Denmark
Old 03-12-2014 , 10:23   Re: Please help with this
Reply With Quote #7

Quote:
Originally Posted by Mitchell View Post
you put the code that is supposed to stop the command from executing after the code of the command...
PHP Code:
    if (g_bCommandUsed[client])
    {
        
CPrintToChat(client"{green}[RandomBuff] {default}You already have used Randombuff this round!");
        return 
Plugin_Handled;
    }
    
    
g_bCommandUsed[client] = true
should be before anything happens in the code.

edited code:
PHP Code:
#include <sourcemod>
#include <morecolors>
#include <sdktools>

new bool:g_bCommandUsed[MAXPLAYERS+1];

public 
Plugin:myinfo =
{
    
name "Random buffs",
    
author "Born",
    
description "Gives you a random buff",
    
version "1.0",
    
url ""
};

public 
OnPluginStart()
{
    
RegConsoleCmd("sm_randombuff"RandomBuffCmd"Roll the dice!");
    
HookEvent("player_spawn"PlayerSpawnEvent); 
    
HookEvent("round_start"RoundStartEvent);
}

public 
Action:RandomBuffCmd(clientargs)
{
    if (
g_bCommandUsed[client])
    {
        
CPrintToChat(client"{green}[RandomBuff] {default}You already have used Randombuff this round!");
        return 
Plugin_Handled;
    }
    
    
g_bCommandUsed[client] = true;
    
ApplyRandomBuff(client);
    return 
Plugin_Handled;
}

public 
ApplyRandomBuff(client)
{
    switch(
GetRandomInt(15))
    {
        case 
1:
        {
            
SetEntityHealth(clientGetClientHealth(client) + GetRandomInt(1030));
            
CPrintToChat(client"{green}[RandomBuff] {default}You got extra health!");
        }
        
        case 
2:
        {
            
SetEntityGravity(client0.7);
            
CPrintToChat(client"{green}[RandomBuff] {default}You got lower gravity!");
        }
        
        case 
3:
        {
            
SetEntProp(clientProp_Send"m_iAccount"GetEntProp(clientProp_Send"m_iAccount") + GetRandomInt(10005000)); 
            
CPrintToChat(client"{green}[RandomBuff] {default}You got extra money!");
        }
        
        case 
4:
        {
            
SetEntPropFloat(clientProp_Data"m_flLaggedMovementValue"1.4);
            
CPrintToChat(client"{green}[RandomBuff] {default}You got extra speed!");
        }
        
        case 
5:
        {
            switch(
GetRandomInt(14))
            {
                case 
1:
                {
                    
GivePlayerItem(client"weapon_ak47");
                    
CPrintToChat(client"{green}[RandomBuff] {default}You got an AK47!");
                }
        
                case 
2:
                {
                    
GivePlayerItem(client"weapon_m4a1");
                    
CPrintToChat(client"{green}[RandomBuff] {default}You got a M4A1!");
                }
        
                case 
3:
                {
                    
GivePlayerItem(client"weapon_awp");
                    
CPrintToChat(client"{green}[RandomBuff] {default}You got an AWP!");
                }
        
                case 
4:
                {
                    
GivePlayerItem(client"weapon_scout");
                    
CPrintToChat(client"{green}[RandomBuff] {default}You got a Scout!");
                }
            }
        }
    }
}

public 
PlayerSpawnEvent(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));

    
SetEntityGravity(client1.0);
}

public 
RoundStartEvent(Handle:event, const String:name[], bool:dontBroadcast)
{
    for (new 
1<= MaxClientsi++)
    {
        
g_bCommandUsed[i] = false;
    }

Thank you!
It works now.
__________________
joac1144 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 20:49.


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