SourceMod 僵尸插件:ZombiePlugin - 创建和使用菜单
#include
TopMenu TMenu = null; TopMenuObject Menu1;
public Plugin myinfo = { name = "ZombiePlugin", author = "xxk", description = "ZombiePlugin", version = "1.0", url = "http://www.sourcemod.net/" };
void ZombieMenuProc(TopMenu topmenu, TopMenuAction action, TopMenuObject topobj_id, int param, char[] buffer, int maxlength) { /switch(action) { case TopMenuAction_DisplayOption::; case TopMenuAction_DisplayTitle::; case TopMenuAction_SelectOption::; case TopMenuAction_DrawOption::; case TopMenuAction_RemoveObject::; }/ }
public void OnPluginStart() { LoadTranslations("ZombieMenu.phrases"); RegConsoleCmd("zmenu", ZombieMenu); }
public void OnAllPluginsLoaded() { TMenu = new TopMenu(ZombieMenuProc); Menu1 = TMenu.AddItem("aa",ZombieMenuProc,TMenu,"sm_admin"); // TMenu2 = TMenu.AddItem("bb",ZombieMenuProc,TMenu); if(Menu1 == INVALID_TOPMENUOBJECT) PrintToServer("Menu Load Fail!"); }
public bool OnClientConnect(int client, char[] rejectmsg, int maxlen) { if(GetClientCount()>=MaxClients) { strcopy(rejectmsg,maxlen,"服务器拒绝了你的连接!"); return false; } return true; }
public void OnClientConnected(int client) //当客户端成功连接时 { char name[255]; if(GetClientName(client,name,255)) { if(IsClientReplay(client)) { PrintToChatAll("%s 已经连接服务器!",name); } else { PrintToChatAll("%s 已经连接服务器!",name); } }
}
public void OnClientDisconnect(int client) //当客户端断开连接时 {
}
public int ZombieMenuHandle(Menu menu, MenuAction action, int param1, int param2) { /* If an option was selected, tell the client about the item. / if (action == MenuAction_Select) { char info[32]; bool found = menu.GetItem(param2, info, sizeof(info)); PrintToConsole(param1, "You selected item: %d (found? %d info: %s)", param2, found, info); } / If the menu was cancelled, print a message to the server about it. / else if (action == MenuAction_Cancel) { PrintToServer("Client %d's menu was cancelled. Reason: %d", param1, param2); } / If the menu has ended, destroy it */ else if (action == MenuAction_End) { delete menu; } return 0; }
public Action ZombieMenu(int client, int args) { if(!TMenu.Display(client,TopMenuPosition_Start)) ReplyToCommand(client,"[K] 菜单显示失败!"); return Plugin_Handled; }
public void OnConfigsExecuted() { char path[PLATFORM_MAX_PATH]; char Err[255]; BuildPath(Path_SM, path, sizeof(path), "configs/ZombieMenu.cfg"); TMenu.LoadConfig(path,Err,255); }
AddItem 返回 INVALID_TOPMENUOBJECT 的原因
AddItem 返回 INVALID_TOPMENUOBJECT 可能是因为添加菜单项时发生了错误,例如参数不正确或者菜单已满。
建议检查以下方面:
- 参数是否正确: 检查 AddItem 函数的参数是否正确,例如:
- 第一个参数:菜单项的名称
- 第二个参数:菜单项的回调函数
- 第三个参数:父菜单
- 第四个参数(可选):权限
- 菜单是否已满: TopMenu 的菜单项数量可能有限制,检查菜单是否已满。
- 错误日志: 查看 SourceMod 的错误日志,查找有关 AddItem 错误的更多信息。
如果以上方法无法解决问题,请提供更详细的代码片段和错误信息,以便更好地帮助你排查问题。
原文地址: https://www.cveoy.top/t/topic/oJq2 著作权归作者所有。请勿转载和采集!