Kemena3D
Loading...
Searching...
No Matches
kemena::kPrefab Class Reference

A serialized object subtree saved as a .prefab JSON asset. More...

#include <kprefab.h>

Public Member Functions

 kPrefab ()
 Constructs an empty prefab with no UUID, name, or root subtree.
 ~kPrefab ()
 Destroys the prefab and releases its stored JSON.
bool loadFromFile (const kString &path)
 Loads a .prefab JSON file from disk into this prefab.
bool saveToFile (const kString &path)
 Writes this prefab to a .prefab JSON file.
kString getUuid () const
 Returns the prefab asset UUID.
void setUuid (const kString &v)
 Sets the prefab asset UUID.
kString getName () const
 Returns the display name of the prefab.
void setName (const kString &v)
 Sets the display name of the prefab.
const jsongetRootJson () const
 Returns the object subtree JSON (the "root" field).
jsongetRootJson ()
 Returns a mutable reference to the object subtree JSON (the "root" field).
void setRootJson (const json &j)
 Replaces the object subtree JSON.

Static Public Member Functions

static json instantiateJson (const json &source, std::unordered_map< kString, kString > *templateMap=nullptr)
 Generates a fresh-uuid copy of an object subtree.

Detailed Description

A serialized object subtree saved as a .prefab JSON asset.

A prefab stores a tree of objects (transforms, scripts, components) rooted at a single object. Multiple prefab instances can be placed in scenes; each instance gets fresh per-node UUIDs while remembering the template UUID it was derived from, so the editor can match nodes across instances and the template.

On-disk format:

{
"type": "prefab",
"uuid": "<asset uuid>",
"name": "<display name>",
"root": { ... full kObject::serialize() output ... }
}
virtual json serialize()
Serialises the object to a JSON value.

Constructor & Destructor Documentation

◆ kPrefab()

kemena::kPrefab::kPrefab ( )

Constructs an empty prefab with no UUID, name, or root subtree.

◆ ~kPrefab()

kemena::kPrefab::~kPrefab ( )

Destroys the prefab and releases its stored JSON.

Member Function Documentation

◆ getName()

kString kemena::kPrefab::getName ( ) const
inline

Returns the display name of the prefab.

◆ getRootJson() [1/2]

json & kemena::kPrefab::getRootJson ( )
inline

Returns a mutable reference to the object subtree JSON (the "root" field).

◆ getRootJson() [2/2]

const json & kemena::kPrefab::getRootJson ( ) const
inline

Returns the object subtree JSON (the "root" field).

◆ getUuid()

kString kemena::kPrefab::getUuid ( ) const
inline

Returns the prefab asset UUID.

◆ instantiateJson()

json kemena::kPrefab::instantiateJson ( const json & source,
std::unordered_map< kString, kString > * templateMap = nullptr )
static

Generates a fresh-uuid copy of an object subtree.

Walks the JSON, replaces every "uuid" with a freshly generated UUID, and stores the original UUID into "template_uuid". Used when instantiating a prefab into a scene so each instance has its own identity but can still be matched back to the template.

Parameters
sourceObject subtree (output of kObject::serialize()).
templateMapOptional out: maps template_uuid -> instance_uuid for every node in the subtree.
Returns
Renamed copy of source, ready to be deserialized into a scene.

◆ loadFromFile()

bool kemena::kPrefab::loadFromFile ( const kString & path)

Loads a .prefab JSON file from disk into this prefab.

Parameters
pathFilesystem path to the .prefab file.
Returns
true on success, false if the file is missing or unparseable.

◆ saveToFile()

bool kemena::kPrefab::saveToFile ( const kString & path)

Writes this prefab to a .prefab JSON file.

Parameters
pathFilesystem path to write to.
Returns
true on success, false if the file cannot be opened.

◆ setName()

void kemena::kPrefab::setName ( const kString & v)
inline

Sets the display name of the prefab.

◆ setRootJson()

void kemena::kPrefab::setRootJson ( const json & j)
inline

Replaces the object subtree JSON.

◆ setUuid()

void kemena::kPrefab::setUuid ( const kString & v)
inline

Sets the prefab asset UUID.


The documentation for this class was generated from the following file: