Giter Club home page Giter Club logo

Comments (5)

Omikhleia avatar Omikhleia commented on September 25, 2024

All world objects have an addTrait method. So you would first create your "lib.traits.attackable" object with the suitable methods and properties, and add it to some other object.

addTrait( ...WorldObject ) ⇒ Integer

Adds traits to the object and returns the number of traits. Traits are what makes the object inherit properties and methods.

Warning: it doesn't prevent from adding an already existing trait. Maybe it should, otherwise the object gets broken, with a 'duplicate parent'.

from room.js.

Omikhleia avatar Omikhleia commented on September 25, 2024

By the way, maybe your question is how to create a new trait in the first place. One possible way among others:

lib.root.new('lib.traits.attackable') // Spawn new object
lib.traits.attackable.rmTrait(lib.root) // Detach from root, for the trait to be standalone
// Then add properties, functions and verbs to lib.traits.attackable
...
// Then add this trait to some object, e.g. here to the baker NPC in the standard demo
items.baker.addTrait(lib.traits.attackable)

This should be what you asked for — Let me know if I can close the question.

from room.js.

dhaveman avatar dhaveman commented on September 25, 2024

if i add a new folder/files under the traits folder of a world, where do i get it to recognize that trait? can you point me to where it reads in all the "built in" traits? thanks!

i guess my main question is can i add a trait by just adding files/folders in the right spot?

something like:
world/lib/traits/attackable/attackable.json and the respective verbs/listener functions

thanks for the response!

from room.js.

Omikhleia avatar Omikhleia commented on September 25, 2024

As I said, the above is one possible way. I tend to favor coding from within the game (e.g. toggling to EVAL mode with TAB), since it's a pretty neat feature (and less error-prone, in my opinion).

But yes, you can also work directly with files directly on disk, if that's your preference. And something such as world/lib/traits/attackable/attackable.json, provided it has the correct structure, will just map to lib.traits.attackable. There's a one-to-one mapping between folders on disk and Javascript objects available in-game.

There's nothing as "built-in" traits, though, so no "right spot", strictly speaking — You are free to organize your object hierarchy as you wish (*). Traits are just other objects you use for inheritance. You could as well decide to create combat.classes.attackable (i.e. on disk, world/combat/classes/attackable/attackable.json) and use that as a trait. You organize your world code as you wish.

EDIT: (*) In the standard demo, lib.npc and lib.npcs.seller are such examples of objects used as traits (in items.baker, items.butcher, items.mouse, etc.) but not located in lib.traits.

from room.js.

dhaveman avatar dhaveman commented on September 25, 2024

Sounds good to me, thanks for the clarification and feedback!

from room.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.