Typescript - How to Correctly access an Effect?

0 favourites
  • 3 posts
From the Asset Store
A total of 214 high quality and unique magic sound effects suitable for RPG, Battle Arena and more!
  • Hey folks, back once again with a bit of typescript weirdness!

    This time it is around effects and how to access them in typescript

    First the obvious -

    Please find a zip of the issue recreated in the example project to assist

    GhostShooter

    Yes I have regenerated the Typescript Definitions on the project

    Yes I am ensuring the Typescript is built and the project reimports the scripts on build

    Setup

    I have added the following method to the monster.ts file, and am calling it when the HP gets to approx half.

     ChangeColour1() {
     this.effects.AdjustHSL.isActive = true;
     }
    

    The Error

    Shown below is the stack trace that occurs when this method is executed, and the debug of the line in question

    The debug shows us that correctly, yes the named property 'AdjustHSL' does not exist. Of course attempting to access directly via [] does not compile, as shown in ChangeColour2() in the example

    The Current Hack

    So I have got a 'solution', but definitely feels like a hack!

    Essentially casting the Instance effects Object to an array, then finding by name

    export function getEffectType(effectArray: any , name: string): IEffectInstance | null {
     const hackCast = effectArray as IEffectInstance[];
     const found = hackCast.find((ef) => ef.name == name);
     return found != null ? found as IEffectInstance : null;
    }
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is actually a bug in Construct's TypeScript definitions: IWorldInstance.effects is actually an array of IEffectInstance, but it generates a type definition with a named map like instance variables and behaviors. So the correct usage is to treat it like an array, and to make TypeScript happy, for now work around it by casting to IEffectInstance[].

    As we're close to a stable release the fix for this is in the next release cycle (first beta after the next stable).

  • Hey again Ashley

    I thought that might be the case, thanks for letting me know!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)