AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   activating ents (https://forums.alliedmods.net/showthread.php?t=24958)

TiToTal 03-05-2006 09:54

activating ents
 
I love you guys, you always solve my problems ^^ ... but, there are always new questions (I am new in coding)... but I think this one is not complicated.. ^^

I searched a little but I didn't find it... How can I activate four doors with the same targetname? I tried:

Code:
    new lalala = find_ent_by_tname(-1,"door")     force_use(id,lalala)

But the problem is that only one of the 4 doors opened when the command was executed... I think all doors should open...
well, all these 4 doors have the same targetname: "door"
but, of course, different entID: "360", "361", "362", "363"
(in the case only the door 360 has been activated)

PM 03-05-2006 10:08

find_ent* functions always only return the first entity found. You have to continue calling it, most likely in a loop, in order to find all matching entities.

From find_ent_by_tname's funcwiki entry, you can see that the function takes a "StartEntity" parameter. I guess that the searching will begin on the entities after this one. It says that startentity should be -1 if you want to search from the beginning. Also, you can see that the function returns 0 if nothing is found.

So, your code would most likely work this way:

find_ent_by_tname(-1,"door")
returned 0? yes -> stop; otherwise:
force_use(id, what it returned)
find_ent_by_tname(what it returned,"door")
returned 0? yes -> stop; otherwise:
...

It's wise to use a variable for this:
Code:
new entid = -1 while (entid = find_ent_by_tname(entid, "door"))    force_use(id, entid)

As you can see, we first set entid to -1; then, we enter the while loop. Here, the condition part ( the thing inside () ) is evaluated. It sets entid to the return value of find_ent_by_tname. The value of an assignment operation is the new value, so if this will be 0, the while loop will exit. If this is nonzero, we go to the force_use line. Then the condition is evaluated again, using the last returned entid as "StartEntity". etc. This could work!

TiToTal 03-05-2006 10:35

Quote:

Originally Posted by PM
find_ent* functions always only return the first entity found. You have to continue calling it, most likely in a loop, in order to find all matching entities.

From find_ent_by_tname's funcwiki entry, you can see that the function takes a "StartEntity" parameter. I guess that the searching will begin on the entities after this one. It says that startentity should be -1 if you want to search from the beginning. Also, you can see that the function returns 0 if nothing is found.

So, your code would most likely work this way:

find_ent_by_tname(-1,"door")
returned 0? yes -> stop; otherwise:
force_use(id, what it returned)
find_ent_by_tname(what it returned,"door")
returned 0? yes -> stop; otherwise:
...

It's wise to use a variable for this:
Code:
new entid = -1 while (entid = find_ent_by_tname(entid, "door"))    force_use(id, entid)

As you can see, we first set entid to -1; then, we enter the while loop. Here, the condition part ( the thing inside () ) is evaluated. It sets entid to the return value of find_ent_by_tname. The value of an assignment operation is the new value, so if this will be 0, the while loop will exit. If this is nonzero, we go to the force_use line. Then the condition is evaluated again, using the last returned entid as "StartEntity". etc. This could work!


I understood, but I think there is something missing.. I got a error when compiling: "warning 211: possibly unintended assignment"
this warning message is for the line:
Code:
while (entid = find_ent_by_tname(entid, "door"))

isn't it possible to do it with "if" instead of "while" ?
I don't know...

PM 03-05-2006 10:41

Quote:

Originally Posted by TiToTal
I understood, but I think there is something missing.. I got a error when compiling: "warning 211: possibly unintended assignment"
this warning message is for the line:
Code:
while (entid = find_ent_by_tname(entid, "door"))

Hehe, I guess the compiler is trying to be too clever :)

I've heard of a "typical C mistake" (although I've never managed to do it myself), where you do:

Code:
if (variable = value)
instead of
Code:
if (variable == value)

ie. you assign variable to value and then check whether value is non-zero, instead of checking whether variable is equal to value.

I think the code is right as I've written it; (you can test it by running it, the warning doesn't prevent code generation). To prevent the warning, you can either try to wrap the assignment in extra parenthesis; if that doesn't silence the compiler, you could try rewriting the line of code to this:
Code:
while ((entid = find_ent_by_tname(entid, "door")) != 0)

edit:
The if instead of while question:

Well, you _could_ do this:
Code:
new entid = -1 while (true) {    entid = find_ent_by_tname(entid, "door")    if (!entid) // entid zero!      break;  // stop loop    force_use(id, entid) }

But I don't like it :D

TiToTal 03-05-2006 11:00

Quote:

Originally Posted by PM
The if instead of while question:

Well, you _could_ do this:
Code:
new entid = -1 while (true) {    entid = find_ent_by_tname(entid, "door")    if (!entid) // entid zero!      break;  // stop loop    force_use(id, entid) }

But I don't like it :D


Yeah, I definitely don't like it too... hehe


Quote:

Originally Posted by PM
I think the code is right as I've written it; (you can test it by running it, the warning doesn't prevent code generation). To prevent the warning, you can either try to wrap the assignment in extra parenthesis; if that doesn't silence the compiler, you could try rewriting the line of code to this:
Code:
while ((entid = find_ent_by_tname(entid, "door")) != 0)


Man, you're very good at it... Now, I believe the code is completely right like you, lol, and the extra "()" worked exactly as you said it would be...

It is perfect... Thanks once more... My tutor :D hehehe...

PM 03-05-2006 11:14

I'm glad that I could help :)


All times are GMT -4. The time now is 20:27.

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