Search code examples
typescriptscreeps

Typescript Function Not Found


Source code can be found here https://github.com/zevrant/screeps

I have an interface that is implemented by several classes, when i go to call the implemented interface i get the following error and i don't know why. I am a java developer and my only typescript experience is with Angular, so my knowledge of TypeScript is limited.

TypeError: spawn.memory.tasks[0].execute is not a function at _.forEach.spawn (../src/main.ts:70:30)

Edit1

logging the object

{"priority":10,"class":"SpawnCreep","outputs":{},"requirements":{},"sourceLocation":{"id":"2bdee70715ebc4e","room":{"name":"W3N7","energyAvailable":14,"energyCapacityAvailable":300,"visual":{"roomName":"W3N7"}},"pos":{"x":27,"y":16,"roomName":"W3N7"},"name":"Spawn1","energy":14,"energyCapacity":300,"spawning":{"name":"0u9tvotgf5cgw4ifhoewivg","needTime":15,"remainingTime":1},"store":{"energy":14},"owner":{"username":"Zevrant"},"my":true,"hits":5000,"hitsMax":5000,"structureType":"spawn"},"storageLocation":{"id":"2bdee70715ebc4e","room":{"name":"W3N7","energyAvailable":14,"energyCapacityAvailable":300,"visual":{"roomName":"W3N7"}},"pos":{"x":27,"y":16,"roomName":"W3N7"},"name":"Spawn1","energy":14,"energyCapacity":300,"spawning":{"name":"0u9tvotgf5cgw4ifhoewivg","needTime":15,"remainingTime":1},"store":{"energy":14},"owner":{"username":"Zevrant"},"my":true,"hits":5000,"hitsMax":5000,"structureType":"spawn"},"creep":{"id":"2bdee70715ebc4e","room":{"name":"W3N7","energyAvailable":14,"energyCapacityAvailable":300,"visual":{"roomName":"W3N7"}},"pos":{"x":27,"y":16,"roomName":"W3N7"},"name":"Spawn1","energy":14,"energyCapacity":300,"spawning":{"name":"0u9tvotgf5cgw4ifhoewivg","needTime":15,"remainingTime":1},"store":{"energy":14},"owner":{"username":"Zevrant"},"my":true,"hits":5000,"hitsMax":5000,"structureType":"spawn"}}

Solution

  • if you store the class instance in the memory, it gets converted into a JSON representation, ie looses all the functions.

    You will need to create an empty class and set the values and invoke the function.

    const spawnCreep = new SpawnCreep()

    assign spawn.memory.tasks[0] to spawnCreep

    and call spawnCreep.execute()