How to change flight mode in the code

Hy there!
How is it possible with ArduPilot (I use Pixracer) to change the current flight mode in the code (after determined conditions e.g. altitude, acceleration etc.)?
Thank you!!

@WickedShell is this possible using lua scripting on ArduCopter 3.7-dev?