aod6060 / dsge

This is a simple 2D engine for my projects.
MIT License
1 stars 0 forks source link

Lua Wrapping System #27

Open aod6060 opened 3 days ago

aod6060 commented 3 days ago

Alright now that I'm done with the configuration system for now I'm going to move onto wrapping the systems into the lua programming language which is going to be the main scripting language for this project. Its going to be a big system to say the least. After this I'm going to add in work on an internal gui system as well as an editor for said gui system.

aod6060 commented 3 days ago

Oh, it going to be under the lw interface and folder were lw="lua wrapper"

aod6060 commented 3 days ago

Alright got a basic interface going. Lua scripts will be used a self contained bit of code that will manage...

  1. Global Script
  2. Gui Scripts
  3. Scene Scripts
  4. Entity Scripts

This is just my though process for how this scripting system will work. Basically lua allows for an Entity for example to have additional behavior that isn't included with DSGE.

aod6060 commented 3 days ago

Here is the interface for the LuaExports and template LuaExportsVariables..

template<typename T>
struct LuaExportVariable {
    std::string name;
    T value;
};
// These will act similar to glsl uniforms
struct LuaExports {
    LuaState* parent = nullptr;

    std::map<std::string, int8_t> export_int8;
    std::map<std::string, uint8_t> export_uint8;
    std::map<std::string, int16_t> export_int16;
    std::map<std::string, uint16_t> export_uint16;
    std::map<std::string, int32_t> export_int32;
    std::map<std::string, uint32_t> export_uint32;
    std::map<std::string, int64_t> export_int64;
    std::map<std::string, uint64_t> export_uint64;
    std::map<std::string, float> export_float;
    std::map<std::string, double> export_double;
    std::map<std::string, bool> export_bool;
    // Going to need to figure out what other types
    // I'll be need here

    void init(LuaState* state);
    void release();

    void setInt8(std::string name, int8_t value);
    void setUInt8(std::string name, uint8_t value);
    void setInt16(std::string name, int16_t value);
    void setUInt16(std::string name, uint16_t value);
    void setInt32(std::string name, int32_t value);
    void setUInt32(std::string name, uint32_t value);
    void setInt64(std::string name, int64_t value);
    void setUInt64(std::string name, uint64_t value);
    void setFloat(std::string name, float value);
    void setDouble(std::string name, double value);
    void setBool(std::string name, bool value);

    int8_t getInt8(std::string name);
    uint8_t getUInt8(std::string name);
    int16_t getInt16(std::string name);
    uint16_t getUInt16(std::string name);
    int32_t getInt32(std::string name);
    uint32_t getUInt32(std::string name);
    int64_t getInt64(std::string name);
    uint64_t getUInt64(std::string name);
    float getFloat(std::string name);
    double getDouble(std::string name);
    bool getBool(std::string name);

    void getAllInt8(std::vector<LuaExportVariable<int8_t>>& variables);
    void getAllUInt8(std::vector<LuaExportVariable<uint8_t>>& variables);
    void getAllInt16(std::vector<LuaExportVariable<int16_t>>& variables);
    void getAllUInt16(std::vector<LuaExportVariable<uint16_t>>& variables);
    void getAllInt32(std::vector<LuaExportVariable<int32_t>>& variables);
    void getAllUInt32(std::vector<LuaExportVariable<uint32_t>>& variables);
    void getAllInt64(std::vector<LuaExportVariable<int64_t>>& variables);
    void getAllUInt64(std::vector<LuaExportVariable<uint64_t>>& variables);
    void getAllFloat(std::vector<LuaExportVariable<float>>& variables);
    void getAllDouble(std::vector<LuaExportVariable<double>>& variables);
    void getAllBool(std::vector<LuaExportVariable<bool>>& variables);

};
aod6060 commented 3 days ago

Alright got this interface implemented.

aod6060 commented 3 days ago

Hmm, I'm not liking this because the types might not work. Maybe instead of returning different types how about return basic types that lua supports.

aod6060 commented 3 days ago

Alright I've changed up the "LuaExports" structure to be more aligned with the lua types.

struct LuaExports {
    LuaState* parent = nullptr;

    std::map<std::string, int32_t> export_integers;
    std::map<std::string, float> export_numbers;
    std::map<std::string, bool> export_bools;

    // Going to need to figure out what other types
    // I'll be need here

    void init(LuaState* state);
    void release();

    // Setters
    void setInteger(std::string name, int32_t value);
    void setNumber(std::string name, float value);
    void setBool(std::string name, bool value);

    // Getters
    int32_t getInteger(std::string name);
    float getNumber(std::string name);
    bool getBool(std::string name);

    // This will get you all of the all variables
    void getAllIntegers(std::vector<LuaExportVariable<int32_t>>& variables);
    void getAllNumbers(std::vector<LuaExportVariable<float>>& variables);
    void getAllBools(std::vector<LuaExportVariable<bool>>& variables);

};
aod6060 commented 3 days ago

Here is the lua wrapper interface for the exports.

void export_initLibs(LuaState* state);

int lw_setInteger(lua_State* l);
int lw_setNumber(lua_State* l);
int lw_setBool(lua_State* l);

int lw_getInteger(lua_State* l);
int lw_getNumber(lua_State* l);
int lw_getBool(lua_State* l);
aod6060 commented 3 days ago

Here is a simple test script to test the export interface

-- test.lua
export.setInteger(state, "test_int", 1)
export.setNumber(state, "test_num", 3.16)
export.setBool(state, "test_bool", true)

test_int = export.getInteger(state, "test_int")
test_num = export.getNumber(state, "test_num")
test_bool = export.getBool(state, "test_bool")

print(test_int)
print(test_num)
print(test_bool)

function callme()
    print("Hello, World")
end
aod6060 commented 3 days ago

Alright I ran into some compiling issues with lua. Had to go back to an earlier version of lua.

aod6060 commented 3 days ago

I figured out something why I was having issues. It turns out if one of the *.h header files from lua is included into a cpp file it will explode. Not in a good way which kind of frastrating if you ask me. Hmm, I might have to consider using a different scripting language if this becomes annoying.

aod6060 commented 3 days ago

Alright, I figured out a bunch of stuff you can do with with lua. I'm exited. Here is an example of what you could potentially do with lua.

--[[
    This is actually a better way to handle experts
    whcih will make lua code to look a lot better.

    I didn't relize I could do this with lua

    export(variable_name, type)
]]

export("test_int", TYPE_INT)
test_int = 1

export("test_num", TYPE_NUMBER)
test_num = 3.16

export("test_bool", TYPE_BOOLEAN)
test_bool = true

print(test_int)
print(test_num)
print(test_bool)

function callme()
    print("Hello, World")
end
aod6060 commented 2 days ago

Yeah this way will simplify everything plus it will allow for range values.

aod6060 commented 2 days ago

Ok, I've made it were I can read stuff from the lua stack. However, I'm having issues modifiying a variable on the stack which I'm going to figure out. If it doesn't work I'll go back to the original way of handling exports.

aod6060 commented 2 days ago

Ok I feel really stupid and just solved it in a view seconds. Here how you set a value in lua.

    lua_pushnumber(this->state, value);
    lua_setglobal(this->state, name.c_str());
aod6060 commented 2 days ago

My God I'm a fool :)

aod6060 commented 2 days ago

Let's call a function from lua.

aod6060 commented 2 days ago

Alright I made it be able to call a function and here is the new interface for the and hopefully the final one.

enum LWType {
    LWT_INTEGER = 0,
    LWT_NUMBER,
    LWT_BOOL,
    LWT_MAX_SIZE
};

struct LWVariable {
    std::string name;
    LWType type;
};

struct LWState {
    std::string path;
    lua_State* state;
    //LWExports exports;
    //std::map<std::string, std::string> exports;
    std::vector<LWVariable> exports;

    void open(std::string path);
    void close();

    int getInteger(std::string name);
    float getNumber(std::string name);
    bool getBoolean(std::string name);

    void setInteger(std::string name, int value);
    void setNumber(std::string name, float value);
    void setBoolean(std::string name, bool value);

    // For now I don't care about arguments
    void callFunction(std::string name);

    void getVariables(std::vector<LWVariable>& variables);
};

This interface will have a bunch of other stuff, however, I won't be change the structure of it. Also here is the upgraded example script.

--[[
    This is actually a better way to handle experts
    which will make lua code to look a lot better.

    I didn't relize I could do this with lua

    export(variable_name, type)
]]

export(state, "test_int", LWType.LWT_INTEGER)
test_int = 1

export(state, "test_num", LWType.LWT_NUMBER)
test_num = 3.16

export(state, "test_bool", LWType.LWT_BOOL)
test_bool = true

print(test_int)
print(test_num)
print(test_bool)
print("What ")

function callme()
    print("Hello, World")

    print("test_int: "..test_int)
    print("test_num: "..test_num)
    --[[
        For values that isn't a number or string 
        use tostring function to get the value
        https://devforum.roblox.com/t/attempt-to-concatenate-string-with-boolean/843822

        Thank you roblox forum. :/
    ]]
    print("test_bool: "..tostring(test_bool))

    print()

end

Its a pretty straight forward. I allow took a look at a video searies on YouTube from javidx9 and here is the video https://www.youtube.com/watch?v=4l5HdmPoynw

aod6060 commented 2 days ago

Alright I'm going to run create a quick test to see how this lua interface will interact with ImGui because I'm going to use ImGui to create editors and I need both lua and ImGui playing nicely with each other.

aod6060 commented 2 days ago

Oh my God this is so cool. I'm going to have to make an update video for this right now because its doing what it does because man it basically does the same thing as what gdscript does for godot or c# does for unity.

aod6060 commented 1 day ago

dsge_update_2_logo

Here is the demonstration video of this issue and a brief demonstration of the system configuration menu as well.

aod6060 commented 10 hours ago

Lua system wrapping up other systems.

  1. Application System
  2. Configuration System
  3. ImGuiWrapper System
  4. Input System
  5. Physics System
  6. Render System
  7. Sound System