Search code examples
flutterflame

function will not set variable back to false in the Bonfire game engine


I am trying to create a way to pick up objects on the map when a senor using is triggered the Bonfire game engine and make the items be put into my inventory through a function that I wrote that uses the games update feature to see if the object is picked up or not. I am having a problem where when I pick up the item and set the pickup value to true and then store it in one of my item slot variables and then after that set it back to false it will not go back to false because the function that I wrote to see if it was picked up can not set the bool back for some reason and proceeds to just copy the item into the rest of my item slots. Here is the object that I created as well as the games update function and the function that I wrote to check to see if the item is picked up.

Function that checks to see if the variable is true

  checkMagicHitForEachBox(bool itemBooleanValue, String magicName) {
    if (itemBooleanValue == true) {
      if (bottomMaigcItemSlot == null) {
        setState(() {
          itemBooleanValue = false; // This is what is broken! It will not set MagicHeart.heart back to false.
          bottomMaigcItemSlot =
              magicName; // Sets the bottom item slot to the item I picked up
        });
      } else if (topMagicItemSlot == null) {
        setState(() {
          itemBooleanValue = false; // This is what is broken! It will not set MagicHeart.heart back to false.
          topMagicItemSlot =
              magicName; // Sets the top item slot to the item I picked up
        });
      }
    } else if (topAttackItemSlot != null) { // Tells me if both are already full
      print('full');
      setState(() {
        widget.magicFull = true;
      });
    }
    print(itemBooleanValue);
  }

Function that updates every millisecond

  @override
  void updateGame() {
    checkMagicHitForEachBox(MagicHeart.heart, 'magic_heart.png');

My object that I created

class MagicHeart extends GameDecoration with Sensor {
  static bool heart = false;
  InterfaceOverlay interfaceOverlay = InterfaceOverlay();
  MagicHeart(Vector2 position)
      : super.withAnimation(
          SpriteAnimation.load(
            "magic/magic_heart.png",
            SpriteAnimationData.sequenced(
              amount: 1,
              stepTime: 0,
              textureSize: Vector2(16, 16),
            ),
          ),
          width: 16,
          height: 16,
          position: position,
        );

  @override
  void onContact(GameComponent component) {
    if (component is Player && interfaceOverlay.magicFull != true) {
      heart = true;
      removeFromParent();
    }
  }
}

Solution

  • You are changing a primitive datatype inside of checkMagicHitForEachBox, itemBooleanValue will only change within the scope of the method, not outside of it. You can solve that by passing in the parent owning the itemBooleanValue instead.

    If you want to have such a value on several classes I recommend that you create a mixin that contains the boolean value and that you can add on several classes.

    Right now you only seem to use the MagicHeart class though, so you can send that class in like this:

      checkMagicHitForEachBox(MagicHeart magicHeart, String magicName) {
        if (magicHeart.heart == true) {
          if (bottomMaigcItemSlot == null) {
            setState(() {
              magicHeart.heart = false;
              bottomMaigcItemSlot =
                  magicName; // Sets the bottom item slot to the item I picked up
            });
          } else if (topMagicItemSlot == null) {
            setState(() {
              magicHeart.heart = false;
              topMagicItemSlot =
                  magicName; // Sets the top item slot to the item I picked up
            });
          }
        } else if (topAttackItemSlot != null) { // Tells me if both are already full
          setState(() {
            widget.magicFull = true;
          });
        }
      }
    

    And last but not least, I don't think that you want your heart variable to be static, so simply remove static from that: static bool heart = false; -> bool heart = false;