Hello, I am looking for a way to pass a process to another class.
I am looking to pass a process to another class.
Perhaps the following topic.
How to implement EventCustom in CC 3.0?
Looks like the class is no longer available in this version. Should I use Event instead?
And how to pass setUserData like in 2.x.x versions?
However, I could not figure out how to implement it after reviewing the conversation here and the following link that was posted.
https://docs.cocos.com/creator/3.0/manual/en/engine/event/event-emit.html#event-dispatching
For example, I have the following three methods in class A
showLogA(){
log("A"); }
}
showLogB(){
log("B"); }
}
showLogC(){
log("C"); }
}
If I pass any of these methods to class B and class B executes it at any given time, how should I describe it?
At this time, class B does not know about class A. Therefore, it does not mean that we make these methods public and call them.
The version of Cocos Creator is 3.8.2
zzy
April 23, 2024, 2:15pm
#2
Directly pass the method to class B, it’s that OK?
Yes, perhaps that is what I want to do.
zzy
April 23, 2024, 3:23pm
#4
Is this what you want?
import { _decorator, Component, Node } from 'cc';
import { ClassB } from './ClassB';
const { ccclass, property } = _decorator;
@ccclass('ClassA')
export class ClassA extends Component {
someData = "Hoooooooooooo";
start() {
let b = this.getComponent(ClassB);
b.addCb(this.methodOfA.bind(this));
}
methodOfA(){
console.log("I am a method of class A");
console.log(this.someData);
}
}
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('ClassB')
export class ClassB extends Component {
private cb: Function;
start() {
this.scheduleOnce(()=>{
this.cb?.();
}, 3);
}
public addCb(cb: Function){
this.cb = cb;
}
}
1 Like
Thank you for everything.
This method worked well for me!