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

[TUT] Finding relations between entities


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 04-05-2010 , 11:50   [TUT] Finding relations between entities
Reply With Quote #1

First of all this tutorial assumes you have knowledge of entities classes also intermediate knowledge of Pawn.
Remember that engine & fakemeta gives you tools and you should put it together to get what you want.

I’ll try to explain how to get relations between entities in a map, specifically to get entities that trigger other entities (func_button).

Entities can be triggered from another entity using it's targetname value (the name of the entity).
For that, the source entity must have it's target value with the name of the destination entity.

The following example will try to find any light entity that can be turned on/off from a func_button.

PHP Code:
// Engine way
#include <amxmodx>
#include <amxmisc>
#include <engine>
 
#define MAX_BUTTONS 10
 
new g_buttons[MAX_BUTTONS]
 
public 
plugin_init()
{
       new 
lightbutton
       
new info[32]
       new 
pos
 
       
while((pos <= sizeof(g_buttons)) && (light find_ent_by_class(light"light")))
       {
               if(!
is_valid_ent(light))
                       continue   
 
               
entity_get_string(lightEV_SZ_targetnameinfocharsmax(info))
 
               while((
button find_ent_by_target(buttoninfo)))
               {
                       if(!
is_valid_ent(button))
                               continue
 
                       if(!
find_button(button))
                       {
                               
g_buttons[pos] = button
                               pos
++
                               break
                       }
               }
       }
}
 
stock find_button(needle)
{
           for(new 
0sizeof(g_buttons); i++)
           {
                       if(
g_buttons[i] == needle)
                                  return 
1
           
}
           return 
0

Let’s see what it does.
This will find entities of type light and not more than sizeof(g_buttons) (actually 10)

PHP Code:
       while((pos sizeof(g_buttons)) && (light find_ent_by_class(light"light"))) 
Then we must check for a valid entity (if not, continue with next entity).
If it’s a valid entity, we need to get it’s name (targetname)

PHP Code:
               if(!is_valid_ent(light))
                       continue   
 
               
entity_get_string(lightEV_SZ_targetnameinfocharsmax(info)) 
Now we need to find which entity has our light found as a target

PHP Code:
               while((button find_ent_by_target(buttoninfo))) 
Again, check for valid entity and, if it’s a valid entity, we are ready to store that entity in the array and only if it’s not already stored (at bottom of the code there's a custom stock to do this work)

PHP Code:
                       if(!is_valid_ent(button))
                               continue
 
                       if(!
find_button(button))
                       {
                               
g_buttons[pos] = button
                               pos
++
                               break
                       } 

Now we have all (no more than 10) entities which handles lights in our array.


This tutorial could be used to find other kind of relations.
Sometimes, maps makers uses multi_manager entity as a tandem between activators and targets (for example when they need just one button to activate multiple lights or door or whatever)

You can find application of this in the following plugins
https://forums.alliedmods.net/showthread.php?t=123373
https://forums.alliedmods.net/showthread.php?t=122531
https://forums.alliedmods.net/showthread.php?t=120991

__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.

Last edited by joropito; 04-05-2010 at 11:53.
joropito is offline
Send a message via MSN to joropito
EXteRmiNaToR
Senior Member
Join Date: Dec 2009
Old 04-05-2010 , 12:04   Re: [TUT] Finding relations between entities
Reply With Quote #2

Nice!Reading it right now!
EXteRmiNaToR is offline
Old 04-05-2010, 13:26
xPaw
This message has been deleted by xPaw.
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 04-05-2010 , 13:34   Re: [TUT] Finding relations between entities
Reply With Quote #3

I prefer to build a list of entities at plugin_init and at Ham_Use only check if it's one of the selected entities.

In the Lights Mgmt plugin you can see that I catch kvd at precache on multi_manager, store un TrieCell and then populate the array.
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
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 22:03.


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