AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   register_event conditions (https://forums.alliedmods.net/showthread.php?t=250695)

Aydeev 10-28-2014 19:47

register_event conditions
 
Hi,

I'm trying to fetch the weapons from kills using register_event on DeathMsg.

Since I'm not interested in all weapons I thought I'd exclude those in the event condition.

I tried this:
PHP Code:

register_event("DeathMsg""weaponcheck""a""4!usp""4!elite"

That doesn't work, it registers all DeathMsg events (including usp and elite)


If i try it the other way round:
PHP Code:

register_event("DeathMsg""weaponcheck""a""4=usp""4=elite"

That works: It only registers usp and elite frags

It seems with multiple conditions that ! is AND while = is OR. If I only use one condition ! works fine too (register_event("DeathMsg", "weaponcheck", "a", "4!usp")

I could use the = comparison and list all weapons I'm trying to fetch (which is a lot) or filter the weapons in the called function, but that seems like a waste of resources

Anyone know how to solve that?

Thanks!

Nextra 10-28-2014 20:50

Re: register_event conditions
 
Combining multiple not statements for the same parameter like that does not work. How this functions internally is that all conditions for a specific parameter are connected with or and all conditions for distinct parameters are connected with and.

In plugin pseudo-code it roughly works like this:
Code:
// Walk through all parameters of the current register_event forward for (new param = 0; param < paramc; ++param) {    new execute = 0;    new condc = get_condc(param);    /* Check each condition for the current parameter. If any one of them    applies, set the execute flag - effectively making this an *or* connection */    for (new cond = 0; cond < condc; ++cond) {       if (match_cond(param, cond))          execute = 1;    }    /* If the current parameter has any conditions, but none of them match    we exit out - so while the conditions for the current parameter are connected    with or, conditions across different parameters are connected with *and* */    if (condc > 0 && !execute)       return; } fire_forward();

Going even simpler, your first register_event basically causes this to happen internally:

Code:
if (!equal(param4, "usp") || !equal(param4, "elites")) {    // do stuff }

This will always be true, since parameter 4 will either not equal "usp" or not equal "elites" - it can't be both. The function will be executed for all weapons. The second version will result in logic equivalent to this:

Code:
if (equal(param4, "usp") || equal(param4, "elites")) {    // do stuff }

This will obviously trigger only if parameter 4 either equals "usp" or "elites" - exactly what you expect and what your tests show.

To achieve what you want you need to use two separate register_event calls. You can have them both call the same function without issues.

Great question btw :)

Aydeev 10-29-2014 07:54

Re: register_event conditions
 
Thank you for your explanations!

I'm not sure about using two register_event calls with the same function though.

Code:

register_event("DeathMsg", "weaponcheck", "a", "4!usp"
register_event("DeathMsg", "weaponcheck", "a","4!elite"

A usp frag won't trigger event 1 but then it will trigger event 2, calling the function after all.

Nextra 10-29-2014 08:15

Re: register_event conditions
 
Quote:

Originally Posted by Aydeev (Post 2217295)
Thank you for your explanations!

I'm not sure about using two register_event calls with the same function though.

A usp frag won't trigger event 1 but then it will trigger event 2, calling the function after all.

You're right. It was a little late when I wrote the post afterall.

With some extra logic you could make it work, though. Not sure if you'd want that.

Aydeev 10-29-2014 10:39

Re: register_event conditions
 
I ended up doing it like this:
PHP Code:

register_event("DeathMsg""weaponcheck""a""4!usp")

public 
weaponcheck()
{
if (!
equal(weaponName,"elite") && if (!equal(weaponName... 

Used usp in the conditin since it's the most used gun of those I want to exclude and did the rest in the function. Not very pretty but it works :)

Thanks again for your help


All times are GMT -4. The time now is 17:40.

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