How to determine if the class is a Vector, RGB, etc.

Started by 2b2ttianxiu, Jan 20, 2023, 03:08 AM

Previous topic - Next topic

2b2ttianxiu

im making json to save my data.
i try use 'type' and 'typeof'. they are 'instance'

2b2ttianxiu

function loadJSON(file) {
    try {
        loadJSON0(fromJSONFile(file))
    } catch (e) saveJSON(file, {})
    return loadJSON0(fromJSONFile(file))
}
function loadJSON0(data) {
    foreach (key, val in data) {
        if (type(val) == "table") {
            data[key] = loadJSON0(val)
        }
        if (str_startswith(val, "!!Vector:")) {
            local a = getParamsFromString(val, ":")
            a.remove(0)
            data[key] = Vector(a[0], a[1], a[2])
        }
        if (str_startswith(val, "!!Quaternion:")) {
            local a = getParamsFromString(val, ":")
            a.remove(0)
            data[key] = Quaternion(a[0], a[1], a[2], a[3])
        }
    }
    return data
}
function IsVector(val) {
    try {
        val.w
        return false;
    } catch (e) {
        try {
            val.x, val.y, val.z
            return true;
        } catch (e) return false;
    }
    return false;
}
function IsQuaternion(val) {
    try {
        val.w, val.x, val.y, val.z
        return true;
    } catch (e) return false;
}
function saveJSON0(data) {
    foreach (key, value in data) {
        if (type(value) == "table") {
            data[key] = saveJSON0(value)
        } else if (type(value) == "instance") {
            if (IsQuaternion(value)) {
                data[key] = format("!!Quaternion:%d:%d:%d:%d", value.w, value.x, value.y, value.z)
            }
            if (IsVector(value)) {
                data[key] = format("!!Vector:%d:%d:%d", value.x, value.y, value.z)
            } else data[key] = value + ""
        }
    }
    return data;
}
function saveJSON(file, data) {
    try {
        return toJSONFile(file, saveJSON0(data))
    } catch (e) {
        file = str_replace(file, "/", "\\")
        if (file.find("\\") != null) {
            local i = -1, mkdir = ".\\", args = getParamsFromString(file, "\\")
            args.remove(args.len() - 1)
            foreach (i, val in args) {
                mkdir += val + "\\"
            }
            system("mkdir " + mkdir)
        }
    }
    return toJSONFile(file, saveJSON0(data))
}
It's e.x

2b2ttianxiu

Quote from: 2b2ttianxiu on Jan 20, 2023, 04:40 AMfunction loadJSON(file) {
    try {
        loadJSON0(fromJSONFile(file))
    } catch (e) saveJSON(file, {})
    return loadJSON0(fromJSONFile(file))
}
function loadJSON0(data) {
    foreach (key, val in data) {
        if (type(val) == "table") {
            data[key] = loadJSON0(val)
        }
        if (str_startswith(val, "!!Vector:")) {
            local a = getParamsFromString(val, ":")
            a.remove(0)
            data[key] = Vector(a[0], a[1], a[2])
        }
        if (str_startswith(val, "!!Quaternion:")) {
            local a = getParamsFromString(val, ":")
            a.remove(0)
            data[key] = Quaternion(a[0], a[1], a[2], a[3])
        }
    }
    return data
}
function IsVector(val) {
    try {
        val.w
        return false;
    } catch (e) {
        try {
            val.x, val.y, val.z
            return true;
        } catch (e) return false;
    }
    return false;
}
function IsQuaternion(val) {
    try {
        val.w, val.x, val.y, val.z
        return true;
    } catch (e) return false;
}
function saveJSON0(data) {
    foreach (key, value in data) {
        if (type(value) == "table") {
            data[key] = saveJSON0(value)
        } else if (type(value) == "instance") {
            if (IsQuaternion(value)) {
                data[key] = format("!!Quaternion:%d:%d:%d:%d", value.w, value.x, value.y, value.z)
            }
            if (IsVector(value)) {
                data[key] = format("!!Vector:%d:%d:%d", value.x, value.y, value.z)
            } else data[key] = value + ""
        }
    }
    return data;
}
function saveJSON(file, data) {
    try {
        return toJSONFile(file, saveJSON0(data))
    } catch (e) {
        file = str_replace(file, "/", "\\")
        if (file.find("\\") != null) {
            local i = -1, mkdir = ".\\", args = getParamsFromString(file, "\\")
            args.remove(args.len() - 1)
            foreach (i, val in args) {
                mkdir += val + "\\"
            }
            system("mkdir " + mkdir)
        }
    }
    return toJSONFile(file, saveJSON0(data))
}
It's e.x
The 'getParamsFromString' is split str to list
It's only Windows

DizzasTeR

You can try using instanceof operator.

if(myVariable instanceof Vector) { ... }

2b2ttianxiu

Quote from: DizzasTeR on Jan 28, 2023, 04:03 AMYou can try using instanceof operator.

if(myVariable instanceof Vector) { ... }
Oh, I wasn't remember have this sytanx, thanks