Advertisement
Guest User

ut inventory

a guest
Apr 25th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. // template to access a character's inventory
  2. // this class automatically handles ignoring invalid (not fully replicated or initialized) inventory items
  3. // it is also resilient against the currently iterated item being destroyed
  4. template<typename InvType = AUTInventory> class UNREALTOURNAMENT_API TInventoryIterator
  5. {
  6. private:
  7. const AUTCharacter* Holder;
  8. AUTInventory* CurrentInv;
  9. AUTInventory* NextInv;
  10. int32 Count;
  11.  
  12. inline bool IsValidForIteration(AUTInventory* TestInv)
  13. {
  14. return (TestInv->GetOwner() == Holder && TestInv->GetUTOwner() == Holder && (/*typeid(InvType) == typeid(AUTInventory) || */TestInv->IsA(InvType::StaticClass())));
  15. }
  16. public:
  17. TInventoryIterator(const AUTCharacter* InHolder)
  18. : Holder(InHolder), Count(0)
  19. {
  20. if (Holder != NULL)
  21. {
  22. CurrentInv = Holder->InventoryList;
  23. if (CurrentInv != NULL)
  24. {
  25. NextInv = CurrentInv->NextInventory;
  26. if (!IsValidForIteration(CurrentInv))
  27. {
  28. ++(*this);
  29. }
  30. }
  31. else
  32. {
  33. NextInv = NULL;
  34. }
  35. }
  36. else
  37. {
  38. CurrentInv = NULL;
  39. NextInv = NULL;
  40. }
  41. }
  42. void operator++()
  43. {
  44. do
  45. {
  46. // bound number of items to avoid infinite loops on clients in edge cases with partial replication
  47. // we could keep track of all the items we've iterated but that's more expensive and probably not worth it
  48. Count++;
  49. if (Count > 100)
  50. {
  51. CurrentInv = NULL;
  52. }
  53. else
  54. {
  55. CurrentInv = NextInv;
  56. if (CurrentInv != NULL)
  57. {
  58. NextInv = CurrentInv->NextInventory;
  59. }
  60. }
  61. } while (CurrentInv != NULL && !IsValidForIteration(CurrentInv));
  62. }
  63. FORCEINLINE bool IsValid() const
  64. {
  65. return CurrentInv != NULL;
  66. }
  67. FORCEINLINE operator bool() const
  68. {
  69. return IsValid();
  70. }
  71. FORCEINLINE InvType* operator*() const
  72. {
  73. checkSlow(CurrentInv != NULL && CurrentInv->IsA(InvType::StaticClass()));
  74. return (InvType*)CurrentInv;
  75. }
  76. FORCEINLINE InvType* operator->() const
  77. {
  78. checkSlow(CurrentInv != NULL && CurrentInv->IsA(InvType::StaticClass()));
  79. return (InvType*)CurrentInv;
  80. }
  81. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement