侧边栏壁纸
博主头像
亿元丁真博主等级

行动起来,活在当下

  • 累计撰写 9 篇文章
  • 累计创建 8 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

🍎Sourcepawn脚本入门(二)命令与事件监听

黎翰
2023-12-08 / 0 评论 / 0 点赞 / 33 阅读 / 6995 字

🍎Sourcepawn脚本入门(二)命令与事件监听

(控制台)命令是常用的插件形式,eg. noclip ...等都是常用的命令,在游戏中使用也很容易,souremod可以注册自己的命令。

事件的监听则需要考虑到不同的起源游戏支持的事件不同,具体可以参考如下:Game Events (Source) - AlliedModders Wiki (alliedmods.net)

1.注册控制台命令

使用RegConsoleCmd注册控制台命令

#include <sdkhooks>
#include <sdktools>
#include <sourcemod>
#pragma newdecls required
#pragma semicolon 1
​
​
public Plugin myinfo =
{
    name = "pluginOne",
    author = "",
    description = "",
    version = "1.0.0",
    url = "https://github.com//pluginOne"
};
​
​
public void OnPluginStart()
{
    PrintToChatAll("这是第一个测试");
    
    //注册控制台命令
    RegConsoleCmd("sm_sayhellow",SayHellowToAll,"这是一个测试测试的控制台命令");
​
}
​
//命令第二个参数为CallBack函数,参数固定为两个int变量(名字随便起)
//但分别代表客服端id和该命令参数的个数
public Action SayHellowToAll(int client,int args)
{
    //向所有人发送信息
    PrintToChatAll("WDNMD");
    
    //Plugin_Handled是Action的返回值,代表执行的结果
    //如果执行成功则为Plugin_Handled,失败则可以为Plugin_Error等
    //当然也可以不返回,但不利于调试
    return Plugin_Handled;
}
​

编译部署到服务器的执行结果:

获取参数信息和客户端信息

#include <sdkhooks>
#include <sdktools>
#include <sourcemod>
#pragma newdecls required
#pragma semicolon 1
​
​
public Plugin myinfo =
{
    name = "pluginOne",
    author = "",
    description = "",
    version = "1.0.0",
    url = "https://github.com//pluginOne"
};
​
​
public void OnPluginStart()
{
    PrintToChatAll("这是第一个测试");
    
    //注册控制台命令
    RegConsoleCmd("sm_sayhellow",SayHellowToAll,"这是一个测试测试的控制台命令");
    RegConsoleCmd("sm_everyone",SaySthSeveralTimes,"这是一个测试测试的控制台命令");
​
}
​
public Action SayHellowToAll(int client,int args)
{
    //向所有人发送信息
    PrintToChatAll("WDNMD");
    
    return Plugin_Handled;
}
​
public Action SaySthSeveralTimes(int client,int args){
​
    char clientname[36],args1[64],args2[10];
    
    //根据需要,限制参数的个数
    if(args!=2){
        PrintToChat(client,"此命令需要两个参数,1为内容,2为要重复的次数");
        return Plugin_Handled;
    }
    
    //利用参数和client的API获取信息
    GetClientName(client,clientname,sizeof(clientname));
​
    GetCmdArg(1,args1,sizeof(args1));
​
    GetCmdArg(2,args2,sizeof(args2));
​
    int times = StringToInt(args2);
​
    if(times<1||times>=10){
​
        PrintToChat(client,"次数必须在10以内!");
        return Plugin_Handled;
​
    }
    
    //循环输出
    for(int i =0;i<times;i++){
        PrintToChatAll("| %s | say %s To EveryOne",clientname,args1);
    }
​
    return Plugin_Handled;
}

2.监听游戏事件

游戏事件在具体游戏上会有一些差别,所以在此处请参考文档

Game Events (Source) - AlliedModders Wiki (alliedmods.net)

此处以Player_death为例:

列表中提供的参数里事件中可以获取到的参数。

#include <sdkhooks>
#include <sdktools>
#include <sourcemod>
#pragma newdecls required
#pragma semicolon 1
​
​
public Plugin myinfo =
{
    name = "pluginOne",
    author = "",
    description = "",
    version = "1.0.0",
    url = "https://github.com//pluginOne"
};
​
​
public void OnPluginStart()
{
    PrintToChatAll("这是第一个测试");
    //绑定事件和它的触发函数
    HookEvent("player_death",OnPlayerDeathFunction);
}
​
//事件触发函数,同上参数类型固定,变量名不固定
public void OnPlayerDeathFunction(Event event, const char[] name, bool dontBroadcast){
    //找到文档中可以参照的属性
    //short userid  user ID who died
    //short attacker    user ID who killed
    //string    weapon  weapon name killer used
    //bool  headshot    singals a headshot
    
    //获取被打人的userid
    int userid = event.GetInt("userid");
    //获取攻击者的id
    int attacker = event.GetInt("attacker");
    //获取是否爆头
    bool isHeadshot = event.GetBool("headshot");
​
    //通过事件ID获取ClientID
    int clientid = GetClientOfUserId(userid);
​
    int attackerid = GetClientOfUserId(attacker);
​
    char victim_name[36],attacker_name[36];
​
    GetClientName(clientid,victim_name,sizeof(victim_name));
​
    GetClientName(attackerid,attacker_name,sizeof(attacker_name));
    //广播通知信息
    if(isHeadshot){
        PrintToChatAll(" | %s | kill | %s |  in headshot ",attacker_name,victim_name);
    }else{
        PrintToChatAll(" | %s | kill | %s |  ",attacker_name,victim_name);
    }
​
​
}
​

0

评论区