What's the deal with the uint8 type?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

What's the deal with the uint8 type?

Post by Player701 »

The Actor class in gzdoom.pk3 defines several fields of type uint8. For example, this data type is used for the following fields: "frame" (apparently, the current sprite frame), "movedir", and several others. However, it appears to be impossible to even declare - let alone assign to! - a local variable of this type. For example, this ZScript will make GZDoom produce an error message:

Code: Select all

version "3.4"

class Test
{
    void TestFunc()
    {
        uint8 test;
    }
}

Code: Select all

Script error, "zscript.txt:ZSCRIPT" line 7:
Invalid statement
Trying to declare a local variable of this type and immediately assign to it will produce a more interesting error message:

Code: Select all

version "3.4"

class Test
{
    void TestFunc()
    {
        uint8 test = 0;
    }
}

Code: Select all

Script error, "zscript.txt:ZSCRIPT" line 7:
Cannot initialize non-scalar variable test here
I have no idea how uint8 could be a non-scalar type, because its name suggests that it's just a single-byte number. And in fact, it really is. However, it also appears that integer literals cannot be cast to uint8, as the following script will produce another error message:

Code: Select all

version "3.4"

class Test
{
    uint8 TestFunc()
    {
        return 0;
    }
}

Code: Select all

Script error, "zscript.txt:ZSCRIPT" line 7:
Trying to cast to invalid type.
My use case is the following: I've been trying to implement a function that would return a sprite frame that is supposed to be later assigned to the calling actor. However, in addition to the above issues, it seems there is also no way to convert character literals (such as "A", "B", "C" etc.) to values of this type.

[edit] Heh, forgot the question itself. Well, it's kind of natural... What makes this type so special and what is the reason for restricting its usage?

[edit2] It appears that values of the "frame" field can be converted to characters with this simple statement:

Code: Select all

int frameChar = frame + int("A");
The reverse is also possible (just subtract int("A") instead). This also raises a question: is this an official way or is it subject to change in the future? The original question about the uint8 type still stands.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: What's the deal with the uint8 type?

Post by Graf Zahl »

You cannot use any integer type smaller than 32 bit as a local variable or return type. The internal VM registers are 32 bit wide and cannot handle smaller type.
Admittedly the error messages are not optimal. These types can only be used as struct or class fields, the VM has opcodes to load and store them but nothing to manipulate them.
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: What's the deal with the uint8 type?

Post by Player701 »

I see. I guess using int would suffice here, then. So, is the current method to convert sprite frame values to letters and back (see above) officially supported or not? I mean, if the way the current frame is stored is going to change in the future, then I'd better refrain from using it so that my code doesn't suddenly stop working correctly when a new GZDoom version is released.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: What's the deal with the uint8 type?

Post by Graf Zahl »

An uint8 is just an integer with less value space. You can use it just like an int with the only caveat that if you store a value that doesn't fit into 8 bit it will get truncated without warning.
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: What's the deal with the uint8 type?

Post by Player701 »

I know. What is more important is: currently, it appears that the value 0 stands for frame "A", value 1 stands for frame "B" and so on (at least according to my experiments). If this were to change, then the current method to convert characters to these values and back would no longer work, and existing script code which depends on it would break. I assume there are currently no plans to alter this setup.

Return to “Scripting”