Announcement

Collapse
No announcement yet.

How To Get and Set Materials and Meshes While In Level

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How To Get and Set Materials and Meshes While In Level

    Dear Everyone,

    Goal:
    In my game I have a in-game 3D editor where user can click on a mesh, that is illustrating a material, to then assign that material to another mesh in the level that the user is working with and placing in level.

    So in this tutorial I am sharing how you can
    1. get the material name of a mesh
    2. create new material from that name
    3. set the material of another mesh to the material you just created from name


    You can use this same process for getting and setting meshes, just use

    Code:
    local string pName;
    local StaticMesh m;
    
    pName = PathName(srcMesh.StaticMeshComponent.StaticMesh);
    m = StaticMesh(DynamicLoadObject(pName , class'StaticMesh'));
    destMesh.StaticMeshComponent.SetStaticMesh(m);
    ~~~

    Getting the Material name of the Source Mesh

    Source Mesh = the mesh that has the material you want the other mesh to have.

    Here is how you get the name of the material you want!

    Code:
    local string matName;
    local string matName;
    
    //PathName is Object static final function. ♥ THANK YOU SPOOF!!! ♥
    matName = PathName(joyWallManifestor.StaticMeshComponent.GetMaterial(0));

    ~~~

    Creating new Material

    Code:
    local Material m;
    
    m = Material(DynamicLoadObject(stringPathofMaterialName, class'Material'));
    ~~~

    Setting Material

    Code:
    var KActorSpawnable levelWall //the mesh whose material we want to change
    
    levelWall.StaticMeshComponent.SetMaterial(0, theMaterial);
    ~~~

    All Together

    Code:
    function setMaterialSrcDest(DynamicSMActor sourceMesh, DynamicSMActor destMesh) {
    //the class of properties could be many things
    //you can use Kactors, StaticMeshActors, many things, 
    //anything with a StaticMeshComponent
    
      //you could remove the local variables to make code more compact
      local string matName;
      local Material m;
    
                         //Spoof for the win
      matName = PathName(joyWallManifestor.StaticMeshComponent.GetMaterial(0));
      m = Material(DynamicLoadObject(matName , class'Material'));
    
      destMesh.StaticMeshComponent.SetMaterial(0, m);
    
    }



    And it all works! (with lovely assistance from Spoof)

    Now when I click on a mesh, I can transfer its material to another mesh/level structure of my choosing!



    Rama

    PS: Since I want to be able to save/load my level from file, I can now simply store the
    basic var type STRING, the name of the mesh's material, and the recreate the mesh
    and its material from file.

    class'Engine'.static.BasicSaveObject only seems to work well with basic var types

    so being able to reduce the material and also the mesh to a simple string, using PathName, enables easy saving to file and loading from file.

    #2
    Originally posted by evernewjoy View Post
    If anyone can find a way around this limitation let me know!

    Code:
    /**
     * Returns the full path name of the specified object (including package and groups), ie CheckObject::GetPathName().
     */
    native static final function string PathName(Object CheckObject);

    Comment


      #3
      Yaaay!!!

      Thank you Spoof!



      I updated the main code to reflect your wonderful information

      Rama

      PS: now my code shows how to get and set the material for a mesh using ANY material from ANY package.

      Comment


        #4
        Thanks a lot evernewjoy! Very useful tutorial! Thanks for sharing!
        I'll try it immediately

        Comment

        Working...
        X