Shop Learn
How to declare a fixed length string — Parallax Forums

How to declare a fixed length string

I'm trying to figure-out how to declare a fixed length string in 5.9.9. What I'm after is something like this:

class tempType
   Var1 as ulong
   Var2 as ushort
   Var3 as string(128)
end class
dim NewType as tempType
NewType.Var3 = "This is a test"
print NewType.Var3

The goal here is to always have 128 bytes reserved for Var3. Ideally I'd also like it never to be garbage-collected... just a static array of bytes that "looks like" a regular string. Any thoughts about how to make this work?


  • msrobotsmsrobots Posts: 3,540

    how about byte(128) ?


  • pik33pik33 Posts: 1,512
    edited 2022-04-13 10:51

    You can alias the string as a pointer to the ubyte array, but then you cannot assign to it as every assign creates a new string with a new pointer. In the class you can write your own methods for such a "string"

    dim a(128) as ubyte
    dim b as ubyte pointer
    declare a$ alias b as string
    print a$
    print a(0)

    The result:


    After aliasing a$ as the pointer to a and updating a, you can print a$.
    After updating a$ the pointer to a$ is no longer the pointer to a, so after assigning the new value, the pointer also changed (and was allocated, etc...)

    And string(128) is an array of 128 strings (= 128 pointers)

Sign In or Register to comment.