Jump to content

Asset Saving via Lua


reepblue
 Share

Recommended Posts

I'm trying to make an editor extension that'll create new files from the editor. I'm working on a "Create New Material" function, but I get an error with the Save command saying it expects usedata type and not a string, although I feel like this code is correct.

local extension = {}

function CreateNewMaterial()
    local file = RequestFile("Select Material Location", "", "Ultra Engine Material File (*.mat):mat", 0, true)
    if file ~= nil then
        local mat = CreateMaterial()
        if mat ~= nil then
            mat:SetColor(1,1,1,1)
            local shaderfamily = LoadShaderFamily("Shaders/PBR.fam")
            if shaderfamily then
                mat:SetShaderFamily(shaderfamily)
                shaderfamily = nil
            end
            mat:Save(file);
        end
    end
end

function extension.hook(event, extension)
    if event.id == EVENT_WIDGETACTION then
        if event.source == extension.menuitem then
            CreateNewMaterial()
        end
    end
end

--------------------------------------------------------------------
-- Add menu item
--------------------------------------------------------------------

local menu = program.menu:FindChild("Create", false)
if menu ~= nil then
    local submenu = menu:FindChild("Asset", false)
    if submenu == nil then
        submenu = CreateMenu("Asset", menu)
    end
    extension.menuitem = CreateMenu("Material", submenu)
end

ListenEvent(EVENT_WIDGETACTION, extension.menuitem, extension.hook, extension)

I also tried making a table and saving it, but it didn't work ether.

        local a = ctable()
        a["material"] = nil
        a["material"]["shaderFamily"] = "Shaders/PBR.fam"
        SaveTable(a, file)

What am I doing wrong here?

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...