Optimizing a Slow Unit-Type Counter

Started by Xalos, March 04, 2015, 10:21:39 AM

Previous topic - Next topic

Xalos

So I'm trying to build a map where the player has very few units, but can build more at the cost of ore in addition to the standard energy needed to build.  However, the script I'm using currently causes the framerate to gain the aerodynamic qualities of a lead anvil.  The problem is that, in order to allow players to build units at the cost of ore, I have to loop through every single unit every single frame to get its unit type - which is slow, to say the least.

I thought about using a CRPLCore "attached" to each unit and then only doing the main unit-count checker if the number of cores and previous number of units of each type doesn't match up, but wouldn't that be similarly slow?  If anyone has any ideas how to speed up the script, please let me know.

EDIT: Changed misleading topic title.

pawel345

No need for that, you can simply set unit limits and then allow the player to increase that limit for ore. Unless you mean that in case of a destruction the limit should be lowered again. In that case I'm not sure how to catch unit destruction.

Xalos

Yes; I essentially want for units to be "paid for", and if a unit is destroyed then the limit is similarly lowered, forcing players to be more intelligent about how they attack positions (for example, a pricey 50 ore lost for a destroyed nullifier, instead of just a couple energy lost).

J

You might want to take a look at abraxis.crpl from the PAC maps that checks for destroyed units. It first saves the position of all units in a list, and then checks if the cells where those units were are occupied. If not the unit is destroyed. This however gives the player the chance to put a new unit where the old unit was before the position is checked.

For you it is probably better to save the unitUID's in a list and check if they are destroyed (using the CONST_ISDESTROYED unit attribute). Then only update the list every few frames. This does not check for units built and destroyed between 2 list updates, but even if that's 60 frames you won't be able to send enough energy to it.

warren

A tricky problem, but optimizable further than what you've got. I have never tried something like this, but I assume it is only possible to create new units near where the player's mouse cursor is. This script would need to operate while paused. When a unit is created near the mouse cursor, destroy it, replace it with your ore sucking core, then recreate it when the ore sucking core is full. Just a suggestion.

Xalos

Well, players can create units in a line, which can extend far from the cursor, but you have something there.  Perhaps instead of recounting all units every single frame, I could recount units only when the player releases the mouse button.  I'd still need a way to track units being destroyed, though...

warren

#6
This was such an interesting question that I implemented a test level that sort of does this. I assume here that objects can only be created under the mouse, the threshold is one per unit type, the cost is ten ore (edit: 5) per extra unit.