Forum

Programmatically cr...
 
Notifications
Clear all

[Solved] Programmatically creating ActiveEffects on Actors problem...

Celestian
(@celestian)
Initiate

I've been trying to apply ActiveEffects to a target programmatically and running into a problem. The code is this:

async function applyActionEffect(sourceActor, targetActor, action) {
    console.log("effects.js", "applyActionEffect", { sourceActor, targetActor, action });
    const effectDuration = new Roll(String(action.effectList[0].durationFormula)).roll().total;

    const effect = ActiveEffect.create({
        label: action.name,
        icon: action.img,
        // origin: `${targetActor.uuid}`,
        _id: randomID(16),
        "duration.rounds": effectDuration,
        // transfer: false,
        "duration.startTime": game.time.worldTime,
        "changes.key": action.effectList[0].key,
        "changes.value": action.effectList[0].value,
        "changes.mode": action.effectList[0].mode
    }, targetActor);

    await effect.create();

    console.log("effects.js", "applyActionEffect", { effect });
}

I've tried without the "effect.create()" (which I think is necessary) and the code executes w/o errors but no ActiveEffects seem to be created anywhere. WITH the effect.create() I get the following error:

foundry.js:3780 TypeError: _0xc6d52b.map is not a function
at ActiveEffect._initialize (C:\DnD\FoundryVTT\resources\app\dist\database\odm\abstract.js2750)
at new AbstractBaseDocument (C:\DnD\FoundryVTT\resources\app\dist\database\odm\abstract.js1559)
at new EmbeddedDocument (C:\DnD\FoundryVTT\resources\app\dist\database\odm\embedded.js2266)
at new ActiveEffect (C:\DnD\FoundryVTT\resources\app\dist\database\embedded\active-effect.js1604)
at Function.create (C:\DnD\FoundryVTT\resources\app\dist\database\odm\embedded.js3721)
at Function._onCreate (C:\DnD\FoundryVTT\resources\app\dist\database\odm\embedded.js6085)
at _onModifyDocument (C:\DnD\FoundryVTT\resources\app\dist\database\odm\embedded.js5489)
at async Socket.handleEvent (C:\DnD\FoundryVTT\resources\app\dist\sockets.js2262)

Obviously I am missing something but im at a loss as to what.

 

Quote
Topic starter Posted : 23/02/2021 8:42 pm
Celestian
(@celestian)
Initiate

The solution to this was the following.

I didnt realize .changes* was an array. Once I removed it this worked. Still have some fine tuning but, so far it's working as I expected.

    const effect = ActiveEffect.create({
        label: action.name,
        icon: action.icon,
        origin: `${targetActor.uuid}`,
        _id: randomID(16),
        "duration.rounds": effectDuration,
        transfer: false,
        "duration.startTime": game.time.worldTime,
        "changes": [
            {
                "key": action.effectList[0].key,
                "value": action.effectList[0].value,
                "mode": action.effectList[0].mode
            }
        ]
    }, targetActor).create();
ReplyQuote
Topic starter Posted : 24/02/2021 12:43 am
JDW liked
Lokdal
(@lokdal)
Candidate

Thank you so much for posting about this, I just ran into this problem.

Cheers!

ReplyQuote
Posted : 06/03/2021 1:33 am
Share: