Putting it all together
This week has been a big one in terms of wrangling together several big pillars of the engine to provide wider functionality. The image below shows how we can now dynamically run an external Lua script that modifies the 3D world on the fly:
![Vortex loading and running an external script that changes the texture of an entity's material.](http://www.alejandrosegovia.net/wp-content/uploads/2017/07/assign_texture_script.gif)
In the image above, I’ve created two boxes. Both of these have different materials and each material references a different texture.
What you can see I’m doing is that I “mistakenly” drag from the Asset Library a character texture and assign it as the second box’s texture. Oh no! How can we fix this? It’s easy: just run an external script that will assign the first box’s texture to the second!
I’ve pasted the code of the script below:
function get_entity_material( entity ) --get an entity's material local RENDER_COMPONENT_TYPE = 1 local rendercomp = entity:first_component_of_type( RENDER_COMPONENT_TYPE ) local material = rendercomp:get_material() return material; end ent0 = vtx.find_first_entity_by_name("box0") mat0 = get_entity_material( ent0 ) tex0 = mat0:get_texture( "diffuseTex" ) ent1 = vtx.find_first_entity_by_name("box1") mat1 = get_entity_material( ent1 ) mat1:set_texture( "diffuseTex", tex0 ) print("done")
As you can see, the script is pretty straightforward. It finds the boxes, drills all the way down to their materials and then assigns the texture of the first box to the second. The changes are immediately seen in the 3D world.
It’s worth noting that all function calls into the vtx namespace and derived objects are actually jumping into C++. This script is therefore dynamically manipulating engine objects, that’s why we see its effects in the scene view.
The function names are still work in progress, and admittedly, I need to write more scripts to see if these feel comfortable or if they’re too long and therefore hard to remember. My idea is to make the scripting interface as simple to use as possible, so please if you have any suggestions I would love to hear your feedback! Feel free to leave a comment below!
Next week I will continue working on adding more functionality to the scripting API, as well as adding more features to the renderer! Stay tuned for more!