Counting up to n packets and using that as trigger

As DavideS mentioned, counters can be used to count events, but almost every P4 implementation I am aware of does NOT allow the data plane to read the values of counters. This is by design, because in a high speed implementation, there are tricks you can take advantage of in the hardware that make such unreadable-from-the-data-plane counters cheaper to implement than readable-from-the-data-plane counters, and such unreadable-from-the-data-plane counters are very very frequently used in production data planes.

I would recommend looking into P4 register arrays. They can be used to implement readable-from-the-data-plane counters, as well as fancier things than counters that can be read from the data plane.