Here's my sample contract code.
contract SimpleStore {
uint public BetRound;
uint public SelectedUser;
struct Struct {
uint _round;
address _owner;
uint _index;
bool _active;
}
mapping(uint => Struct) public userIndexStruct;
uint[] public userArray;
function add(address _address) public {
Struct storage accounts = userIndexStruct[userArray.length];
accounts._round = BetRound+ 1;
accounts._owner = _address;
accounts._active = true;
accounts._index = userArray.length;
userArray.push(accounts._index);
}
function update(uint _index) public {
userIndexStruct[_index]._round++;
}
function SetUsers() public returns (uint) {
// If we reached the end of our array
// it should mean we looped all users already
// We need to increment the BetRound
if(SelectedUser >= userArray.length ) {
BetRound++;
SelectedUser = 0;
}
for (uint i = SelectedUser; i < userArray.length; i++) {
if (userIndexStruct[i]._round < BetRound) {
if (userIndexStruct[i]._active) {
// This is the user who is next to receive a reward.
// Send rewards to the user, and then update his round.
update(i);
SelectedUser = i;
// Return what user was selected
return i;
}
}
}
// Our loop finished without result, we should move to the next round.
BetRound++;
}
}
Whenever i add a user to my struct, the first account index is 0.
If i verify this by entering index 0 in my userIndexStruct
and the userArray
, it returns the correct address.
However, every time my SetUsers
loop hits 0
, it returns address 0x0
, and the next loop it returns the actual stored index[0]
address.
Could someone please explain what's happening, and why it Always returns the 0x0 address when my loop begins with 0, instead of giving the correct address for my users stored in the struct with index 0?
Thank you!
Solved!
I simply forgotten to check array.length -1. Facepalm.
Thank you all for the time and effort!