Skip to content

SelfAware Extended

Vurv edited this page Feb 14, 2021 · 8 revisions

Last updated for version v0.3.1

Main Info

This is a part of VExtensions that allows for self-aware behavior for e2s, on steroids.
Some example functions that this provides are:

  • A function to retrieve all of the user defined functions in an e2,
  • A function to retrieve all of the builtin functions on the server, their op costs, names, etc.

Documentation

Functions

n = defined(s func_name)

Searches for whether a function exists at runtime in an e2, similarly to the #ifdef preprocessor.
The difference between this and #ifdef is that:

  • This does not allow you to search with names like entity:applyForce(vector)
  • It instead uses a format of applyForce(e:v) or simply "applyForce" to get the first function defined() finds.
  • It also searches for user-defined functions.

If it finds a user-defined function with the name specified, the function will return 2. <-- This is checked first
If it finds a builtin (server / lua-added function), the function will return 1.
If it finds nothing, it will return 0.

s = getFunctionPath(s func_name)

Searches for a BUILTIN e2 function (created by lua), and if it is found, returns the path to the lua code that created it.
This is useful if you ever want to know if a certain function is from vanilla wiremod, or want to look into the source code of a function.

t = getExtensionsInfo()

Returns a table of every E2 extension on the server.
The structure of the table goes like this:

table(
    "VExtensions" = array(number is_enabled, string extension_description),
    "Blah" = array(0, "A simple extension that does nothing.")
)

t = getConstants()

Returns an e2 table of all of the constants provided by Expression2. Table structure:

table(
    "XCO_MAX" = 1000,
    "XCO_STACK_MAX" = 50
)

t = getUserFunctionInfo(n mode)

Mode is a number that can be used from a list of these constants:

  • UDF_FLAT
  • UDF_DNC Returned table structure for UDF_FLAT:
table(
    "cool_func(e:v)" = array(
        [1] = string return_type_name
    ),
    "bruh()" = array(
        [1] = "xco"
    )
)

Returned table structure for UDF_DNC:

table(
    "cool_func" = array(
        [1] = "cool_func(e:v)",
        [2] = "number"
    )
)

See L167 of selfaware for the TODO status of this function.

t = getBuiltinFuncInfo(s func_name)

Tries to find function info about a builtin (lua made) function.
Returns it in a structure like:

table(
    [1] = "print(...)",
    [2] = ""
    [3] = 100
)

With [1] being the signature, 2 being the return type, 3 being the opcost.
When you pass "*" as the func_name, you get a structure like this, which holds every single builtin function from E2.

table(
    "print(ns)" = array(
        [1] = "print(ns)",
        [2] = "",
        [3] = 100
    ),
    "health(e:)" = array(
        [1] = "health(e:)",
        [2] = "n",
        [3] = 5
    )
)

t = getTypeInfo()

Returns a table containing E2 types information (type ID is used as table key, and type name as the table value).
If you need it other way around, just use invert function. (You should cache the result in your E2 for performance.) Returns a structure like:

table(
    "xco" = "coroutine",
    "n" = "number"
)

n = deleteUserFunction(s func_name)

Attempts to delete a user-defined function from this E2's context. You must specify a full signature (i.e. "myFunc(e:av)")
Returns 0 if the function you passed does not exist, 1 if you successfully deleted it.

Example

@name SelfAware Extended Example
# Say a function name in chat, and the chip will print the OPS cost of the function.

if(first()){
    runOnChat(1)
}elseif(chatClk()){
    local YELLOW = vec(255,255,50)
    local WHITE = vec(255)
    local RED = vec(200,70,70)

    local FuncName = lastSaid():explode(" ")[1,string]
    local FuncData = getBuiltinFuncInfo(FuncName)
    if(FuncData){
        local OPSCost = FuncData[3,number]
        printGlobal(RED, "Function", WHITE, ": ", YELLOW, FuncName, RED, "\nCosts", WHITE, ": ", YELLOW, OPSCost, " ops")
    }else{
        printGlobal(RED, "No function data found for: ", YELLOW, FuncName)
    }
}

See a more in-depth example at our Discussions

Clone this wiki locally