Add function AIEntityToGentity to reduce duplicate code and prevent crashes