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

[API] Custom Entities


Post New Thread Reply   
 
Thread Tools Display Modes
Plugin Info:     Modification:   ALL        Category:   Technical/Development       
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 11-03-2016 , 08:18   [API] Custom Entities
Reply With Quote #1


Custom Entities API

Description

This API help you create some custom entities for your mod with some presets like models, size, etc.
It's can be used to create gameplay entities, items, props.
Create custom entity on your map
Just create entity with custom classname, which registered in CE API
Create custom entity

PHP Code:
new ceEnt CE_Create(szClassnamevOrigin);
if (
ceEnt) {
    
dllfunc(DLLFunc_SpawnceEnt);


Remove custom entity correctly

PHP Code:
CE_Remove(ent); 
API

PHP Code:
#define CE_LOG_PREFIX "[CE]"
#define CE_BASE_CLASSNAME "info_target"

enum CEPreset
{
    
CEPreset_None 0,
    
CEPreset_Item// For items
    
CEPreset_NPC// For NPC
    
CEPreset_Prop // For static props
};

enum CEFunction
{
    
CEFunction_Spawn// Call when entity spawned
    
CEFunction_Kill// Call when some plugin try to kill entity. return PLUGIN_HANDLED to discard kill.
    
CEFunction_Killed// Call when entity killed
    
CEFunction_Remove,    // Call when entity removed
    
CEFunction_Picked// Call when player pick item
    
CEFunction_Pickup// Call when player touch item. Should return PLUGIN_HANDLED if picked.
    
CEFunction_KVD // Call when new key value obtained
};
/*
 * Register entity.
 *
 * @param szName            Name of entity.
 * @param modelIndex        Precached model index.
 * @param size                Size of entity.
 * @param offset            Offset of entity origin.
 * @param lifeTime            Life time of entity.
 * @param preset            Preset for entity.
 * @return                    Handler of registered entity.
 */
native CE_Register
(
    const 
szName[],
    
modelIndex 0,
    const 
Float:vMins[3] = {-8.0, -8.0, -8.0},
    const 
Float:vMaxs[3] = {8.08.08.0},
    
Float:fLifeTime 0.0,
    
Float:fRespawnTime 10.0,
    
bool:ignoreRounds false,
    
CEPreset:preset CEPreset_None
);

/*
 * Spawn entity.
 *
 * @param szName            Name of entity.
 * @param vOrigin            Spawn origin.
 * @return                    Entity index.
 */
native CE_Create(const szName[], const Float:vOrigin[3], bool:temp true);

/*
 * Kill entity.
 *
 * @param ent                Index of entity.
 * @param killer            Index of killer.
 */
native bool:CE_Kill(entkiller 0);


/*
 * Gets size of entity.
 *
 * @param szClassname        Classname of entity.
 * @param vSize                Output vector.
 */
native CE_GetSize(const szName[], Float:vMins[3], Float:vMaxs[3]);

/*
 * Gets modelindex of entity.
 *
 * @param szClassname        Classname of entity.
 * @return                    Modelindex of entity
 */
native CE_GetModelIndex(const szName[]);

/*
 * Remove entity correctly.
 *
 * @param ent                Index of entity.
 * @return                    Result true/false
 */
native bool:CE_Remove(ent);

/*
 * Register new hook for entity.
 *
 * @param function            Function handler
 * @param szClassname        Classname of entity
 * @param szCallback        Callback
 */
native CE_RegisterHook(CEFunction:function, const szClassname[], const szCallback[]);

/*
 * Check if entity is associated with current plugin.
 *
 * @param ent                Index of entity.
 * @return                    Result true/false
 */
native CE_CheckAssociation_(ent);

stock bool:CE_CheckAssociation(ent) {
    static 
bool:notified false;
    if (!
notified) {
        
log_amx("%s function ^"CE_CheckAssociation^" is deprecated. Check ^"CE_GetHandlerByEntity^" function."CE_LOG_PREFIX);
        
notified true;
    }
    
    return 
CE_CheckAssociation_(ent);
}

native CE_GetHandler(const szClassname[]);
native CE_GetHandlerByEntity(ent); 
CE_SPAWNER

Custom Entity Spawner.

Spawner KVD
  • ce_name (string) - Entity to spawn
  • delay (float) - Delay before spawn (0.0 - spawn once per round)
  • impulse (float) - Max velocity for entity after spawn.


Attached Files
File Type: sma Get Plugin or Get Source (api_custom_entities.sma - 186 views - 22.2 KB)
File Type: inc api_custom_entities.inc (3.0 KB, 108 views)
File Type: sma Get Plugin or Get Source (entity_ce_spawner.sma - 1206 views - 5.3 KB)
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 11-17-2022 at 10:05.
Hedgehog Fog is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 11-03-2016 , 08:20   Re: [API] Custom Entities
Reply With Quote #2


Examples

Super Healthkit

PHP Code:
#pragma semicolon 1

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>

#include <api_custom_entities>

#define ENTITY_NAME "item_super_healthkit"

#define MAX_HEALTH 100.0
#define HEAL_AMOUNT MAX_HEALTH

new g_szSndItemSpawn[] = "fvox/beep.wav";
new 
g_szSndItemPickup[] = "items/smallmedkit1.wav";
new 
g_szSndItemDisappeared[] = "fvox/buzz.wav";

new 
g_ceHandler;

public 
plugin_precache()
{
    
precache_sound(g_szSndItemSpawn);
    
precache_sound(g_szSndItemPickup);
    
precache_sound(g_szSndItemDisappeared);

    
//Register new entity
    
g_ceHandler CE_Register(
        .
szName ENTITY_NAME//classname
        
.modelIndex precache_model("models/w_medkit.mdl"), //modelindex
        
.vMins Float:{-12.0, -12.00.0}, //min size
        
.vMaxs Float:{12.012.024.0}, //max size
    
.fLifeTime 10.0//lifetime of entity(0.0 to permanent)
    
.fRespawnTime 25.0// respawn delay
        
.preset CEPreset_Item //preset for entity
    
);
    
    
//Register spawn hook.
    
CE_RegisterHook(CEFunction_SpawnENTITY_NAME"OnSpawn");

    
//Register pickup hook.
    
CE_RegisterHook(CEFunction_PickupENTITY_NAME"OnPickup");

    
//Register picked hook
    
CE_RegisterHook(CEFunction_PickedENTITY_NAME"OnPicked");

    
//Register kill hook.
    
CE_RegisterHook(CEFunction_KilledENTITY_NAME"OnKilled");
    
}

public 
plugin_init()
{
    
register_plugin("[Custom Entity] Item Super Health Kit""1.0.0""Hedgehog Fog");

    
//Register Think Hook for custom entity
    
RegisterHam(Ham_ThinkCE_BASE_CLASSNAME"OnThink");
}

public 
OnSpawn(ent)
{
    
//Some effect for item
    
set_pev(entpev_rendermodekRenderNormal);
    
set_pev(entpev_renderfxkRenderFxGlowShell);
    
set_pev(entpev_renderamt4.0);
    
set_pev(entpev_rendercolor, {255.0255.0255.0});
    
    
//Emit spawn sound
    
emit_sound(entCHAN_BODYg_szSndItemSpawnVOL_NORMATTN_NORM0PITCH_NORM);
}

public 
OnPickup(entid)
{
    
//Get player health
    
static Float:fHealth;
    
pev(idpev_healthfHealth);
    
    if (
fHealth MAX_HEALTH) {
        
//Can be picked
        
return PLUGIN_HANDLED;
    }

    
//Can not be picked
    
return PLUGIN_CONTINUE;
}

public 
OnPicked(entid)
{
    
//Get player health
    
static Float:fHealth;
    
pev(idpev_healthfHealth);

    
//Add health to player
    
fHealth += HEAL_AMOUNT;
    if (
fHealth MAX_HEALTH) {
        
fHealth MAX_HEALTH;
    } 
set_pev(idpev_healthfHealth);

    
//Emit pickup sound
    
emit_sound(entCHAN_VOICEg_szSndItemPickupVOL_NORMATTN_NORM0PITCH_NORM);
}

public 
OnKilled(entbool:picked)
{
    if (!
picked) {
        
//Emit disappear sound
        
emit_sound(entCHAN_VOICEg_szSndItemDisappearedVOL_NORMATTN_NORM0PITCH_NORM);
    }
}

public 
OnThink(ent)
{
    
//Check if custom entity is associated with current plugin
  
if (g_ceHandler != CE_GetHandlerByEntity(ent)) {
        return;
    }

    
//Make something here

__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 08-21-2017 at 06:55.
Hedgehog Fog is offline
gabuch2
AlliedModders Donor
Join Date: Mar 2011
Location: Chile
Old 11-03-2016 , 08:30   Re: [API] Custom Entities
Reply With Quote #3

???? Last time I checked item_medkit existed in (most)all mods. Including CS.

I know that's just an example, but still.
__________________
gabuch2 is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 11-03-2016 , 08:34   Re: [API] Custom Entities
Reply With Quote #4

Shattered Heart Lynx
If you interested in more complex examples:

https://forums.alliedmods.net/showthread.php?t=273992
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities
Hedgehog Fog is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 11-03-2016 , 17:23   Re: [API] Custom Entities
Reply With Quote #5

I like this, but all entities can move/attack players?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 11-03-2016 , 17:52   Re: [API] Custom Entities
Reply With Quote #6

EFFx
If you talking about npc preset, it's just a basics setups for npc (private data) like movetype, solidtype, takedamage etc.
If you wanna make something like npc, you need to add hook for think, make first think in spawn event for entity and set pev_nextthink every think event.
In think event you need make some npc logic, like target finding and attack.
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 11-03-2016 at 18:05.
Hedgehog Fog is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 11-03-2016 , 20:47   Re: [API] Custom Entities
Reply With Quote #7

And what you think if you add this on your Custom Entities? Only making entities have no sense for me, whats your opinion about this?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 11-03-2016 at 20:47.
EFFx is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 11-03-2016 , 22:32   Re: [API] Custom Entities
Reply With Quote #8

EFFx,
I have no idea what do you need.
It's can be helpful for complex project (Halloween Mod for example).
This allow to you register new entity class and use this class on your map for example.
This allow you to controll entity remove and clear some data, for example arrays, which binded to entity and store in pev_iuser1 as handlers.
It's remove entities automatically in new round.
It's allows you to make temp entities with life time.
It's allows you to hook pickup events, it's can be usefull if you have a lot of item entities, so you don't need to create forward for each item.
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 11-04-2016 at 10:17.
Hedgehog Fog is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 11-04-2016 , 16:08   Re: [API] Custom Entities
Reply With Quote #9

Quote:
Originally Posted by Hedgehog95 View Post
EFFx,
It's remove entities automatically in new round.
And if I don't want that? Any cvar?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 11-04-2016 at 16:08.
EFFx is offline
Hedgehog Fog
Senior Member
Join Date: Jun 2010
Location: Ukraine
Old 11-04-2016 , 16:38   Re: [API] Custom Entities
Reply With Quote #10

EFFx
PHP Code:
/*
 * Create entity.
 *
 * @param szName           Name of entity.
 * @param vOrigin          Spawn origin.
 * @param temp             Remove entity in next round if true.
 * @return                 Entity index.
 */
native CE_Create(const szName[], const Float:vOrigin[3], bool:temp true); 
__________________

❄️ CS Snow Wars - Mod based on snowballs fights
🧟 CS Zombie Panic - A port of HL Zombie Panic!
🎃 CS Halloween Mod - Completely new gamemode for Halloween Holidays
📦 AMXXPack - CLI and build system for amxx projects
🔧 Custom Entities API - API to register custom entities

Last edited by Hedgehog Fog; 11-04-2016 at 16:55.
Hedgehog Fog is offline
Reply


Thread Tools
Display Modes

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 17:00.


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