How to trigger onEnable event in nodejs

How to trigger onEnable event in nodejs
0.0 0

#1

Hi im trying to implement cocos logic in nodejs server. Currently i can create node, add collider component and listen to the collider fine follow this code so far :slight_smile:

//predefine Editor variable
global.Editor = {
    isMainProcess: true,
	versions:{},
	log:function(str){
		console.log(str);
	}
};
//predefine process.versions.electron variable
process.versions.electron = "";
require("./engine-master");
//simulate window variable
window = {
	addEventListener:function(str,cb,b){
		cb();
	},
	removeEventListener:function(){},
	setTimeout:function(cb,time_out){
		setTimeout(cb,time_out);
	}
};
//simulate document variable
document = {};
//predefine cc.sys.capabilities variable
cc.sys.capabilities = {
        "canvas": false,
        "opengl": false,
        "webp": false
    };
require("./engine-master/CCBoot");
console.log("cc.ENGINE_VERSION",cc.ENGINE_VERSION);
cc.game._initRenderer = function(){};
//Config here
cc.initEngine({
	width: 1280,
	height: 720,
	debugMode: 0,
	exposeClassName: false,
	showFPS: false,
	frameRate: 60,
	id: "kem_game_portal",
	renderMode: 0,
	//registerSystemEvent: "registerSystemEvent",???
	//scenes: "scenes",?? maybe no need
	groupList : ['Bullet', 'Creep'],
	collisionMatrix:[[false, true], [true, false]]
},function(){
	cc._engineLoaded = true;
});
cc._initDebugSetting(0);
require("./engine-master/cocos2d");
require("./engine-master/cocos2d/actions");
require("./engine-master/cocos2d/core/CCScheduler");
require("./engine-master/cocos2d/core/CCNode");
require("./engine-master/cocos2d/core/components/CCComponent");
require("./engine-master/cocos2d/core/renderer");
require("./engine-master/cocos2d/core/collider");

//No Render
cc.renderer = {clear:function(){},rendering:function(){},pushDirtyNode:function(){}};
//creating a director instance
cc.director = cc.Director._getInstance();
//Enable collision manager
cc.director.getCollisionManager().enabled = true;
//Rewrite Main Loop like in normal game ( not editor )
cc.director.mainLoop = function () {
	this.calculateDeltaTime();
	if (!this._paused) {
		this.emit(cc.Director.EVENT_BEFORE_UPDATE);
		this._compScheduler.startPhase();
		this._compScheduler.updatePhase(this._deltaTime);
		this._scheduler.update(this._deltaTime);
		this._compScheduler.lateUpdatePhase(this._deltaTime);
		this.emit(cc.Director.EVENT_AFTER_UPDATE);
		cc.Object._deferredDestroy();
	}
}

//Run game
cc.game.run();




//Creating Node Top Right

var top_right_node = new cc.Node();
top_right_node.name = "Top Right";
top_right_node.setPosition(cc.p(1280,720));
top_right_node.runAction(new cc.MoveTo(0.5,cc.p(640,360)));

//Creating Node Bottom Left

var bottom_left_node = new cc.Node();
bottom_left_node.name = "Bottom Left";
bottom_left_node.setPosition(cc.p(0,0));
bottom_left_node.runAction(new cc.MoveTo(0.5,cc.p(640,360)));


//Creating collider component
var c1 = top_right_node.addComponent(cc.CircleCollider);
var c2 = bottom_left_node.addComponent(cc.CircleCollider);
c1.radius = 50;
c2.radius = 50;
//This does not trigger the onEnable event so it is not added to collider list
c1.active = true;
c1.enabled = true;
//Must be called manual
c1.onEnable();
c2.active = true;
c2.onEnable();
var NodeColliderEventComponent = cc.Class({
        name: 'NodeColliderEventComponent',
        extends: cc.Component,
        onCollisionEnter: function (other,s) {
			var other_node_name = other.node.name;
			var my_node_name = s.node.name;
			console.log(my_node_name+" get hit by "+other_node_name);
        },

        onCollisionExit: function () {
        },

        onCollisionStay: function () {
        }
    });
top_right_node.addComponent(NodeColliderEventComponent);
bottom_left_node.addComponent(NodeColliderEventComponent);
//Setting group for Top right node
top_right_node.group = "Bullet";
//Setting group for Bottom left node
bottom_left_node.group = "Creep";


/* * TEST **/
var game_time = (new Date()).getTime();
var game_elapse = 0;
function format_number(n){
	return Math.floor(n*100)/100;
}
var Timer = setInterval(function(){
	var d = (new Date()).getTime();
	var dt = (d-game_time)/1000;
	game_elapse+=dt;
	game_time = d;
	if(game_elapse<0.6){
		console.log(format_number(game_elapse),format_number(top_right_node.x),format_number(top_right_node.y),format_number(bottom_left_node.x),format_number(bottom_left_node.y));
	}
},1000/60); 

I want to ask how to trigger the onEnable method of the component without to call it manual like in the code. I tried :

c1.active = true;
c1.enabled = true;

But it is not working, so i have to call it manually

c1.onEnable();

So what is the correct way to trigger the component onEnable method? I search the engine-master with the keyword onEnable, there is some noticeable variable listed below, maybe is there some config that i must define ?

var IsOnEnableCalled = Flags.IsOnEnableCalled;
var IsEditorOnEnableCalled = Flags.IsEditorOnEnableCalled;
var callOnEnableInTryCatch = CC_EDITOR && callerFunctor('onEnable');
var IsOnEnableCalled = CCObject.Flags.IsOnEnableCalled;

#2

Hmm, the goal i want to reach is add the collider component to the collisionManager, i see there is only the code

onEnable: function () {
		console.log("on enable Adding collider========================");
        cc.director.getCollisionManager().addCollider(this);
    }

in CCCollider.js, so i think trigger the enable is the only way to do that.