The 'type' attribute of 'Script' is undefined when loading script

The 'type' attribute of 'Script' is undefined when loading script
0

#1

I’ve just started developing for cocos creator 2.0 using typescript and I’ve got a pair of classes that each reference each other in the editor However in editor I get the error “The ‘type’ attribute of ‘Weapon.playerController’ is undefined when loading script”. Additionally, the “PlayerController” property is always null and cannot have anything dragged into it. The classes are in separate files.

From what I can find it appears to be a circular reference and I did find a couple people with similar issues but they were using Javascript and it didn’t help me. Below is the relevant snippets, any suggestions?

PlayerController.ts:

import Weapon from "./Weapon";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PlayerController extends cc.Component 
{	
	@property(
	{
		visible: true,
		type: Weapon
	})
	private weapon: Weapon = null;

}

Weapon.ts:

import PlayerController from "./PlayerController";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Weapon extends cc.Component 
{	
	@property
	({
		type: PlayerController
	})
	private playerController : PlayerController = null;	
}

Untitled


#2

I have the same question, I worked around creating a reference to one node with this script and accessing to his property with .getComponent(String) method of Node.
it’s a really bad approach because because if one developer or artist modify the name of the script or the node… All will fail.

@property(cc.Node)
private weaponNode: cc.Node = null;

private weaponScript : Weapon = null;

onLoad(){
this.weaponScript = this.weaponNode.node.getComponent(“weaponScript”);
}

note: Is not necessary that the script it’s located in another node, you can use this.node.getComponent(String) if the script is in the same object.

@jare exist any cc type that reflects an script (javascript, typescript or coffescript) to avoid that?

Sorry, I can’t bring a better solution :frowning:


#3

Also I found a requireComponent in cc._decorator, so I think this is used for incuding necessary components when you apply the script to an node.


#4

I worked around creating a reference to one node with this script and accessing to his property with .getComponent(String) method of Node.

This is what I did in the end also. Not ideal but it worked in this instance.


#5

I really think one admin must explain this, because it’s a big deal if you are trying to implement any pattern or you are simply trying to access to an property of another script.
@slackmoehrle, @jare


#6

Sorry, for TypeScript, you can only split your classes to avoid circular reference.