How do I detect when my custom unit receives a packet?

Started by CapeMan1008, June 20, 2021, 09:13:12 PM

Previous topic - Next topic

CapeMan1008

I don't know how to detect when my custom unit receives a packet. I suspect it might have something to do with the GetUnitSupplied command, but the wiki doesn't specify what the "supplied status" is. The unit I'm working on is supposed to be a "Battery" unit. It should request packets when you have surplus energy, and give energy when you have deficit by the time I finish coding it. Here is the unfinished code, as it cannot gain energy yet (sorry that it's not commented very much):

# --Battery-- 6/20/2021 3:57:53 PM

$$requestRate:15
$$decayRate:0.01

once
   SetUnitPacketRequestRate(<-requestRate)
endonce

# Start by getting the amount of batteries in the level currently.
GetUnitType(self) GetUnitsByType(1) GetListCount ->batteryCount

# Next, calculate energy gain.
GetEnergyGeneration GetEnergyUse - ->energyGain

# If energy gain is sufficent, request packets.
<-energyGain
if
   SetUnitRequestPackets(self TRUE)
   SetObjColor(self "LIGHT" V3(0 1 0))
else
   SetUnitRequestPackets(self FALSE)
   if(and(and(<=(GetEnergyStore 10) GetUnitConnectedToRiftLab(self) <=(<-energyGain 0))))
      SetObjColor(self "LIGHT" V3(1 0 0))
      @releaseEnergy
   else
      SetObjColor(self "LIGHT" V3(1 1 0))
   endif
endif

:releaseEnergy
   min(neg(<-energyGain) <-batteryCount /) <-storedEnergy ->releasedEnergy
   GenerateEnergy(<-releasedEnergy)
   <-storedEnergy <-releasedEnergy - ->storedEnergy



I have also tried ammo, but it doesn't work due to there being no way to disable ammo requests through 4RPL.

EDIT: I may also just be stupid and the SetUnitPacketRequestRate and SetUnitRequestPackets commands affect ammo packet requests.

Karsten75


CapeMan1008

#2
So I was being stupid, as I didn't know the SetUnitPacketRequestRate and SetUnitRequestPackets commands referred to ammo packets.

EDIT: I forgot to say thanks, so thank you for helping me realize my problem.