Skip to content

Enforce Script Cheat Sheet

Home | Cheat Sheet


Tipos

TipoDescricaoPadraoExemplo
intInteiro com sinal de 32 bits0int x = 42;
floatFloat de 32 bits0.0float f = 3.14;
boolBooleanofalsebool b = true;
stringTipo de valor imutavel""string s = "hello";
vector3 componentes float (x,y,z)"0 0 0"vector v = "1 2 3";
typenameReferencia de tiponulltypename t = PlayerBase;
ClassRaiz de todos os tipos de referencianull--
voidSem retorno----

Limites: int.MAX = 2147483647, int.MIN = -2147483648, float.MAX, float.MIN


Metodos de Array (array<T>)

MetodoRetornaNotas
Insert(item)int (indice)Adiciona ao final
InsertAt(item, idx)voidInsere na posicao
Get(idx) / arr[idx]TAcesso por indice
Set(idx, item)voidSubstitui no indice
Find(item)intIndice ou -1
Count()intQuantidade de elementos
IsValidIndex(idx)boolVerificacao de limites
Remove(idx)voidSem ordem (troca com o ultimo!)
RemoveOrdered(idx)voidPreserva ordem
RemoveItem(item)voidBusca + remove (ordenado)
Clear()voidRemove todos
Sort() / Sort(true)voidCrescente / decrescente
ShuffleArray()voidAleatorizar
Invert()voidReverter
GetRandomElement()TEscolha aleatoria
InsertAll(other)voidAdiciona todos de outro
Copy(other)voidSubstitui com copia
Resize(n)voidRedimensiona (preenche com padroes)
Reserve(n)voidPre-aloca capacidade

Typedefs: TStringArray, TIntArray, TFloatArray, TBoolArray, TVectorArray


Metodos de Map (map<K,V>)

MetodoRetornaNotas
Insert(key, val)boolAdiciona novo
Set(key, val)voidInsere ou atualiza
Get(key)VRetorna padrao se ausente
Find(key, out val)boolGet seguro
Contains(key)boolVerifica existencia
Remove(key)voidRemove por chave
Count()intQuantidade de entradas
GetKey(idx)KChave no indice (O(n))
GetElement(idx)VValor no indice (O(n))
GetKeyArray()array<K>Todas as chaves
GetValueArray()array<V>Todos os valores
Clear()voidRemove todos

Metodos de Set (set<T>)

MetodoRetorna
Insert(item)int (indice)
Find(item)int (indice ou -1)
Get(idx)T
Remove(idx)void
RemoveItem(item)void
Count()int
Clear()void

Sintaxe de Classe

c
class MyClass extends BaseClass
{
    protected int m_Value;                  // field
    private ref array<string> m_List;       // owned ref

    void MyClass() { m_List = new array<string>; }  // constructor
    void ~MyClass() { }                              // destructor

    override void OnInit() { super.OnInit(); }       // override
    static int GetCount() { return 0; }              // static method
};

Acesso: private | protected | (publico por padrao) Modificadores: static | override | ref | const | out | notnullModded: modded class MissionServer { override void OnInit() { super.OnInit(); } }


Fluxo de Controle

c
// if / else if / else
if (a > 0) { } else if (a == 0) { } else { }

// for
for (int i = 0; i < count; i++) { }

// foreach (value)
foreach (string item : myArray) { }

// foreach (index + value)
foreach (int i, string item : myArray) { }

// foreach (map: key + value)
foreach (string key, int val : myMap) { }

// while
while (condition) { }

// switch (SEM fall-through!)
switch (val) { case 0: Print("zero"); break; default: break; }

Metodos de String

MetodoRetornaExemplo
s.Length()int"hello".Length() = 5
s.Substring(start, len)string"hello".Substring(1,3) = "ell"
s.IndexOf(sub)int-1 se nao encontrado
s.LastIndexOf(sub)intBusca do final
s.Contains(sub)bool
s.Replace(old, new)intModifica in-place, retorna contagem
s.ToLower()voidIn-place!
s.ToUpper()voidIn-place!
s.TrimInPlace()voidIn-place!
s.Split(delim, out arr)voidDivide em TStringArray
s.Get(idx)stringCaractere unico
s.Set(idx, ch)voidSubstitui caractere
s.ToInt()intConverte para int
s.ToFloat()floatConverte para float
s.ToVector()vectorConverte "1 2 3"
string.Format(fmt, ...)stringPlaceholders %1..%9
string.Join(sep, arr)stringJunta elementos do array

Metodos Matematicos

MetodoDescricao
Math.RandomInt(min, max)[min, max) maximo exclusivo
Math.RandomIntInclusive(min, max)[min, max]
Math.RandomFloat01()[0, 1]
Math.RandomBool()true/false aleatorio
Math.Round(f) / Floor(f) / Ceil(f)Arredondamento
Math.AbsFloat(f) / AbsInt(i)Valor absoluto
Math.Clamp(val, min, max)Limitar ao intervalo
Math.Min(a, b) / Max(a, b)Minimo/maximo
Math.Lerp(a, b, t)Interpolacao linear
Math.InverseLerp(a, b, val)Lerp inverso
Math.Pow(base, exp) / Sqrt(f)Potencia/raiz
Math.Sin(r) / Cos(r) / Tan(r)Trigonometria (radianos)
Math.Atan2(y, x)Angulo a partir dos componentes
Math.NormalizeAngle(deg)Normalizar para 0-360
Math.SqrFloat(f) / SqrInt(i)Quadrado

Constantes: Math.PI, Math.PI2, Math.PI_HALF, Math.DEG2RAD, Math.RAD2DEG

Vector: vector.Distance(a,b), vector.DistanceSq(a,b), vector.Direction(a,b), vector.Dot(a,b), vector.Lerp(a,b,t), v.Length(), v.Normalized()


Padroes Comuns

Downcast Seguro

c
PlayerBase player;
if (Class.CastTo(player, obj))
{
    player.DoSomething();
}

Cast Inline

c
PlayerBase player = PlayerBase.Cast(obj);
if (player) player.DoSomething();

Guard de Null

c
if (!player) return;
if (!player.GetIdentity()) return;
string name = player.GetIdentity().GetName();

Verificar IsAlive (Requer EntityAI)

c
EntityAI eai;
if (Class.CastTo(eai, obj) && eai.IsAlive()) { }

Iteracao de Map com Foreach

c
foreach (string key, int value : myMap)
{
    Print(key + " = " + value.ToString());
}

Conversao de Enum

c
string name = typename.EnumToString(EDamageState, state);
int val; typename.StringToEnum(EDamageState, "RUINED", val);

Bitflags

c
int flags = FLAG_A | FLAG_B;       // combine
if (flags & FLAG_A) { }           // test
flags = flags & ~FLAG_B;          // remove

O Que NAO Existe

Recurso AusenteSolucao
Ternario ? :if/else
do...whilewhile(true) { ... break; }
try/catchGuard clauses + retorno antecipado
Heranca multiplaSimples + composicao
Sobrecarga de operadoresMetodos nomeados (exceto [] via Get/Set)
LambdasMetodos nomeados
nullptrnull / NULL
\\ / \" em stringsEvitar (CParser quebra)
#includeconfig.cpp files[]
NamespacesPrefixos de nome (My, VPP_)
Interfaces / abstractMetodos base vazios
Fall-through em switchCada case e independente
Valores em #defineUse const
Expressoes em parametros defaultApenas literais/NULL
Parametros variadicosstring.Format ou arrays
Redeclaracao de variavel em else-ifNomes unicos por branch

Criacao de Widget (Programatica)

c
// Get workspace
WorkspaceWidget ws = GetGame().GetWorkspace();

// Create from layout
Widget root = ws.CreateWidgets("MyMod/gui/layouts/MyPanel.layout");

// Find child widget
TextWidget title = TextWidget.Cast(root.FindAnyWidget("TitleText"));
if (title) title.SetText("Hello World");

// Show/hide
root.Show(true);
root.Show(false);

Padrao RPC

Registrar (servidor):

c
// In 3_Game or 4_World init:
GetGame().RPCSingleParam(null, MY_RPC_ID, null, true, identity);  // Engine RPC

// Or with string-routed RPC (MyRPC / CF):
GetRPCManager().AddRPC("MyMod", "RPC_Handler", this, 2);  // CF
MyRPC.Register("MyMod", "MyRoute", this, MyRPCSide.SERVER);  // MyMod

Enviar (client para servidor):

c
Param2<string, int> data = new Param2<string, int>("itemName", 5);
GetGame().RPCSingleParam(null, MY_RPC_ID, data, true);

Receber (handler no servidor):

c
void RPC_Handler(CallType type, ParamsReadContext ctx, PlayerIdentity sender, Object target)
{
    if (type != CallType.Server) return;
    if (!sender) return;

    Param2<string, int> data;
    if (!ctx.Read(data)) return;

    string itemName = data.param1;
    int quantity = data.param2;
    // Process...
}

Tratamento de Erros

c
ErrorEx("message");                              // Default ERROR severity
ErrorEx("info", ErrorExSeverity.INFO);           // Info
ErrorEx("warning", ErrorExSeverity.WARNING);     // Warning
Print("debug output");                           // Script log
string stack = DumpStackString();                // Get call stack

I/O de Arquivos

c
// Paths: "$profile:", "$saves:", "$mission:", "$CurrentDir:"
bool exists = FileExist("$profile:MyMod/config.json");
MakeDirectory("$profile:MyMod");

// JSON
MyConfig cfg = new MyConfig();
JsonFileLoader<MyConfig>.JsonLoadFile(path, cfg);  // Returns VOID!
JsonFileLoader<MyConfig>.JsonSaveFile(path, cfg);

// Raw file
FileHandle fh = OpenFile(path, FileMode.WRITE);
if (fh != 0) { FPrintln(fh, "line"); CloseFile(fh); }

Criacao de Objetos

c
// Basic
Object obj = GetGame().CreateObject("AK101", pos, false, false, true);

// With flags
Object obj = GetGame().CreateObjectEx("Barrel_Green", pos, ECE_PLACE_ON_SURFACE);

// In player inventory
player.GetInventory().CreateInInventory("BandageDressing");

// As attachment
weapon.GetInventory().CreateAttachment("ACOGOptic");

// Delete
GetGame().ObjectDelete(obj);

Funcoes Globais Principais

c
GetGame()                          // CGame instance
GetGame().GetPlayer()              // Local player (CLIENT only, null on server!)
GetGame().GetPlayers(out arr)      // All players (server)
GetGame().GetWorld()               // World instance
GetGame().GetTickTime()            // Server time (float)
GetGame().GetWorkspace()           // UI workspace
GetGame().SurfaceY(x, z)          // Terrain height
GetGame().IsServer()               // true on server
GetGame().IsClient()               // true on client
GetGame().IsMultiplayer()          // true if multiplayer

Documentacao completa: Wiki de Modding DayZ | Gotchas | Tratamento de Erros

Released under CC BY-SA 4.0 | Code examples under MIT License