Talk:How do I destroy all the NPCs on a single map?
Bob: A faster, and even simpler (but perhaps less intuitive?) version of the same script would be:
define script(autonumber, destroy npcs, none) script, destroy npcs, begin variable(npc ref) # NPC references are negative numbers (NPC IDs are positive) for(npc ref, -1, -300, -1) do, begin destroy npc(npc ref) end end
Mike: Well, the speed of my script depends on how many npcs there are. If there's no NPCs, it will be very quick. If the map is full of NPCs, it will be roughly twice + a little bit slower than yours. Mmm... no, not twice, maybe 1.8 times slower, since looking up the npc references would be faster in compil... eh... I digress.
Hmm, maybe if we had a "npc count" command, we could have it choose which script to use depending on how many NPCs there are. But, then that would further complicate the script and confuse people, so I think my script is best, especially since I don't think it is the type of thing to be called in a tight loop. (In other news, I'm bored)
FyreWulff I would think James' version of my script would be faster, because calling npc copy count every time you go around the loop would be slower than just destroying NPC references outright.
Mike: Perhaps, but if there's ony 30 NPCs, it only needs to loop a couple times. no matter which way you slice it, ~30-40 iterations will be faster than 300, even if the smaller one does more.
FyreWulff yes, but what if there are 200 copies of each NPC? that would take forever. general use scripts like this should work in the fastest possible way, and in this case, it's brute force. reverting to James Paige version, your for loop is broken anyway
TMC: (Firstly, Mike's code was broke, destroy npc(npc reference(npc id, npc copy -- 1)) should be destroy npc(npc reference(npc id, 0))
James' script is 300 fast iterations in script. Mike's is 36 + num npcs somewhat slower iterations in script, and 10500 + 300 * num npcs iterations in BASIC (assuming you are deleting npcs created with scripts, else that 300 could be near 0). I don't really know just how much slower HS is than BASIC, but I'd guess James' would be faster for 30 NPCs on map.
Bob: Well, we could actually compile the two versions of the script, and test them on maps with various numbers of NPCs. A real test seems better than guessing.
Mike: Anyone else think this is a stupid and pointless discussion about something that is, ultimately, irrelavent. I mean, the only way the speed of a script like this would ever affect anything (since, either way, all the scripts are still pretty fast) is if you called this script frequently. And, if you're doing that, it means that you're also creating more NPCs at the same time. Which, of course, is all fine and good. But.
The pointless part of this is that for the speed of this to matter, you'd have to call it more than, say, 5 times a second, or every 1/5th of a second. NPCs, therefore, would only be visible for 1/5th of a second. Not long enough for the NPCs to do anything useful, much less be seen by the player.
I imagine that any usage for this script would be at an "end of level" type event, which would only happen occasionally, and not nearly often enough to affect anything.
Either way, as James mentioned, his script is more confusing than mine. And, since the only people who would need this script created for them would be those who aren't ultra-plotscripters anyway, confusion is not good.
Bob: Yes, the speed difference is unlikely to matter one way or another. I am nonetheless still curious about exactly direct testing of how big that speed difference is.
TMC: I don't care much about the speed of this script, I'm looking at npc manipulation and writing fast scripts in general. Plotscripting isn't particularily slow, but the interpreter can't take much stress before slowing down the game (with QB at least) either.
I've guiltily only ever done a couple speed tests to justify years of speculation on script optimisation :P I think I'll go do some timing now.
Ok, I added a command to print TIMER to g_debug, modified destroynpc to not actually delete npcs, and timed 1000 map wipes on my 1.5GHz P4 on both QB and FB. I ran FB windowed. Times in seconds:
NPCs | NPC Copy Count - Mike | Raw NPC references - James | ||
---|---|---|---|---|
- | QB | FB | QB | FB |
1 placed in CUSTOM | 12.8 | 0.53 | 28.7 | 2.11 |
8 placed in CUSTOM | 15.4 | 0.64 | 28.9 | 2.17 |
30 " " " | 21.6 | 0.98 | 28.8 | 2.19 |
30 script created | 21.8 | 0.91 | Couldn't be bothered | No record |
30 CUSTOM + 70 PS | 38.1 | 1.83 | No record | No record |
300 | 83.4 | 4.72 | No record | No record |
(Ok, I went a little overboard)
So, I a little optimistic on PS speed. I'm not really sure how time in the npc copy count code was split between interpreting and the loops in npc reference and copy count. I'm not sure whether that's what I care about either. Uhh...
Bob: Interesting. I am surprised at how much faster FB is. That is cool. It looks like My version of the script is faster when the map is full of NPC's, but that Mikes version is faster when the map is not full. Since the slowest possible case of a single iteration of Mikes version (QB with a map full of NPC's) would a only take like 83 milliseconds, I'm going to say his version is better. Maybe if a future version increases NPC support to 5000 per map or something we will want to reconsider-- but for now, his version is better.
FyreWulff: wow, fb blows qb out of the water. 20 second gap?!?! Perhaps if there was a general command to check the # of NPCs, you could switch into the script that would go faster. But at that point you might be better off making the script native inside the engine. Also, slightly off-topic, but we really should be commenting using the three-tildes technique, it's confusing when people like Mike Caron use Mike as their name on comments, because not only do I think I'm reading comments from myself, but people won't know who to talk to (I've already had people ask me why I comment under two different names on this page)
Bob the Hamster Oooh! Nice! I did not know that the three tilde's technique existed! That is very convenient!
The Mad Cacti- Oh, I've just been using bold type all this time to stay consistant with everyone else's comments :P
Mike C. Is there really that big of a difference between script-created npcs and those placed in custom? I mean, other than the fact that they would be at opposite ends of the npc reference spectrum, but that shouldn't matter at all.
Either way, I'm curious to know exactly when my script becomes worse.
Two and a half years later...[edit]
Mike C.: I wonder if any speed improvements in the engine would affect this at all.
Continuing from my previous comment about when each script is optimal, it's clear that this is simply a case of linear equations. James script can be defined by:
LET Y be the running time, and X be the number of NPCs
Y = 2.15
Or, constant time. Mine, however, is not constant. I am going to guess that it looks something like:
Y = 0.014X + .5
This holds up better at the higher end, but it really depends on which NPCs are placed. The worst case scenario for my script is that one of each NPC ID is placed. The best case is that all NPCs are of the same ID.
Let's assume my equations are correct, and do the math:
Y = 0.014X + .5 (2.15) = 0.014X + .5 2.15 - .5 = 0.014X 1.65 = 0.014X (1.65 / 0.014) = X 117.9 = X
So, under these assumptions, my script is faster with less than 118 NPCs, while James is faster with 118 or more.
Oh, hey, this script is obsolete anyway. With 100 NPC IDs, now, the script has more overhead, which affects the timing...