REManagedObject

REManagedObjects are the basic building blocks of most types in the engine (unless they're native types).

They are returned from methods like:

Example usage:

local scene_manager = sdk.get_native_singleton("via.SceneManager")
local scene_manager_type = sdk.find_type_definition("via.SceneManager")
local scene = sdk.call_native_func(scene_manager, scene_manager_type, "get_CurrentScene")

-- Scene is an REManagedObject
if scene ~= nil then
    local current_timescale = scene:call("get_TimeScale")
    log.info("Current timescale: " .. tostring(current_timescale))

    scene:call("set_TimeScale", 5.0)
end

Custom indexers

self.foo

If foo is a field or method of the object, returns either the field or REMethodDefinition if it exists.

self:foo(bar, baz)

If foo is a method of the object, calls foo with the supplied arguments.

If the method is an overloaded function, you must instead use self:call(name, args...) with the correct function prototype, as this does not deduce the correct function based on the passed arguments.

self.foo = bar

If foo is a field of the object, assigns the value bar to the field.

This automatically handles the reference counting for the old and new field. Do not use :force_release() and :add_ref_permanent() in this case to handle the references.

self[i]

Checks if the object has a get_Item method and calls it with i.

self[i] = foo

Checks if the object has a set_Item method and calls it with i and foo as the respective parameters.

Meta-methods

REManagedObject == REManagedObject

Compares two REManagedObjects for equality. Returns true if they point to the same object.

Methods

self:call(method_name, args...)

Return value is dependent on the method's return type. Wrapper over sdk.call_object_func.

Full function prototype can be passed as method_name if there are multiple functions with the same name but different parameters.

e.g. self:call("foo(System.String, System.Single, System.UInt32, System.Object)", a, b, c, d)

Valid method names can be found in the Object Explorer. Find the type you're looking for, and valid methods will be found under TDB Methods.

self:get_type_definition()

Returns an RETypeDefinition*.

self:get_field(name)

Return type is dependent on the field type.

A literal (const) System.String field returns the constant's text.

self:set_field(name, value)

For a value type field, value can be a ValueType of the field's type: its value is copied into the field. A ValueType of another type raises an error.

self:get_address()

self:get_reference_count()

Returns the object's reference count.

Right after the object's last reference was released, this returns 0, although the object still exists for about two more frames (see self:release).

self:get_object_size()

Returns the size of the object in bytes.

self:deserialize_native(data, objects)

Experimental API to deserialize data into self.

data is RSZ data, in table format as an array of bytes.

Will only work on native via types.

Dangerous Methods

Only use these if necessary!

self:add_ref()

Increments the object's internal reference count. Returns the object, so the call can be chained.

Lua holds at most one reference per object this way: calling add_ref() again on an object Lua already holds a reference to adds nothing.

Calling it on an object you gave up with self:release() takes a reference again, as long as the object still exists. The object it returns is the same Lua object you called it on.

self:add_ref_permanent()

Increments the object's internal reference count without REFramework managing it. Any objects created with REFramework and also using this method will not be deleted after the Lua state is destroyed.

self:release()

Decrements the object's internal reference count. Can only be used on objects managed by Lua.

If that was the last reference, the object is not destroyed inside the call. REFramework passes the last release on to the engine two frames later, on the game thread at a frame boundary. The engine destroys the object then, or in some games a few frames after that. During those two frames:

  • self:get_reference_count() already returns 0.
  • Another self:release() or self:force_release() on the object does nothing: there is no reference left to give up.
  • self:add_ref() takes a new reference and keeps the object alive.

The delay is what makes add_ref() followed by release() safe on an object you just created. A small new object lives in a memory page the engine keeps for the current thread's new objects, and add_ref() makes the object's reference the only thing that keeps that page alive. Releasing it before the thread's next cleanup would destroy the object and free the page while the engine still allocates from it, and the game could crash some frames later in unrelated code. Two frames later the cleanup has run.

local arr = sdk.create_managed_array("System.Int32", 4):add_ref()

-- ... use arr, also in later frames ...

arr:release()
-- arr:get_reference_count() is 0 now; the array is destroyed two frames later.
-- Don't use arr after this.

self:force_release()

Decrements the object's internal reference count, also when Lua holds no reference to it. Can be used on any REManagedObject. Can crash the game or cause undefined behavior.

The object is destroyed when the count reaches 0, with the same two-frame delay as for self:release.

Automatic reference counting

When a new Lua reference is created to an REManagedObject whose reference count is above 0, REFramework automatically increments its reference count internally with self:add_ref(). This will keep the object alive until you are no longer referencing the object in Lua.

An object with a count of 0 or less is one the engine just created and still treats as local to the current thread. Lua gets it without a reference, so the variable does not keep it alive: the engine frees it at its next cleanup of local objects, normally by the next frame. Use it right away, or call self:add_ref() on it to keep it.

When Lua is no longer referencing the object anywhere, REFramework releases that reference for you. This does not happen inside Lua's garbage collection: the reference is released a few frames after Lua has collected the object's userdata. The userdata has no __gc metamethod (getmetatable(obj).__gc is nil).

The only time you will need to manually call self:add_ref() and self:release() is when a newly created object is returned by the engine, e.g. an array, or something from sdk.create_instance().

A more in-depth explanation can be found in the "FrameGC Algorithm" section of this GDC presentation by Capcom:

https://github.com/kasicass/blog/blob/master/3d-reengine/2021_03_10_achieve_rapid_iteration_re_engine_design.md#framegc-algorithm-17

self:read_byte(offset)

self:read_short(offset)

self:read_dword(offset)

self:read_qword(offset)

self:read_float(offset)

self:read_double(offset)

self:write_byte(offset, value)

self:write_short(offset, value)

self:write_dword(offset, value)

self:write_qword(offset, value)

self:write_float(offset, value)

self:write_double(offset, value)

offset is in bytes from the start of the object. The whole value must lie inside the object: read_qword needs 8 bytes starting at offset, read_byte needs one. A read that does not fit returns 0, and a write that does not fit is dropped. Neither raises an error.

The limit is the size of the object's type (self:get_type_definition():get_size()). Arrays and System.String objects are the exception: they are bounded by their real size, the one self:get_object_size() returns. So an array's elements can be read, and so can a String's length (at offset 0x10) and its UTF-16 characters (from offset 0x14).