amxmodx-basics
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAMX Mod X (Pawn) Code Style Guide
AMX Mod X (Pawn) 代码风格指南
This document provides an overview of coding conventions for AMX Mod X projects. Each topic is covered in detail in its own file.
本文档概述了AMX Mod X项目的编码规范,每个主题的详细内容都在独立文件中。
Core Conventions
核心规范
| Category | Description |
|---|---|
| Basics | Syntax, basics |
| Code Style | File structure, formatting, braces, spacing, plugin registration |
| Naming Conventions | Hungarian notation, variable prefixes, naming patterns |
| Constants & Enums | Define constants, enums, TASKID constants |
| Macros | Common macros, IS_PLAYER, patterns to avoid |
| Function Declarations | Return types, @ prefix, static variables |
| Validations | FM_NULLENT, entity checks, player validation, early returns |
| 分类 | 描述 |
|---|---|
| 基础 | 语法、基础知识 |
| 代码风格 | 文件结构、格式、大括号、间距、插件注册 |
| 命名规范 | 匈牙利命名法、变量前缀、命名模式 |
| 常量与枚举 | 定义常量、枚举、TASKID常量 |
| 宏 | 常用宏、IS_PLAYER、需避免的模式 |
| 函数声明 | 返回类型、@前缀、静态变量 |
| 验证 | FM_NULLENT、实体检查、玩家验证、提前返回 |
API Patterns
API模式
| Category | Description |
|---|---|
| Hooks | Ham, FakeMeta, ReAPI, Event, Message hooks and handles |
| Forwards | CreateMultiForward, ExecuteForward, pre/post patterns |
| Natives | Native registration and implementation |
| Callbacks | Tasks, SQL queries, CVar queries |
| Menus | Menu creation, callbacks, and patterns |
| Commands | Client, server, and console commands |
| CVars | CVar creation, binding, and change hooks |
| 分类 | 描述 |
|---|---|
| 钩子 | Ham、FakeMeta、ReAPI、事件、消息钩子及句柄 |
| 转发 | CreateMultiForward、ExecuteForward、前置/后置模式 |
| 原生函数 | 原生函数注册与实现 |
| 回调 | 任务、SQL查询、CVar查询 |
| 菜单 | 菜单创建、回调及模式 |
| 命令 | 客户端、服务器及控制台命令 |
| CVar | CVar创建、绑定及变更钩子 |
Performance & Data
性能与数据
| Category | Description |
|---|---|
| Optimizations | Native call reduction, dynamic hooks, model path caching |
| Data Structures | Arrays, Tries, entity access, strings |
| 分类 | 描述 |
|---|---|
| 优化 | 减少原生函数调用、动态钩子、模型路径缓存 |
| 数据结构 | 数组、Trie、实体访问、字符串 |
Custom API Reference
自定义API参考
For project-specific APIs, see dedicated skill files:
| API | Description |
|---|---|
| assets | Asset management from JSON configs |
| custom-entities | OOP-style custom entities |
| custom-events | Pub/sub event system |
| custom-weapons | Custom weapon framework |
| entity-force | Physics force application |
| entity-grab | Entity grab and carry |
| player-camera | Custom camera views |
| player-model | Custom player models |
| player-music | MP3 music playback |
| player-roles | Player role management |
| rounds | Round management |
| shops | In-game shop system |
| states | State machine implementation |
For large project organization with namespace constants, see amxx-modding-kit-project.
针对项目特定API,请查看专用技能文件:
| API | 描述 |
|---|---|
| assets | 基于JSON配置的资源管理 |
| custom-entities | 面向对象风格的自定义实体 |
| custom-events | 发布/订阅事件系统 |
| custom-weapons | 自定义武器框架 |
| entity-force | 物理力施加 |
| entity-grab | 实体抓取与携带 |
| player-camera | 自定义相机视角 |
| player-model | 自定义玩家模型 |
| player-music | MP3音乐播放 |
| player-roles | 玩家角色管理 |
| rounds | 回合管理 |
| shops | 游戏内商店系统 |
| states | 状态机实现 |
如需使用命名空间常量进行大型项目组织,请查看amxx-modding-kit-project。
Quick Reference: Best Practices Summary
快速参考:最佳实践摘要
Code Style
代码风格
- Always use at file start
#pragma semicolon 1 - Use Hungarian notation for all variable names
- Use K&R brace style (opening brace on same line)
- Pass plugin info directly to - avoid macros
register_plugin() - Group hooks and functions with section comments
- 始终在文件开头使用
#pragma semicolon 1 - 所有变量名使用匈牙利命名法
- 使用K&R大括号风格(左大括号与代码同行)
- 直接将插件信息传入- 避免使用宏
register_plugin() - 使用分段注释对钩子和函数进行分组
Constants & Validation
常量与验证
- Use instead of
FM_NULLENTor-1for null entity checks0 - Always check after entity creation before processing
FM_NULLENT - Return not
FM_NULLENTfor failures -0is worldspawn0 - Return early for invalid conditions
- 检查空实体时,使用而非
FM_NULLENT或-10 - 实体创建后,在处理前务必检查
FM_NULLENT - 失败时返回而非
FM_NULLENT-0代表worldspawn(世界实体)0 - 遇到无效条件时提前返回
Macros
宏
- Don't redefine macros - use shared definitions from includes
- Never use patterns in code - only in macro definitions
MACRO()_Suffix
- 不要重定义宏 - 使用头文件中的共享定义
- 代码中绝不要使用模式 - 仅在宏定义中使用
MACRO()_Suffix
Function Declarations
函数声明
- Use prefix only for OOP-like methods:
@@{EntityName}_{MethodName} - Never add keyword to
publicprefixed functions@ - Use prefix for all handle arguments
const - Use for "this" argument in OOP-like methods
const &this - Use static variables in frequently called class methods
- Declare and assign static on same line using semicolon separator
- 仅在类对象风格的方法中使用前缀:
@@{EntityName}_{MethodName} - 带前缀的函数绝不要添加
@关键字public - 所有句柄参数使用前缀
const - 类对象风格的方法中,为“this”参数使用
const &this - 频繁调用的类方法中使用静态变量
- 静态变量的声明与赋值在同一行,使用分号分隔
Hooks
钩子
- Ham hooks must return constants
HAM_* - FakeMeta hooks must return constants
FMRES_* - ReAPI hooks must return constants
HC_* - Message hooks must return constants
PLUGIN_* - Hook handle variables: (Ham),
g_pfwham(FakeMeta)g_pfwfm - Inline when only used once in
get_user_msgidregister_message
- Ham钩子必须返回常量
HAM_* - FakeMeta钩子必须返回常量
FMRES_* - ReAPI钩子必须返回常量
HC_* - 消息钩子必须返回常量
PLUGIN_* - 钩子句柄变量:(Ham)、
g_pfwham(FakeMeta)g_pfwfm - 若仅在
get_user_msgid中使用一次,则内联调用register_message
Forwards
转发
- Forward names use - not
LibraryName_OnSomethingprefixFw_ - Multi-forward handle variables use prefix
g_pfw
- 转发名称使用格式 - 不要使用
LibraryName_OnSomething前缀Fw_ - 多转发句柄变量使用前缀
g_pfw
Natives & Callbacks
原生函数与回调
- Native implementations use prefix with
Native_for argumentsconst - Task callbacks use prefix with offset in task ID
Task_ - SQL callbacks use prefix
Callback_SQLQuery_{Name}
- 原生函数实现使用前缀,参数使用
Native_const - 任务回调使用前缀,任务ID中包含偏移量
Task_ - SQL回调使用前缀
Callback_SQLQuery_{Name}
Menus
菜单
- Menu callbacks use prefix
Callback_Menu_{Name} - Use enum for menu items to avoid index mistakes
- Destroy dynamic menus in callback with
menu_destroy
- 菜单回调使用前缀
Callback_Menu_{Name} - 使用枚举定义菜单项,避免索引错误
- 在回调中使用销毁动态菜单
menu_destroy
Commands & CVars
命令与CVar
- Command handlers use prefix, server commands use
Command_ServerCommand_ - Use for admin commands that should work from RCON
register_concmd - Avoid - use
client_cmdfor server-side executionengclient_cmd - Use instead of deprecated
create_cvarregister_cvar - Use instead of
bind_pcvar_*for cvar accessget_pcvar_*
- 命令处理函数使用前缀,服务器命令使用
Command_前缀ServerCommand_ - 对于可通过RCON执行的管理员命令,使用
register_concmd - 避免使用- 服务器端执行时使用
client_cmdengclient_cmd - 使用替代已废弃的
create_cvarregister_cvar - 访问CVar时使用而非
bind_pcvar_*get_pcvar_*
Optimizations
优化
- Minimize native calls in hot paths
- Use single global - never create trace handles per-call
g_pTrace - Register high-frequency hooks dynamically
- Use instead of
xs_vec_setfor vector initializationxs_vec_copy - Cache model index with static inside function, not in separate global
- 在热点路径中尽量减少原生函数调用
- 使用单个全局- 绝不要每次调用都创建追踪句柄
g_pTrace - 动态注册高频钩子
- 初始化向量时使用而非
xs_vec_setxs_vec_copy - 在函数内部使用静态变量缓存模型索引,而非单独的全局变量
Naming Conventions
命名规范
- Sound arrays use prefix with
g_rgszcounterg_i*Num - Message IDs use prefix (without underscore)
gmsg - Player arrays use with type prefix
g_rg*[MAX_PLAYERS + 1] - Don't use for players - use
idpPlayer - Don't use for entities - use
indexpEntity
- 声音数组使用前缀,搭配
g_rgsz计数器g_i*Num - 消息ID使用前缀(无下划线)
gmsg - 玩家数组使用格式,带类型前缀
g_rg*[MAX_PLAYERS + 1] - 不要用指代玩家 - 使用
idpPlayer - 不要用指代实体 - 使用
indexpEntity
Data Structures
数据结构
- Always free dynamic handles - ,
ArrayDestroyinTrieDestroyplugin_end()
- 务必释放动态句柄 - 在中调用
plugin_end()、ArrayDestroyTrieDestroy