# Working with UObjects

Athenaware's Lua implemention was designed to make working with objects easy and similar to how you'd do it in C++.&#x20;

### Accessing Object Members

In Lua, the colon (`:`) and dot (`.`) are both used for accessing members (methods or fields) of an object. However, they have a slight difference in terms of syntax and behavior.

The dot (`.`) operator is used to access a field directly. It is used with the following syntax:

```lua
object.member
```

Here, `object` is the table or object, and `member` is the name of the field within that object. When using the dot operator.

On the other hand, the colon (`:`) operator is used to call table functions and explicitly pass the object itself as the first argument. It is used with the following syntax:

```lua
object:func(arguments)
```

Here, `object` is the object, `func` is the name of the function within the object, and `arguments` are the arguments to be passed to the function.&#x20;

Members/fields can also be modified from Lua:

```lua
local vec = FVector()
vec.X = 123.0
vec.Y = 0
vec.Z = 12
```

### Construction

{% hint style="danger" %}
Only ScriptStruct's can be constructed. Trying to construct anything beyond that is not supported and may result in a crash.

In order to construct, we need to first load the class via [Get](/prismarine/lua-deprecated/functions/objects.md)Struct.
{% endhint %}

Constructing structs can be achieved by "calling" the pre-defined ScriptStruct with your supplied parameters. The order of which the parameters are layed out in the struct is the same order you'll supply it in the constructor. For example:<br>

The FVector struct is defined as followed in UE4:

```cpp
struct FVector {
    float X;
    float Y;
    float Z;
};
```

Creating an instance of an FVector, we'd do the following:

```lua
local vectorA = FVector(1, 2, 3) -- {X=1, Y=2, Z=3}
local vectorB = FVector(2, 4) -- {X=2, Y=4, Z=0} since Z wasn't passed in, it will be 0

-- We can also initialize a vector and assign values later...
local vectorC = FVector()
vectorC.X = 1.0
vectorC.Y = 12.0
vector.Z = 43.0
```

###

### Object Validity

All UObjects held in lua are weak references. In order to check if the object is valid, you can use `UObject::IsValid`. This function will return true or false depending on whether or not the function is a valid UObject.

```lua
local storage = CONTROLLER
if storage:IsValid() then
    -- valid object, can be used
    local player = storage:K2_GetPawn()
else
    storage = nil -- nil the value since it's not valid
end
```

### UObject::IsA

Check if an object is of a type of class. Returns true if it is, false if not.

```lua
local AShip = GetStruct('Athena', 'Ship')
function onActorTick(actor)
    if actor:IsA(AShip) then
        -- this actor is a ship
    end
end
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.shulker.app/prismarine/lua-deprecated/working-with-uobjects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
