Can the new asset system load assets dynamically?

I’m trying to make a function for helping me streamline entity creation::


function makeAThing(name, modelString, positionVec3, rotationVec3, scaleVec3)
    local newEntity = scene:entity()
    newEntity.name = name or "entityMadeAt"..tostring(os.time())
    newEntity.position = positionVec3 or newEntity.position
    newEntity.eulerAngles = rotationVec3 or newEntity.eulerAngles
    newEntity.scale = scaleVec3 or newEntity.scale
    newEntity.modelString = modelString
  --> doesn't work:  
    newEntity.model = craft.model(asset[modelString])
    newEntity.name = name
    return newEntity
end

I’ve tried every combination I can think of, including using the .. operator using bracket notation.

I have also searched the forum, and the reference, and I’ve found lots of stuff about how to save things by appending strings to asset, but that format doesn’t seem to work in reverse.

Either there’s nothing documenting loading things, or I’ve floundered in finding the documentation, or, I guess, it might not be possible with the new asset system.

Can it be done?

Is this kind of what you’re after.

viewer.mode=STANDARD

function setup()
    folder=asset.builtin.Planet_Cute    -- change folder name
    tab=folder.all
end

function draw()
    background(0)
    for a,b in pairs(tab) do
        sprite(b,WIDTH/2,HEIGHT-a*50)
    end
end

@UberGoober - I tend to use tables with the relevant data in and a path to the source.

Kinda like:


pics = {}

path = asset.documents.Dropbox
For lp =1, #Objects do
       pics[lp] = readImage(Objects[lp])
end


Objects = {
                     image1,
                     Image2,
                     Image3  
}

Please ignore poor formatting and incomplete code , just trying to illustrate the loading method I use.

@dave1707 I don’t think so.

I want to be able to pass a string to a function and have it use that string to attach a model to an entity.

Here’s the function I pasted above, but pared down to just that functionality:


function makeAThing(modelString)
    local newEntity = scene:entity()  
    local modelFromString = craft.model(asset[modelString]) <—does not work
    newEntity.model = modelFromString
    return newEntity
end

@Bri_G for saving or for loading?

Sample code?

This works if you pass the whole name. Create a table with the full names of what you want and pass a selection.

viewer.mode=FULLSCREEN

function setup()
    scene = craft.scene()
    scene.camera.position=vec3(0,0,-80)  
    scene.sky.material.horizon=color(0,100,150)
    
    xx(asset.builtin.Blocky_Characters.Robot)
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()	   
end

function xx(name)
    ch = scene:entity()
    ch.model = craft.model(name)
end

Or this.

viewer.mode=FULLSCREEN

function setup()
    scene = craft.scene()
    scene.camera.position=vec3(0,0,-80)  
    scene.sky.material.horizon=color(0,100,150)
    xx("Robot")
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()	   
end

function xx(name)
    ch = scene:entity()
    ch.model = craft.model(asset.builtin.Blocky_Characters[name])
end

@UberGoober - for loading. I use it for loading images. I also used it for loading models - when I made the video in The Final Frontier thread. I will be posting a project to load the 8 galaxies from Elite in that thread shortly once I sort out galaxy selection and find a way to export all the assets in the zipped project.

[[text deleted because of apparently unfixable auto-formatting misbehavior by the post itself. The exact same text has been copy/pasted below and is formatting perfectly.]]

@UberGoober - are you posting on your phone? Every time I do the tilde reformatting doesn’t work. -Android phone. Must use a different character set.

I am but it’s worked before…

I think you have to use three ` not three ~

@dave1707 I don’t think craft.model(name) works in any but very specific conditions, and in the example where xx(name) has a hard-coded path to a specific asset pack, that’s the exact thing I am trying to get around having to do.

It think I’ve got this now, though. The breakthrough was that, for anything in an asset pack, the pack has to be accessed by brackets too, as in:



craft.model(asset.builtin[packName][modelName])


I am a bit abashed to admit this, but the frustrating thing was that once I figured this out I looked back through my code and found a place where I’d cut-and-pasted code from the 3D Asset Viewer that does this exact thing.

[original post was written on iPhone, this one was copied and pasted here on an iPad. ]

I was looking at the touch demo you gave me and found that it’s possible to do use string concatenation also because asset returns a string for however deep you go.

packs = { “Space cute” }
models = { “Rocketship”}
 
asset.builtin..packs[1]..models[1]