ZScript classes

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.

ZScript classes are similar to C++ classes which may contain a variety of data.

Internal classes

Below is a list of internal classes used in ZDoom. Classes inheriting from other classes use a colon, much like how DECORATE performs inheritance. These classes are only accessible to ZScript itself (save for Actor)

Cross-Mod Support

Currently there is not much ability to check between loading different mods on top of primary ones (like addons), but there is a way to check if a class exists.

// Checked at compile time. This will error upon loading if the class is missing.
class<Actor> cls = "MissingNameOfClass";
// Checked at run time instead of compile time. 'cls' will just be null if the actor class isn't defined.
string classname = "MissingNameOfClass";
class<Actor> cls = classname;

Class names are checked at compile time by default. However, using a string in place of a name forces the engine to rely upon run-time instead. A simple check will allow compatibility for addons:

if (cls)
{
    A_SpawnItemEx(cls,...);
}

Modifiers

Certain words may appear after the name of a class and (if any) its parent class. This example uses the abstract modifier:

class SomeClass : Actor abstract
{}

abstract

Marks a class as abstract, which means it can only be used as a parent class, and cannot be spawned or instantiated directly.

// Trying to instantiate with new('One') is a compile error.
class One abstract
{}

// But new('Two') is allowed, because it's not abstract.
class Two : One
{}

This can be used on classes only, not structs. Abstract classes also allow you to define abstract functions.

A class pointer can be checked for abstraction using IsAbstract() like such:

static bool CheckAbstract(Class<Object> obj)
{
	return (!obj || obj.IsAbstract());
}


play, ui, data

By default new objects are marked as belonging to the data scope (data can also be specified explicitly, but if nothing is specified, it's considered data-scoped implicitly). If the object is a class based on another class, it'll inherit its scope as well.

play or ui mark the object as belonging to play or UI scope respectively.

Can be used on both classes and structs.

Extending Classes

Classes may be extended, but the class must be part of the same mod. I.e. one can have extensions occur in a separate file within the archive, but not a separate archive itself. I.e. extending class Actor is impossible since it's defined within gzdoom.pk3 archive. Any attempt to extend a class in another archive file will result in an error at compile time.

Similarly, attempting to extend a class before its definition is read will also fail. Also, due to the way the #include directive works (adding the file to a list of files to be compiled), extending a class that is on a deeper level of inclusion than the extension results in an error on start-up, as the extension is then read before the class.

Extending a class allows extra definitions to be added when needed, for ease of organization.

extend class MyClass
{
    // Insert new functions, variables, and the like here.
}

See also