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

CapeMan1008 · 535

CapeMan1008

  • Newbie
  • *
    • Posts: 4
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):

Code: [Select]
# --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.
« Last Edit: June 21, 2021, 05:09:40 pm by CapeMan1008 »



Karsten75

  • Hero Member
  • *****
    • Posts: 7224
Re: How do I detect when my custom unit receives a packet?
Reply #1 on: June 21, 2021, 08:44:35 am
« Last Edit: June 21, 2021, 08:46:08 am by Karsten75 »

'If you want to live a happy life, tie it to a goal, not to people or things.'

Albert Einstein
(1879-1955)


CapeMan1008

  • Newbie
  • *
    • Posts: 4
Re: How do I detect when my custom unit receives a packet?
Reply #2 on: June 21, 2021, 05:12:27 pm
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.
« Last Edit: June 21, 2021, 06:20:49 pm by CapeMan1008 »