Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Utilizing the API to create NPC's

October 24 (1 year ago)

I have the following script, I'm trying to use to create an NPC. It creates the NPC fine, but doesn't upload the stats, when I check in my game, it prompts with charactermancer. Is there any way 

on('ready', function() {

    // Check if the NPC statblock already exists to avoid duplicates.

    var existingNPC = findObjs({

        type: 'character',

        name: 'Fighter (Tier 1)'

    })[0];


    if (!existingNPC) {

        // Create the Fighter NPC

        var fighter = createObj('character', {

            name: 'Fighter (Tier 1)',

            avatar: '', // Add a link to an image if you have one

            bio: 'Medium or Small humanoid, any alignment.',

            gmnotes: '', // Any notes for the GM only

        });


        // Set the attributes

        var attributes = [

            {name: 'sheet_type', current: 'npc'},  // This is the crucial attribute to set the sheet to NPC mode

            {name: 'npc_ac', current: 14},

            {name: 'npc_hpformula', current: '4d10 + 12'},

            {name: 'npc_speed', current: '30 ft.'},

            {name: 'strength', current: 16},

            {name: 'dexterity', current: 13},

            {name: 'constitution', current: 16},

            {name: 'intelligence', current: 12},

            {name: 'wisdom', current: 10},

            {name: 'charisma', current: 14},

            // ... add more attributes specific to the NPC statblock

        ];


        _.each(attributes, function(attr) {

            createObj('attribute', {

                characterid: fighter.id,

                name: attr.name,

                current: attr.current,

                max: attr.max || ''

            });

        });

        // Set abilities (skills, special moves, etc.)

        var abilities = [

            {

                name: 'Action Surge',

                action: 'The fighter can take one additional action. (1/Short or Long Rest)',

                istokenaction: true

            },

            {

                name: 'Maneuvers',

                action: 'When the fighter takes the attack action, they can choose one weapon attack to deal an extra 1d8 damage (same damage type as the attack) and cause Distraction.',

                istokenaction: true

            },

            // ... add more abilities as needed

        ];


        _.each(abilities, function(abil) {

            createObj('ability', {

                characterid: fighter.id,

                name: abil.name,

                action: abil.action,

                istokenaction: abil.istokenaction || false

            });

        });

    }

});


October 24 (1 year ago)

Edited October 24 (1 year ago)

Problem with a mod creating NPCs is that charactersheets are evolving. Though the semantics of most fields stay the same over time, there are constant changes. None of it open or documented. It is safer create NPCs manually once in a reference game and then use the transmogrifier to copy these NPCs over.
Having said this: if you add the following attiributes, you'll directly see the NPC
  {name: 'l1mancer_status', current: 'completed'},

  {name: 'npc', current: 1},