Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Problem with drawn rectangle

dekon_17
Registered User
Quote
2021-09-28 15:08:28

The previously drawn (by my script) rectangles aren't disappearing, so, this is what I get as a result after the pistol inspection animation plays in the begining of a level:
🔎︎

So... How do I make them disappear after a new one is created on screen?
Also, to better understand this, here's the code:
/*
<behavior jsname="behavior_aim" description="Look how bullet will fly">
<property name="StartPoint" type="scenenode"/>
<property name="EndPoint" type="scenenode"/>
<property name="Crosshair" type="texture"/>
<property name="Crosshair_Size_X" type="float"/>
<property name="Crosshair_Size_Y" type="float"/>
</behavior>
*/

behavior_aim = function()
{
};

behavior_aim.prototype.onAnimate = function()
{
var SP = ccbGetSceneNodeProperty(this.StartPoint, "PositionAbs");
var EP = ccbGetSceneNodeProperty(this.EndPoint, "PositionAbs");
var Col = ccbGetCollisionPointOfWorldWithLine(SP.x, SP.y, SP.z, EP.x, EP.y, EP.z);
if (this.Col == null)
{
var Pos2D = ccbGet2DPosFrom3DPos(EP.x, EP.y, EP.z);
}
else
{
var Pos2D = ccbGet2DPosFrom3DPos(Col.x, Col.y, Col.z);
};

function onFrameDrawing()
{
ccbDrawColoredRectangle(0x77ff0000, Pos2D.x - 100, Pos2D.y - 100, Pos2D.x + 100, Pos2D.y + 100);
}
ccbRegisterOnFrameEvent(onFrameDrawing);
}



sven
Registered User
Quote
2021-09-28 15:49:13

behavior_aim.prototype.onAnimate = function()
{
var SP = ccbGetSceneNodeProperty(this.StartPoint, "PositionAbs");
var EP = ccbGetSceneNodeProperty(this.EndPoint, "PositionAbs");
var Col = ccbGetCollisionPointOfWorldWithLine(SP.x, SP.y, SP.z, EP.x, EP.y, EP.z);
if (this.Col == null)
{
var Pos2D = ccbGet2DPosFrom3DPos(EP.x, EP.y, EP.z);
}
else
{
var Pos2D = ccbGet2DPosFrom3DPos(Col.x, Col.y, Col.z);
};

//INSIDE A LOOP.................................................................
function onFrameDrawing()//PROBLEM
{//PROBLEM
ccbDrawColoredRectangle(0x77ff0000, Pos2D.x - 100, Pos2D.y - 100, Pos2D.x + 100, Pos2D.y + 100);//PROBLEM
}//PROBLEM
ccbRegisterOnFrameEvent(onFrameDrawing);//PROBLEM
//..................................................................................................

}



sven
Registered User
Quote
2021-09-28 16:22:40

Example ccb download:
https://drive.google.com/file/d/...


/*
<behavior jsname="behavior_aim" description="Look how bullet will fly">
<property name="StartPoint" type="scenenode"/>
<property name="EndPoint" type="scenenode"/>
<property name="Crosshair" type="texture"/>
<property name="Crosshair_Size_X" type="float"/>
<property name="Crosshair_Size_Y" type="float"/>
</behavior>
*/

p2d=0;
behavior_aim = function()
{
};

behavior_aim.prototype.onAnimate = function()
{
var SP = ccbGetSceneNodeProperty(this.StartPoint, "PositionAbs");
var EP = ccbGetSceneNodeProperty(this.EndPoint, "PositionAbs");
var Col = ccbGetCollisionPointOfWorldWithLine(SP.x, SP.y, SP.z, EP.x, EP.y, EP.z);

if (Col == null)
{
p2d = ccbGet2DPosFrom3DPos(EP.x, EP.y, EP.z);
}
else
{
p2d = ccbGet2DPosFrom3DPos(Col.x, Col.y, Col.z);
};

}

function onFrameDrawing()
{
ccbDrawColoredRectangle(0x77ff0000, p2d.x - 100, p2d.y - 100, p2d.x + 100, p2d.y + 100);
}
ccbRegisterOnFrameEvent(onFrameDrawing);



sven
Registered User
Quote
2021-09-28 16:37:30

As you know you can..
ccbRegisterOnFrameEvent(onFrameDrawing);

And you also can unregister it as..
ccbUnregisterOnFrameEvent(onFrameDrawing);
(no rect will be drawn after this)

And you can re-register it again if needed..
ccbRegisterOnFrameEvent(onFrameDrawing);


dekon_17
Registered User
Quote
2021-09-28 16:42:58

So, I tried to do this (but I placed Pos2D instead of p2d) and, surprizingly, it works. I still don't understand how one little thing can do this, but okay. Also, thanks, sven.


dekon_17
Registered User
Quote
2021-09-28 16:46:59

Also, am I right that only change was a global variable (or something like that) that fixed it? And, yeah, won't it be bad for framerate?


sven
Registered User
Quote
2021-09-28 17:01:16

Global variables has no effect for framerate.
I dont know how to pass variable into that function thats why i used global.
( i am not that pro with javascript :D )

If you compare my script and your script then you see some more difference.. you had atleast 3 errors that i fixed.


dekon_17
Registered User
Quote
2021-09-28 17:12:51

Oh, I think I found those errors too: "this" in "this.Col". Also, I kinda changed the script, so, now it makes a little crosshair:
/*
<behavior jsname="behavior_aim" description="Look how bullet will fly">
<property name="StartPoint" type="scenenode"/>
<property name="EndPoint" type="scenenode"/>
<property name="Crosshair" type="texture"/>
<property name="Crosshair_Size_X" type="float"/>
<property name="Crosshair_Size_Y" type="float"/>
</behavior>
*/

Pos2D = 0;
behavior_aim = function()
{
};

behavior_aim.prototype.onAnimate = function()
{
var SP = ccbGetSceneNodeProperty(this.StartPoint, "PositionAbs");
var EP = ccbGetSceneNodeProperty(this.EndPoint, "PositionAbs");
var Col = ccbGetCollisionPointOfWorldWithLine(SP.x, SP.y, SP.z, EP.x, EP.y, EP.z);
if (Col == null)
{
Pos2D = ccbGet2DPosFrom3DPos(EP.x, EP.y, EP.z);
}
else if (Col)
{
Pos2D = ccbGet2DPosFrom3DPos(Col.x, Col.y, Col.z);
};

function onFrameDrawing()
{
ccbDrawColoredRectangle(0x35ffffff, Pos2D.x - 30, Pos2D.y - 2, Pos2D.x - 10, Pos2D.y + 2);
ccbDrawColoredRectangle(0x35ffffff, Pos2D.x + 10, Pos2D.y - 2, Pos2D.x + 30, Pos2D.y + 2);
ccbDrawColoredRectangle(0x35ffffff, Pos2D.x - 2, Pos2D.y + 10, Pos2D.x + 2, Pos2D.y + 30);
}
ccbRegisterOnFrameEvent(onFrameDrawing);
}

This is better than a red square in the center, so, this would be good!
🔎︎
🔎︎



sven
Registered User
Quote
2021-09-28 17:25:47

function onFrameDrawing()
{
ccbDrawColoredRectangle(0x35ffffff, Pos2D.x - 30, Pos2D.y - 2, Pos2D.x - 10, Pos2D.y + 2);
ccbDrawColoredRectangle(0x35ffffff, Pos2D.x + 10, Pos2D.y - 2, Pos2D.x + 30, Pos2D.y + 2);
ccbDrawColoredRectangle(0x35ffffff, Pos2D.x - 2, Pos2D.y + 10, Pos2D.x + 2, Pos2D.y + 30);
}
ccbRegisterOnFrameEvent(onFrameDrawing);
}


This part is not correct.. look how i made it..
right now you have function in a loop and you registrering it every loop.(and that will affect framerate for sure in long run)


Should be:
/*
<behavior jsname="behavior_aim" description="Look how bullet will fly">
<property name="StartPoint" type="scenenode"/>
<property name="EndPoint" type="scenenode"/>
<property name="Crosshair" type="texture"/>
<property name="Crosshair_Size_X" type="float"/>
<property name="Crosshair_Size_Y" type="float"/>
</behavior>
*/

Pos2D = 0;
behavior_aim = function()
{
};

behavior_aim.prototype.onAnimate = function()
{
var SP = ccbGetSceneNodeProperty(this.StartPoint, "PositionAbs");
var EP = ccbGetSceneNodeProperty(this.EndPoint, "PositionAbs");
var Col = ccbGetCollisionPointOfWorldWithLine(SP.x, SP.y, SP.z, EP.x, EP.y, EP.z);
if (Col == null)
{
Pos2D = ccbGet2DPosFrom3DPos(EP.x, EP.y, EP.z);
}
else if (Col)
{
Pos2D = ccbGet2DPosFrom3DPos(Col.x, Col.y, Col.z);
};


}

function onFrameDrawing()
{
ccbDrawColoredRectangle(0x77ff0000, Pos2D.x - 30, Pos2D.y - 2, Pos2D.x - 10, Pos2D.y + 2);
ccbDrawColoredRectangle(0x77ff0000, Pos2D.x + 10, Pos2D.y - 2, Pos2D.x + 30, Pos2D.y + 2);
ccbDrawColoredRectangle(0x77ff0000, Pos2D.x - 2, Pos2D.y + 10, Pos2D.x + 2, Pos2D.y + 30);
}
ccbRegisterOnFrameEvent(onFrameDrawing);










dekon_17
Registered User
Quote
2021-09-28 17:44:22

Oh... Well, looks like I should've look at your script better. Really didn't saw that. Thanks for noting this mistake, sven!


just_in_case
Moderator
Quote
2021-09-29 06:10:15

yeah, @sven is right, you were registering the event onAimate function, either you will get a script temp cache[] error, or it will go in a loop. and will crash your game after some time.

I am glad that we have many good programmers here like @sven who help others in learning the scripting part of the engine. cause that's the topic generally people avoid answering, I have also avoided some of the threads that deal with scripting, one reason can be that I am not that good a programmer yet, and another reason is sometimes it can be a pain to debug scripts.

So Kudos to @sven for helping the members with scripting :)


dekon_17
Registered User
Quote
2021-09-29 14:00:51

You were right, sven, now it is kinda better on framerate. But, well, now I have problem with white color. I tried to use 0xffffffff, but it gives me black color, which is strange.
🔎︎

Also, 0xff000000 does black color too.
🔎︎

Had I picked the color wrong?


sven
Registered User
Quote
2021-09-29 19:11:47

just_in_case Thank you for your nice words.
I help others so i can learn from doing it.. i get good ideas out of helping others to solve their scripting problems.

dekon_17
This should do the trick: now you can choose color in editor.


/*
<behavior jsname="behavior_rectWithColor" description="behavior_rectWithColor">
<property name="StartPoint" type="scenenode"/>
<property name="EndPoint" type="scenenode"/>
<property name="Rcolor" type="color" default="ffffffffff" />
</behavior>
*/

behavior_rectWithColor = function()
{
var me = this;
this.registeredFunction = function() {me.DrawFunc();};
ccbRegisterOnFrameEvent(this.registeredFunction);
}

behavior_rectWithColor.prototype.onAnimate = function()
{
}

behavior_rectWithColor.prototype.DrawFunc = function()
{
var Pos2D=0;
var SP = ccbGetSceneNodeProperty(this.StartPoint, "PositionAbs");
var EP = ccbGetSceneNodeProperty(this.EndPoint, "PositionAbs");
var Col = ccbGetCollisionPointOfWorldWithLine(SP.x, SP.y, SP.z, EP.x, EP.y, EP.z);
if (Col == null)
{
Pos2D = ccbGet2DPosFrom3DPos(EP.x, EP.y, EP.z);
}
else if (Col)
{
Pos2D = ccbGet2DPosFrom3DPos(Col.x, Col.y, Col.z);
};

ccbDrawColoredRectangle(this.Rcolor, Pos2D.x - 30, Pos2D.y - 2, Pos2D.x - 10, Pos2D.y + 2);
ccbDrawColoredRectangle(this.Rcolor, Pos2D.x + 10, Pos2D.y - 2, Pos2D.x + 30, Pos2D.y + 2);
ccbDrawColoredRectangle(this.Rcolor, Pos2D.x - 2, Pos2D.y + 10, Pos2D.x + 2, Pos2D.y + 30);
}



dekon_17
Registered User
Quote
2021-09-29 20:05:03

So, it actually works! But the thing with the colors that I have is kinda strange, because I did everything like the documentation said (about colors). However, I see you have used the different system of script. Isn't it... works with the previous one? Just for information.


just_in_case
Moderator
Quote
2021-10-01 11:16:21

Just added a new section to my website Script Snippets that will contain some code snippets to help with coppercube.

Just posted first code snippets in there, that will solve your problem of color.

@sven you should also check that code snippet out. it will be helpful for you as well.

https://neophyte.cf/JS_script_sn...

hope it helps


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internatio?al" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact