Author Topic: Expression 2 - Arrays.  (Read 6262 times)

Offline Nicknero

  • Hatsune Miku
  • Gold Member
  • *
  • Posts: 5,032
  • Awards [OCRP Award] Millionaire [OCRP Award] Evo City Marathon [OCRP Award] Social Player [GMR2 Award] 100 MPH
    • View Profile
    • Awards
Expression 2 - Arrays.
« on: July 20, 2011, 07:26:06 PM »
Right, so I am pretty good with Expression 2, and I've made myself a very awesome E2 based turret. This thing only requires the E2 itself, 1 prop with a turret, and the turret wired to the E2 of course.

Now that everything is working, I added a feature to target all the players OR npcs on the map, starting from the closest from the turret.
However, the problem is that if a player or NPC dies, his entity takes a couple of seconds to disappear. This causes the speed of switching target to be VERY slow. For this, I TRIED to add a feature to check if the target is still alive. If not, it would skip to the second target. This is howerver where the problem comes in effect, as I can easily use findResult(2) for the second target, but then the same problem appears for the third target etc. (Seeing as the first target's entity is still not gone.)

Here is a tiny piece of code I wrote in attempt to use a value (A) to go to the next target.
Code: [Select]
elseif(All==2){
    findByClass("npc_*")
    findSortByDistance(E:pos())
    Target = find()
    if (Target:isAlive()){A = 1}
    if (!Target:isAlive()){A = A + 1       Target = findResult(A)}

    E:setColor(randint(0,255),randint(0,255),randint(0,255),255)
}
E = the prop with the turret attached to it.
Target = obviously the target.

However, I believe it would be much easier to use arrays in this case.
Target = Array[A,entity]
Then make somehow call A from the current target, add 1 to it to skip to the next target, but here is the problem again: HOW do you call A. Aka how do you call the array number of the current findResult.

I really hope the smart E2ers out there understand what I'm talking about, and know what my goal is, hence might help me out a bit, as I'm out of options D:
Check out my Youtube channel with all kinds of cool videos including LP's which I'm currently working at. Don't forget to give feedback as well. ;)


Offline Somone77

  • Developer
  • *
  • Posts: 652
    • View Profile
    • Awards
Re: Expression 2 - Arrays.
« Reply #1 on: July 20, 2011, 07:36:44 PM »
Could you post the entire code, please?

Your issue is right here:
Code: [Select]
    if (Target:isAlive()){A = 1}
    if (!Target:isAlive()){A = A + 1       Target = findResult(A)}

You're not thinking correctly in terms of how an E2 is run.
You're saying that, when this target dies, add 1 to A, then switch the target. But then, you change A back to 1, this I don't understand. What is A?


Now that I read it a bit more I think there is a better way to do it. I haven't much experience with find functions but you shouldn't have to be checking if it's alive if that part of the code is only run when the NPC is dead. In fact, I believe you could just remove this line:
if (Target:isAlive()){A = 1}

As long as A is a persistent variable.


Wait I'm not thinking right, you're running the find function again. You shouldn't need to do that more than once unless you expect that the amount of entities getting returned it going to change between execution. Trying adding a changed() function in there.
« Last Edit: July 20, 2011, 07:44:36 PM by Somone77 »

Offline Nicknero

  • Hatsune Miku
  • Gold Member
  • *
  • Posts: 5,032
  • Awards [OCRP Award] Millionaire [OCRP Award] Evo City Marathon [OCRP Award] Social Player [GMR2 Award] 100 MPH
    • View Profile
    • Awards
Re: Expression 2 - Arrays.
« Reply #2 on: July 20, 2011, 07:42:51 PM »
Could you post the entire code, please?

Your issue is right here:
Code: [Select]
    if (Target:isAlive()){A = 1}
    if (!Target:isAlive()){A = A + 1       Target = findResult(A)}

You're not thinking correctly in terms of how an E2 is run.
You're saying that, when this target dies, add 1 to A, then switch the target. But then, you change A back to 1, this I don't understand. What is A?

Hmmm yeah I didn't thought about that.
A is just a value to replace the number in findResult(NUMBER)
I thought that if the first target dies, it should add 1 to A thus targeting the next target. But of course when it does that, the target is alive thus resetting A back to 1 D:
However, if the first target's entity finally disappears, then the chance is there that it skips an alive entity, thus breaking the entire function.

The entire idea with A is just bad.... I want to do it with arrays, but I have no idea how they work. I have an example array in my code which is easy to understand, but it works quite different:
Code: [Select]
Entities=E:getConstraints()

Entities[1,entity]:setMass(0.00001)
Entities[2,entity]:setMass(0.00001)
Entities[3,entity]:setMass(0.00001)
Entities[4,entity]:setMass(0.00001)
Entities[5,entity]:setMass(0.00001)
(This is to change the mass of 5 props welded to the prop using as turret to 0.00001)
Check out my Youtube channel with all kinds of cool videos including LP's which I'm currently working at. Don't forget to give feedback as well. ;)


 

SimplePortal 2.3.7 © 2008-2024, SimplePortal