0

I have been learning Solidity and I have run into a scoping issue with the structs. I thought by default, all properties of a struct are put into storage. When I try to add the battles variable as an argument, it is not identified by the EVM compiler. Why is this? Thanks.

struct TargetCoreBase {
uint hp;
uint levels;
uint AlienCount;
uint TargetAliens;
uint LossCounter;
uint WinCounter;
uint battles;
}

TargetCoreBase[] target;

mapping(uint => address) base;

        address owner = msg.sender;

function CreateTarget(string _hp, uint _levels, uint _AlienCount, uint _TargetAliens, uint _LossCounter, uint _WinCounter, uint _battles) internal {
       require(msg.sender == ownerOf(_CoreBaseId));
       uint TargetId =  target.push(TargetCoreBase(_hp, _levels, _AlienCount, _LossCounter, _WinCounter, 0));
       base[TargetId];
    }


string TargetCoreBaseErr = "No CoreBase level";

function CheckTargetPairing(_battles) returns (bool) {
   if(TargetCoreBase.battles = 0) {
       return true;
   }
   else {
       return false;
   }

UPDATE

I have the right number of arguments now, but the problem still persists. In fact, replacing _battles with any of the other struct variables returns the same result of - "identifier not found".

  • Don't you get a TypeError: Wrong argument count... compiler error for the push() line? Which would then point you in the direction of flygoing's answer? – Richard Horrocks Oct 11 '18 at 17:22
0

It's not recognized because you're omitting it. Your struct:

struct TargetCoreBase {
    uint hp;
    uint levels;
    uint AlienCount;
    uint TargetAliens;
    uint LossCounter;
    uint WinCounter;
    uint battles;
}

But when you create an instance of the struct:

target.push(TargetCoreBase(_hp,        //TargetCoreBase.hp
                          _levels,     //TargetCoreBase.levels
                          _AlienCount, //TargetCoreBase.AlienCount
                          _LossCounter,//TargetCoreBase.TargetAliens
                          _WinCounter, //TargetCoreBase.LossCounter
                           0));        //TargetCoreBase.WinCounter
                                       //TargetCoreBase.battles is omitted

Common sense says that you're actually omitting the TargetAliens variable though.

0

Just to build on the previous answer...

You've got the following issues:

  • Your push() call is missing and entry for battles, or whichever parameter it is that's missing (as per flygoing's answer).
  • CheckTargetPairing() doesn't include the type of the _battles parameter.
  • In CheckTargetPairing(), the if statement isn't using the parameter you've passed in. It's trying to reference the type declaration, rather than an actual variable.
  • The same if statement is assigning a value (=) rather than making a comparison (==).
  • CreateTarget() has the wrong type for the _hp parameter (string instead of uint).

Here's something that will get you part-way there. I've no idea if the below is actually what you're aiming to achieve, but is the closest representation to what you've provided that compiles.

contract YourContract {

  struct TargetCoreBase {
    uint hp;
    uint levels;
    uint AlienCount;
    uint TargetAliens;
    uint LossCounter;
    uint WinCounter;
    uint battles;
  }

  TargetCoreBase[] target;

  mapping(uint => address) base;

  address owner = msg.sender;

  function CreateTarget(uint _hp, uint _levels, uint _AlienCount, uint _TargetAliens, uint _LossCounter, uint _WinCounter, uint _battles) internal {
    uint TargetId =  target.push(TargetCoreBase(_hp, _levels, _AlienCount, _LossCounter, _WinCounter, 0, 0));
    base[TargetId];
  }

  string TargetCoreBaseErr = "No CoreBase level";

  function CheckTargetPairing(uint _battles) returns (bool) {
    if (_battles == 0) {
      return true;
    } else {
       return false;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.