Jump to content
Kelvin Lee

Tank AI won't use main gun to shoot target

Recommended Posts

Hi guys, I'm very new to Arma 3 (less than 200 hours) and am building a base in Eden editor. I tried making a tank fire at targets but it uses its machine gun rather than the main turret so it just goes pew pew rather than boom boom, pls help 😞 below is the code I used (found it on youtube) I tried changing the "currenetWeapon" to "primaryWeapon" and secondaryWeapon" but it still doesn't work


null = this spawn {  
 _this dotarget k2;  
 sleep 0.5;  
 _this addEventHandler ["Reloaded",{   
  _unit = _this select 0 ;   
  _mag = _this select 4 select 0 ;   
  _unit addMagazine _mag ;   
 }] ;  
 while {alive k2 and alive _this} do {  
  sleep 3;  
  [_this,currentWeapon _this] call BIS_fnc_fire;  
 } ;  
} ;

 

EDIT: where do you guys learn how to write all these codes? is there a website for this or do you guys study/ already have experience in computer science and coding stuff?

Share this post


Link to post
Share on other sites
(group _tank) reveal _target;
_tank doWatch _target;
_tank doTarget _target;
sleep 1;
_tank fireAtTarget [_target,(weapons _tank select 0)];

 

I used this in a fire support function I wrote ages ago for vehicles. Just tried it from the debug console and it still works. I assume _tank will be _this and _target will be k2. So slot that into your lopp and see how it goes.

 

Edit: You should probably run the reveal command before the loop starts too.

Share this post


Link to post
Share on other sites
1 hour ago, beno_83au said:

(group _tank) reveal _target;
_tank doWatch _target;
_tank doTarget _target;
sleep 1;
_tank fireAtTarget [_target,(weapons _tank select 0)];

 

I used this in a fire support function I wrote ages ago for vehicles. Just tried it from the debug console and it still works. I assume _tank will be _this and _target will be k2. So slot that into your lopp and see how it goes.

 

Edit: You should probably run the reveal command before the loop starts too.

Hi ! thanks for the help, i have no experience whatsoever in coding so this may sound stupid. I changed the values as you said (there's a copy of it below) and put it in the tank's init box, but an error message popped up saying "Local variable in global space" what does that mean? Oh and the reveal command is put also in the init box right? Again, many thanks for your help 😄

 

(group _this) reveal k2; 
_this doWatch k2; 
_this doTarget k2; 
sleep 1; 
_this fireAtTarget [k2,(weapons _this select 0)];

Share this post


Link to post
Share on other sites

If it's in the unit's init, use this:

nul = [this] spawn {
	params ["_tank"];
	(group _tank) reveal k2; 
	_tank doWatch k2; 
	_tank doTarget k2; 
	sleep 1; 
	_tank fireAtTarget [k2,(weapons _tank select 0)];
};

 

That'll have it fire a shot once, so all you need to do is work your reload event handler in and loop it with your while loop.

  • Like 1

Share this post


Link to post
Share on other sites
3 hours ago, beno_83au said:

If it's in the unit's init, use this:


nul = [this] spawn {
	params ["_tank"];
	(group _tank) reveal k2; 
	_tank doWatch k2; 
	_tank doTarget k2; 
	sleep 1; 
	_tank fireAtTarget [k2,(weapons _tank select 0)];
};

 

That'll have it fire a shot once, so all you need to do is work your reload event handler in and loop it with your while loop.

Hmmmm maybe I'll try this later, thanks!

Share this post


Link to post
Share on other sites

OK so after almost a whole day of looking up stuff on the internet (I have nothing better to do, that's life), I think I found a possible solution which is just by removing the last  "_this" on the BIS_fnc_fire line and replacing "useWeapon" with the muzzle name of the vehicle. so it is basically just the same thing as what I have posted at the start just with some minor tweaks.

 

If anyone's wondering, I got the idea of changing the muzzle name when I saw this website https://community.bistudio.com/wiki/BIS_fnc_fire
if someone wants the website where I found the muzzle name, its https://community.bistudio.com/wiki/Arma_3_CfgWeapons_Vehicle_Weapons

 

Below is the code I used ! 🙂


null = this spawn {   
 _this dotarget k2;   
 sleep 0.5;   
 _this addEventHandler ["Reloaded",{    
  _unit = _this select 0 ;    
  _mag = _this select 4 select 0 ;    
  _unit addMagazine _mag ;    
 }] ;   
 while {alive k2 and alive _this} do {   
  sleep 3;   
  [_this, "cannon_120mm"] call BIS_fnc_fire;   
 } ;   
} ;

 

For anybody new like me who has no experience in coding and stuff like this, I wrote down some stuff I think that might be useful ( words in bold are part of the code )


1. This is very very very basic but if there's someone out there like me, the code is inserted in the gunner's init
2. My target has been set as "k2" but its changeable ( go to your target eg: pop up target or wrecks or anything, change the variable name to whatever you want eg: dumdum, poopy, etc )
3. For {alive k2 and alive _this}, you can change the target too, I used to for my gunner to continue shooting the target behind while my target in "dotarget" is down.
4.  _this dotarget k2;           this is for your gunner to target your target
5. while {alive k2 and alive _this} do {           I think this means when the target is up ( not shot down for in the case of pop-up targets I guess ) and your unit is alive, it'll keep on executing an action
6. sleep ( I'm talking about the 2nd one, I dunno what the first one does can someone explain? ) is the time period between shots ( eg: sleep 3  means the second shot will be fired 3 secs after the first shot ) 
7. [_this, "cannon_120mm"] call BIS_fnc_fire;           this is to let your unit fired from your desired muzzle, so in my case, I'm using a tank so my muzzle will be "cannon_120mm" you can find your muzzle types in the link I pasted above. So "_this" is your unit and "cannon_120mm" is the muzzle you want your unit to fire from (also can be changed)

8. I don't know if game logic is required for this or not as the video where I found the original on uses game logic so I didn't delete mine
9. This video helped me a lot (THANKS RAVEN !)  you can find the original version of the code I used there 

 

I think that's all I wanted/remembered to share for now. ⚠️AGAIN, I HAVE NO EXPERIENCE WHATSOEVER IN CODING SO SOME OF THE THINGS I SAID ABOVE MIGHT BE WRONG⚠️  PLS CORRECT ME IF THERE'S ANYTHING WRONG WITH MY EXPLANATIONS (they're based on my understanding, so probably some will be inaccurate)
I guess that's it, bye !

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×