Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Turret problem

dekon_17
Registered User
Quote
2021-09-02 16:22:05

Alright, because my previous thread name isn't about the current state of script, I'll try to write here.
So, first of all, it appears that the turret won't unregister the shoot function after player's death and the scene reset. Which means that after I got killed by turret and restarted level, it will stand still without rotation, but it will rapidly shoot.
🔎︎

Also, as you can see, it shoots incredibly fast. But the thing is that the AttackCD (cooldown) is on 2000, but for some reason firerate got attached to FPS. I don't know how, but this did happened, yet I am trying to fix this.
So, is there a solution to unregister the function after player died?


VP
Guest
Quote
2021-09-03 11:59:56

To disable the turret when restarting the scene -

1- you can use a simple variable to disable the turret (eg: If Active? = 1, then do......"

or

2- you can use "on proximity" to make the turret active when you're near it and inactive when you leave it's range.

As for the bug you mentioned - did you use the "Do Later" script in "Before first Draw" at all?


dekon_17
Registered User
Quote
2021-09-03 14:15:57

Well, I have a DoLater on a start. Two of them. It goes like "Before first draw", "If a variable has a value do something" and there "Do something later". It's just for sure.


VP
Guest
Quote
2021-09-04 22:43:32

OK, so I think that's the problem. The "Do Something Later" Script causes lots of bugs when placed in the "on first draw".

If you check out some of my posts on the [bugs] section, you'll find the solution - someone uploaded a new script/plugin which works perfectly.

If I find it, I'll post here, if not, you can find it by searching the posts - the post wasn't too long ago...


VP
Guest
Quote
2021-09-04 22:46:33

Here's the post I mentioned. Sorry to say it wasn't the "do something later" script, it was the "Animate Scale" script that caused problems when the "Do something Later" script was used in the "Before First Draw".

Either way, it was fixed by editing the script.....

https://www.ambiera.com/forum.ph...

Hope this is helpful for you.


dekon_17
Registered User
Quote
2021-09-05 14:48:51

Well... I don't use the "Animate Scale" script, so, I suppose this won't help me much. Also, maybe I did something wrong, but your solution about shooting after scene reset didn't really worked. Also, trying to make this, I broke the stopping of shooting somehow. So, I'll leave the code here:
/*
<behavior jsname = "behavior_TurretAI" description = "AI for turrets because why not">
<property name = "Turret" type = "scenenode"/>
<property name = "ToAttack" type = "scenenode"/>
<property name = "Radius" type = "float"/>
<property name = "DetectionRadius" type = "float"/>
<property name = "OnAttack" type = "action"/>
<property name = "AttackCD" type = "float"/>
</behavior>
*/

behavior_TurretAI = function()
{
this.LastTime = null;
var me = this;
this.registeredFunction = function() { me.angerFunc();};
};

behavior_TurretAI.prototype.onAnimate = function(node, timeMs)
{
if (this.LastTime == null)
{
ccbUnregisterOnFrameEvent(this.registeredFunction);
this.LastTime = timeMs;
var can_shoot = "no";
return false
}

var Tur_Pos = ccbGetSceneNodeProperty(this.Turret, "Position");
var Plr_Pos = ccbGetSceneNodeProperty(this.ToAttack, "Position");
var Col = ccbGetCollisionPointOfWorldWithLine(Tur_Pos.x, Tur_Pos.y, Tur_Pos.z, Plr_Pos.x, Plr_Pos.y, Plr_Pos.z);

//I took the distance calculation script part from "behavior_Test_fileV2" by Sven
var dist=Math.sqrt( Math.pow( (Tur_Pos.x-Plr_Pos.x) ,2) + Math.pow( (Tur_Pos.y-Plr_Pos.y) ,2) + Math.pow( (Tur_Pos.z-Plr_Pos.z) ,2) );

if (Col == null && dist <= this.DetectionRadius)
{
//I took the rotation script part from the "action_rotatetowardstarget" by just_in_case
var position = ccbGetSceneNodeProperty(this.Turret, "Position");
var rotation = ccbGetSceneNodeProperty(this.Turret, "Rotation");
var pos_target = ccbGetSceneNodeProperty(this.ToAttack, "Position");
var can_shoot = "yes";
var radians_Horizontal = Math.atan2(pos_target.x - position.x,pos_target.z - position.z);
var degree_Horizontal = (radians_Horizontal * (180 / Math.PI));

ccbSetSceneNodeProperty(this.Turret,"Rotation",rotation.x, degree_Horizontal, rotation.z);
};

if (dist <= this.Radius && can_shoot == "yes")
{
ccbRegisterOnFrameEvent(this.registeredFunction);
var can_shoot = "no";
}
else if (dist >> this.Radius && can_shoot == "no")
{
ccbUnregisterOnFrameEvent(this.registeredFunction);
can_shoot = "yes";
};
return true;
}

behavior_TurretAI.prototype.angerFunc = function()
{
var now = (new Date()).getTime();
if (CD == null)
{
var CD = 1;
ccbInvokeAction(this.OnAttack);
ST = (new Date()).getTime();
ET = ST + this.AttackCD;
var N = (new Date()).getTime();
};
if (N > ET && CD == 1)
{
var CD = null;
};
}



VP
Guest
Quote
2021-09-06 12:57:05

As mentioned before, I simply used the "turn towards" action and selected a target node for the turret to point to. This method definitely works for me without problems but I understand that you want to code it for yourself - I've no idea how to script though, so I can't check your code for you.

Having the "do something later" or the "animate scale" in "before first draw" definitely caused multiple problems for me on several projects (until I swapped out the "do something later" and "animate scale" scripts for alternatives suggested in the post I linked).

If you're convinced that's not the issue - other than uploading a ccb with a working example of my turret for you, I can't suggest anything more - so hopefully someone can fix your script for you instead.

Good luck - hope it gets fixed.


dekon_17
Registered User
Quote
2021-09-07 16:05:43

Well, guess I should do this with a wacky-clanky way, because the "DoLater" isn't the reason: I tested my script in clear project and nah, it didn't work well. So, I will do it in other ways. Anyway, thanks for trying to help, VP. Hope I'll get better at the JavaScript...


sven
Registered User
Quote
2021-09-07 16:41:54

I have made simple Turret script ..

Download CCB.
https://drive.google.com/file/d/...

There you can edit GUN magazin size..its firerate..its reload time (that you can use as cooldown counter also)

My script should be easy to understand and easy to modify as you need.







//Made By User sven at Ambiera Forum (2021 year)
/*
<behavior jsname="behavior_MOTT" description="behavior_MOTT">
<property name="Target" type="scenenode" default="Camera1" />
<property name="AI" type="scenenode" default="AI" />
<property name="Distance1" type="float" default="100" />
<property name="Bullets" type="int" default="5" />
<property name="ReloadTime" type="int" default="50" />
<property name="FireRate" type="int" default="15" />
<property name="Actions_On_Attack" type="action" />
</behavior>
*/

var radToDegA = 180.0 / 3.14159265359;

function calculateDistance(p1, p2) {
var a = p2.x - p1.x;
var b = p2.y - p1.y;
var c = p2.z - p1.z;
return Math.sqrt(a * a + b * b + c * c);
}

function calculateAngleY(p1, p2) {
pos_o_x=p1.x-p2.x;
pos_o_z=p1.z-p2.z;
angle_towards_target = Math.atan2(pos_o_x, pos_o_z) * radToDegA;
if (angle_towards_target < 0.0)
angle_towards_target += 360.0;
if (angle_towards_target >= 360.0)
angle_towards_target -= 360.0;
return angle_towards_target;
}

behavior_MOTT = function()
{
this.B =null;
this.RT=null;
this.FR=null;
}

behavior_MOTT.prototype.onAnimate = function()
{

if (this.RT==null) //its null on start for sure
{
this.RT=this.ReloadTime;//copy of input
}
if (this.B==null) //its null on start for sure
{
this.B=this.Bullets;//copy of input
}
if (this.FR==null) //its null on start for sure
{
this.FR=this.FireRate;//copy of input
}

var pt = ccbGetSceneNodeProperty(this.Target, "Position");
var pn = ccbGetSceneNodeProperty(this.AI, "Position");
var dist=calculateDistance(pt, pn);
can_see_me=ccbGetCollisionPointOfWorldWithLine(pn.x,pn.y+1.0,pn.z, pt.x, pt.y+1.0, pt.z);

if (dist <= this.Distance1 && can_see_me==null)//I see player and in range.
{

ccbSetSceneNodeProperty(this.AI, 'Rotation', 0.0, calculateAngleY(pt, pn), 0.0);//Rotate turret Y --Horizontal--

this.FR-=1;//firerate counter
if (this.FR==0 && this.B!=0)//if i have bullet and firerate counter allows me to shoot then do it.
{
ccbInvokeAction(this.Actions_On_Attack);//shoot
this.FR=this.FireRate;//reset firerate counter
this.B-=1;//remove bullet
}
if (this.B==0)//if no bullets in magazine
{
this.RT-=1;//reload timer counter
}
if (this.RT==0)//reload timer counter ended -magazin is full and ready to fire.
{
this.RT=this.ReloadTime;//reset
this.B= this.Bullets;// reset
this.FR=this.FireRate;//reset
}

}

return true;
}



dekon_17
Registered User
Quote
2021-09-08 14:31:22

Believe it or not, I found the reason of this mess. And guess where it was? In almost the begining of script!
if (Col == null && dist <= this.DetectionRadius)
{
//I took the rotation script part from the "action_rotatetowardstarget" by just_in_case
var position = ccbGetSceneNodeProperty(this.Turret, "Position");
var rotation = ccbGetSceneNodeProperty(this.Turret, "Rotation");
var pos_target = ccbGetSceneNodeProperty(this.ToAttack, "Position");
var can_shoot = "yes";
var radians_Horizontal = Math.atan2(pos_target.x - position.x,pos_target.z - position.z);
var degree_Horizontal = (radians_Horizontal * (180 / Math.PI));

ccbSetSceneNodeProperty(this.Turret,"Rotation",rotation.x, degree_Horizontal, rotation.z);
};

if (dist <= this.Radius && can_shoot == "yes")
{
ccbRegisterOnFrameEvent(this.registeredFunction);
var can_shoot = "no";
}
else if (dist >> this.Radius && can_shoot == "no")
{
ccbUnregisterOnFrameEvent(this.registeredFunction);
can_shoot = "yes";
};

So, the reason was the fact that the script every frame player was in detection radius made variable can_shoot get the yes condition... Should I say more? (Yeah, I should. I didn't saw this for a week or something, which means that I should've do a better code structure in order to see the stuff, because there is a lot of text so close to eachother).

And, well, this means that it's not over yet - I will try to make this thing working...

"Huh" and "eh" until it's "Bruh".
🔎︎



spicymilk
Registered User
Quote
2023-03-31 04:52:24

I know this thread is old but I can't say thankyou enough for this


dekon_17
Registered User
Quote
2023-04-01 13:13:03

I don't know how someone was even able to find this old thread, but damn, looking at the script I made there... Damn. This "can_shoot" part is especially horrible, since it's not boolean true or false but rather string "yes" or "no". I mean, I was only starting to learn programming back then, but this is probably bad even for a starter.


just_in_case
Moderator
Quote
2023-04-01 19:18:01

I can feel that I remember the first behavior I wrote "CheatSheet", now when I look at that behavior, I think man how bad I was, but then suddenly I start thinking in reverse, no man you weren't bad, you just improved your skills. I can now create way better scripts than those. I think at some point I will update those obsolete extensions and video tutorials on my website that I created years ago. I am thinking of creating a series of tutorials for Coppercube after my exams, at least one video every weekend.


spicymilk
Registered User
Quote
2023-04-03 04:06:27

I would love it if you did that I'm not very good at scripting with coppercube and need help


just_in_case
Moderator
Quote
2023-04-03 04:45:48

@spicymilk, there are already actions available on the neophyte.cf that you can use to create a sentry gun(turret), all you need is to use the action together with a shoot action.
here is the link to the actions that rotates and object toward another object https://neophyte.cf/ccb_action_r...


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internati?nal" (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