Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Can structs be instantiated?

    Help
    3
    6
    264
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • _
      _JKDOS F last edited by

      Sort of like

      person = [
          .name = "John Doe"
          .age    = 43
          .sex    = "male" 
      ]
      
      person personA
      person personB
      
      personA.name = "Jimmy"
      personB.name = "Sally"
      
      print(personB.name," is married to ",personA.name)
      // returns Sally is married to Jimmy
      print(person.name," is just an average guy")
      // returns John Doe is just an average guy
      N 1 Reply Last reply Reply Quote 0
      • N
        Nisse5 F @_JKDOS last edited by

        @_JKDOS Yes:

        struct person
            string name = "John Doe"
            int age = 43
            string sex = "male"
        endstruct
        
        _ 1 Reply Last reply Reply Quote 0
        • _
          _JKDOS F @Nisse5 last edited by

          @Nisse5 said in Can structs be instantiated?:

          @_JKDOS Yes:

          How about the instance though?

          N 1 Reply Last reply Reply Quote 0
          • Martin
            Martin Fuze Team last edited by MikeDX

            PersonA and PersonB should work just as you’re expecting provided you use the form of struct definition used by @Nisse5 In this case you cannot use the base struct directly with just person, it has to be an instance

            Edit: Now I'm on a computer, I can be a bit clearer!

            This works (note the commas):

            person = [
                .name = "Bob",
                .age = "20",
                .sex = "M"
            ]
            
            loop
                clear()
                print(person)
                update()
            repeat
            

            Or this works

            struct person
                string name = "John Doe"    // default value
                int age = 20
                string sex = "M"
            endstruct
            
            person personA
            personA.name = "BoB"
            
            loop
                clear()
                print(personA)
                update()
            repeat
            

            (ignore the {1} I don't know where that is coming from)

            But not a mix of both.

            Personally I find the first use awkward and confusing but its probably just because I'm used to OO programming these days.

            _ 1 Reply Last reply Reply Quote 0
            • N
              Nisse5 F @_JKDOS last edited by

              @_JKDOS It seems like it's only possible to access variables created from structs.

              1 Reply Last reply Reply Quote 0
              • _
                _JKDOS F @Martin last edited by

                @Martin @Nisse5 Thank you both. Now that I am off work I've had the chance to test them out. Works well. Loving it.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post