Studio GuideWorld SDK Guide
Log In

Script Import

Other that being attached to a GameObject, ZEPETOScript can be used directly by other scripts.

You can simply import and use the ZEPETOScript the same way you would libraries or modules. In TypeScript syntax, you need to use the relative path to the ZEPETOScript file.

Considering the situation bellow, if you want to import ExtraComponent into ScriptImport, you would need to use the following syntax.

The following example is a declaration in ScriptImport.ts to call ExtraComponent.TS located in the lib folder.

import ExtraComponent from './lib/ExtraComponent';

200

Refer to the script file location

👍

Tips

  • It's important to specify the path correctly when importing another TS file.
  • Please familiarize yourself with the relative path notation.
  • ./ denotes the current directory.
  • ../ denotes the parent directory, i.e., the directory one level up from the current directory.

You can access the exported functions and variables declared in the imported script by directly referencing them, like shown in the sample.

this.gameObject.AddComponent<ExtraComponent>();
const extraComponent = this.gameObject.GetComponent<ExtraComponent>();

// Get value by method call
const count = extraComponent.GetCount();
// Set value by method call
extraComponent.SetCount(0);
// Get public property
const resultString = extraComponent.stringProperty;

The following is a sample code showing the imported TypeScript.

import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { Text } from 'UnityEngine.UI';
// Import custom script from path
import ExtraComponent from './lib/ExtraComponent';

export default class ScriptImport extends ZepetoScriptBehaviour {

    public resultUI: Text;
    private extComponent: ExtraComponent;

    Start() {
        // Add script component
        this.gameObject.AddComponent<ExtraComponent>();
        this.extComponent = this.gameObject.GetComponent<ExtraComponent>();
    }

    Update() {
        // Get value by method call
        const count = this.extComponent.GetCount();

        if (count > 10) {
            // Set value by method call
            this.extComponent.SetCount(0);
        }

        // Get public property
        const resultString = this.extComponent.stringProperty;

        // Print result
        console.log(`result : ${resultString}`);
        this.resultUI.text = resultString;
    }
}

Sample code for ExtraComponents.

import { ZepetoScriptBehaviour } from 'ZEPETO.Script';

export default class ExtraComponent extends ZepetoScriptBehaviour {
    
    public stringProperty: string;
    private message: string;
    private count: int;

    Start() {
        this.message = "Hi zepeto!";
        this.count = 0;
    }
     
    Update() {
        this.stringProperty = `${this.message} : ${this.count++}`;
    }
     
    GetCount() {
        return this.count;
    }
     
    SetCount(newCount: int) {
        this.count = newCount;
    }

}

Check out the test code output screen below.