Not a possible solution, I am afraid (that was also what I tried to do last night). RemovePerk will remove all ranks of the perks in one go. When that happens there isn't a way that the AddPerk function to tell the difference between rank 2 and rank 3 of the perk. The trouble comes down to rank 2 and 3 since since addperk function will return a 3 in both instances.
Since my mod is about fixing pulse explosions to actually scale, I may just ending up throwing my hands in the air and set the global variable to max if the player has even 1 rank of demolition expert (as if the player has all 3 ranks of demolition expert) with a one time run quest on start. My thinking is that if the player has one or two ranks of demolition expert, they are most likely going to eventually invest and get more ranks of demolition expert, in which case the perk will reset the global variable appropriately. For those who already have 3 ranks, they are good to go, since the quest will automatically set it to max for the first time. Since this mod will only affect Pulse grenade weapons, I don't think I have to worry about the player gaming the system for unearned damage since the damage boost only applies to pulse grenades (regular,40mm, and 25mm) as well as only affecting robots and power armors. It is funny, making the mod isn't the hard part...it is circumventing flaws in the game engine that refuses to tell me the answer to a simple question of how many ranks does the player have of one perk.
Hmm...it gets me thinking that perhaps TTW should straight up alter all perks that have ranks, to set a global variable with each rank added in case in the future the team members or other modders might want to write a script that needs to check the ranks. This is to avoid the problem that I run into now and serves as a good precaution in the future in case for whatever reason someone needs to know the ranks for a script.
