Is there a built-in function to detect whether a terrain cell has a unit on it?

Started by Argonwolf, July 18, 2018, 09:05:02 PM

Previous topic - Next topic

Argonwolf

Simple enough question, is there any reasonable built-in CRPL function that tells you whether a terrain cell is occupied by a unit? I want to know for the purposes of placing objects that can't overlap existing towers/collectors etc...

If there's not a function built in, is there any simpler method for doing this than parsing all units, checking their type, populating a 2d array and manually checking your point against all the others? I'm down to do that if necessary, but I thought I'd ask first since it's going to be time consuming and probably not too efficient on the ol' CPU.

GoodMorning

A narrative is a lightly-marked path to another reality.

Argonwolf

Quote from: GoodMorning on July 19, 2018, 12:45:57 AM
https://knucklecracker.com/wiki/doku.php?id=crpl:docs:getcelloccupiedcount

This may help.

I don't recall if GetUnitAt works unless you are pointing at the middle of the unit.

This is pretty much exactly what I needed, thank you for pointing it out. Easy to lose useful functions in the walls of text in the CRPL reference section, so I didn't even see this nestled there in the unit functions.

GoodMorning

Some of us were so foolish as to read the entire page while looking for something and think "Oooh! I could use that!" a lot.

The benefit is that we can refer you to whatever it is that you need.
A narrative is a lightly-marked path to another reality.

Grabz

Quote from: GoodMorning on July 19, 2018, 12:45:57 AM
I don't recall if GetUnitAt works unless you are pointing at the middle of the unit.
`GetUnitAt` indeed will only find the unit in the middle. It's also going to find units like Ore patches which don't occupy cells, and if there's units above them (like an Ore Mine), it's only going to tell you about the ore patch that is below it, so it's completely not up to the task.

Pretty much every unit searching function only finds the middle of the unit, `GetCellOccupiedCount` is the only way to 100% reliably find out if a cell is occupied without searching through every unit, and if you need the unit UID not just if the cell is occupied, you have to search through the whole map (unless you search by range but assume a max unit size that will exist on the map).
For quicker response, reply to me directly at Grabz#4707 on Discord. Find me on the KC server: https://discord.gg/knucklecracker