I'm a Python Developer jumping into Solidity and I feel like I'm missing something about variables and scope.

How do I set and access variables outside of a function's scope?

I can't seem to add spins ( simple uint ) to my spin history ( uint[] ) and access them elsewhere. I can use push and get it in an array within the same function, but it doesn't update the State level variable.

contract GoldenEgg{
uint[] public spinHistory;

function spin() constant returns (uint) {
    uint spin = uint8(uint256(keccak256(block.timestamp, block.difficulty))%37);
    return spin;

function getHistory() constant returns (uint[]) {
    return spinHistory;

function addToHistory( uint spin ) constant returns (uint[]) {
   return spinHistory;

spin() is marked constant (a deprecated alias for view). So it can't change any state. When you call a view function, instead of sending a transaction to the network, you just ask one node (the one you're connected to) to run the code and tell you the result. No changes are persisted or stored in the blockchain.

If you remove constant, you'll send a transaction instead when you call that function. It will take longer to execute and won't actually return you a result, but the state changes you make will persist.

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.