Array Discussion



  • [link text](link url)
    So I've had a little Conversation with my dad which learned C, C++, Java, PhP and so on...

    He told me it should be possible to do this:

    Playerstruct = [
    .Name = "New Player" ,
    .xpos = 0,
    .ypos = 0]

    for i = 1 to 8 step 1 loop
    Array Player[i]
    Player[i-1] = Playerstruct

    Repeat

    He told me that way the script would create 8 Arrays Numerated 1-8 containing Playerstruct and he insisted on programming being easy like this.

    I told him that doesn't work, he was able to print these locally created arrays tho but he had no access to them like changing properties of the struct. He couldn't change
    Player[1].Name = "test"

    Basically I want to be able to put a struct inside an array, but as a little bonus it would be really cool if we could skip hammering these arrays into the keyboard.

    My goal is to have every player specific Variable for my project stored inside of a struct that is inside of an Array if I even need that. I don't really think I need to put them all inside Arrays, it's all about saving text.

    What would be the use of Structs in Arrays anyway?



  • If I am understanding you correctly you are trying to initialize an array where every spot is initialized to PlayerStruct. That is accomplished like this

    Playerstruct = [
    .Name = "New Player" ,
    .xpos = 0,
    .ypos = 0]

    array players[8] = PlayerStruct

    Then you can loop over every player and update what you want to.

    For i = 0 to len(players) loop
    players[i].name = "text"
    repeat

    The main reason that you would want to store structures in arrays is so that you can handle processing for multiple structures without having to repeat the same code over and over again. It is part of the DRY (dont repeat yourself) principle and is generally a good practice when working with similar data.



  • @rubiks14 that's what I call a straight answer
    Thumbs up
    My dad approved as well :D


  • Fuze Team

    Worth noting that a more explicit way of defining the struct is like so:

    struct Playerstruct
        string Name = "New Player"
        int xpos = 0
        int ypos = 0
    endstruct
    

    Also that if when entering code in the forum you put three "backticks" before and after the block of code the forum will nicely format it as a block. For a single line or a few words, use a single backtick.

    PS: In case there are any syntax issues with my code above, or case, I'm not near my Switch at the moment.



  • @martin oh yes I actually just read about that section in the tutorials. It looks like when using that method you can actually declare an array of a struct by simply typing:

    struct Playerstruct
        string Name
        int xpos
        int ypos
    endstruct
    
    Playerstruct players[4]
    

    that is very handy



  • @rubiks14

    So if I type:

    ///

    Playerstruct = [
    . Name = "new name ",
    . Xpos = 0,
    . ypos = 0]

    Players[7] = Playerstruct

    ///

    Don't I need to type array before initializing?

    Is the Player[1].Name exclusive to player 1?
    Or does a change in Playerstruct of player 1 change the Playerstruct for player 2?



  • You will need the array keyword when you declare your array otherwise fuze will think that you are trying to edit the 7th element of an array that does not exist and throws an error

    If you change the name as you have above then it will only change the name of the structure at players[1].


  • F

    @Mechanical Probably you found out by now. If not, I just tested it (to be honest ;-) )
    If you leave the array keyword away, the interpreter will tell you that it does not know about players, So, as you mentioned, this works:
    array players[7] = playerDefault
    or if you have a type Playerstruct defined like @rubiks14
    Playerstruct players[7] (here you have to fill in the data later)
    And its initiated multiple times, its not one reference copied to all 7 player elements: means their are changeable independently of each other.
    Its different with a simple struct:

    a = [.x=1, .y=2]
    b = a
    a.x = 3
    print(b.x)
    

    This prints 3. The reference to a is copied into b, and they point to the same data afterwards (I guess).

    Ah, before I forget: look at the video tutorials about structures, they clarify things very well: https://fuzearena.com/forum/category/6/tutorials


Log in to reply