Navigation

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

    Coding for fun: Improvising a type function

    Functions
    type function
    3
    4
    418
    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.
    • Chronos
      Chronos last edited by Chronos

      This is the first in a series of functions I'll be transcribing from my projects. The type function helps you tell what kind of variable something is (unfortunately haven't figured out how to distinguish sprites/handles without crashing it yet).

      Sorry if I made any transcription errors. Look forward to (de)serialization, matrix transformations, collision responses, sprite splicing, and a simple fake file system.

      function type(content)
          string s = ""
          // everything (I can distinguish) can be safely string'ed
          // crashes on handles, sprites, structs, etc
          string st = str(content)
          // check for an empty string
          if st == "" then
              s = "string"
          else
              // check for a vector or array based on the brackets
              //  make sure it isn't a string with brackets 
              //  (i.e., "{fakeout}") by seeing if the len() is 0 (a property of vectors)
              if st[0] == "{" and len(content) == 0 then
                  s = "vector"
              else
                  // if the bracket suggests it is an array,
                  //  verify that the len() for the string version 
                  //  is different than the content :
                  //  given arr = [0], len("[ 0 ]") != len(arr)
                  if st[0] == "[" and len(st) != len(content) then
                      s = "array"
                  else
                      // we established that it isn't an empty string, 
                      //  so see if it has any length because ints and floats do not.
                      if len(content) > 0 then
                          s = "string"
                      else
                          // the valuable decimal place will always appear for floats
                          if strContains(st, ".") then
                              s = "float"
                          else
                              if int(content) == content then
                                  s = "integer"
                              endif
                          endif
                      endif
                  endif
              endif
          endif
          return s
      

      Edit: It's part of a much larger package I haven't fully finished commenting yet: XFXE2MNDNS

      1 Reply Last reply Reply Quote 3
      • vinicity
        vinicity F last edited by

        Interesting! Would you mind sharing the function instead?

        1 Reply Last reply Reply Quote 0
        • Chronos
          Chronos last edited by

          Sure, I hadn't shared stuff before. Just be aware that it is also part of the fake file system and (de)serialization project I was doing, so there's a lot more stuff that I haven't added, but there are examples at the bottom for that stuff.
          ID: XFXE2MNDNS

          1 Reply Last reply Reply Quote 1
          • L
            LucasJG25 last edited by

            Your solution looks pretty solid. Another way to accomplish this would be to use structs as in

            integer = 1
            strings = 2
            floats = 3
            arrays = 4
            
            n = [.data = 123, .type = integer]
            n1 = [.data = "123", .type = strings]
            
            if n.type == integer then
              //do something
            endif
            

            Keep up the good work!!

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