Skip to content
Advertisement

Making use of a nested Array with solidity

I need to create an online game using solidity. The games have games within them with each game having their respective players. it can be likened to a battle royale on a FPS game where there are various battle royales going on simultaneously within the game with their respective participants. I tried using an array within a struct to keep record of the games. However, I have been facing error after error trying to do this.

The Struct:

JavaScript

The function to create the game:

JavaScript

Advertisement

Answer

You need to initialize the array on a separate line, and then pass it to the struct. See the _participants variable in the snippet:

JavaScript

If you want to set some participants in the code (not passed from an argument), working with dynamic array in memory is a bit tricky. See this answer for more info and an example.

Edit: To add participants to the array in a separate function, you need to store the Game in a storage variable first. See the games mapping in my update snippet. Then you can .push() into the storage array from a separate function.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement