HalfdeadKiller 0 Posted September 10, 2019 Alright, I'm going to preface this with I have barely any idea of what I'm doing. My coding experience is that I know enough to stumble around and copy and paste things to make them work, but not enough to write anything from scratch. My current project is telling an AI grenadier, to fire a smoke shell from his underbarrel grenade launcher, at the target he has been assigned by the group leader. Ideally I'd like this to be modular, so I can just shove it into the init of any grenadier I want to do that with. Bonus points if it's possible to have the AI only fire the smoke shells within a certain range. I've done digging for the past two days and came up with this. At first I had it in it's own .sqf file, but the game kept throwing errors so I did away with that and went straight for the Grenadier's Init. Spoiler _target = assignedTarget this; while {true} do { if (!isNull _target) then { this forceWeaponFire ["rhs_weap_ak74m_gp25","hlc_GRD_Red"]; } else { }; sleep 15 }; This code doesn't work at all. Currently it spams the console with "Suspending not allowed in this context". Which would be related to the sleep command. When I had it in it's own SQF it threw errors like "Error assignedtarget: Type Array, expected Object" or "Error Undefined variable in expression: this". I'm completely lost so any help would be appreciated. Share this post Link to post Share on other sites
stanhope 411 Posted September 10, 2019 0 = [_this] spawn { params ["_unit"]; private _target = assignedTarget _unit; private _weapon = "rhs_weap_ak74m_gp25"; private _range = 200; while {alive _unit} do { if (!isNull _target && _unit distance _target < _range) then { if (currentWeapon _unit != _weapon) then { _unit selectWeapon "TODO: find the muzzle name of rhs_weap_ak74m_gp25 GL launcher"; sleep 2; }; _unit forceWeaponFire [_weapon,"hlc_GRD_Red"]; }; sleep 15 }; }; Untested, let me know if it works Share this post Link to post Share on other sites
HalfdeadKiller 0 Posted September 10, 2019 (edited) Looks like we're getting the following errors. The main one being spammed is the Suspending not Allowed section. The last block only happens right before hitting ESC according to the timestamps in the .rpt. I'm pretty sure the muzzle name is rhs_weap_ak74m_gp25, since currentMuzzle player; gives me "rhs_weap_ak74m_gp25" when the gp-25 is selected. Edit: I had the wrong weapon selected. currentMuzzle gives me "GP25Muzzle". Edit2: Maximum Zeroing range for the GP25 seems to be 400 meters currently, so we can change the 200 to 400. Just gotta figure out what why it doesn't like sleep. Last I checked using spawn was a way for sleep to function properly. Not sure why it's tossing errors at me now. Spoiler 13:18:04 Error Generic error in expression 13:18:04 Suspending not allowed in this context 13:18:04 Error in expression <hlc_GRD_Red"]; } else { }; sleep 15 }; }> 13:18:04 Error position: <sleep 15 }; }> 13:18:05 Error in expression <ivate _range = 200; while {alive _unit} do { if (!isNull _target > 13:18:05 Error position: <_unit} do { if (!isNull _target > 13:18:05 Error Undefined variable in expression: _unit 13:18:05 Error in expression <]; private _target = assignedTarget _unit; private _weapon = "rhs_weap_ak> 13:18:05 Error position: <_unit; private _weapon = "rhs_weap_ak> 13:18:05 Error Undefined variable in expression: _unit Edited September 10, 2019 by HalfdeadKiller Added proper muzzle name. Edit's will be labeled as Edit#: Share this post Link to post Share on other sites
HalfdeadKiller 0 Posted September 10, 2019 (edited) I've modified the code and ended up with this. However it still gives me an undefined variable for _grenadier. changing _this to this doesn't seem to yield any results either. Not sure how to continue. The sleep errors have stopped after changing 0 = [_this] spawn { to [] spawn { . However I don't know the intricacies of [] spawn vs the other. Edit1: Try as I might I keep getting undefined variable for _grenadier. It's as if _this isn't passing it's information through to the script that is being spawned. I'm not sure how to proceed. Spoiler [] spawn { private _grenadier = _this; private _target = assignedTarget _grenadier; private _weapon = "rhs_weap_ak74m_gp25"; private _range = 400; while {alive _grenadier} do { if (!isNull _target && _grenadier distance _target < _range) then { if (currentWeapon _grenadier != _weapon) then { _grenadier selectWeapon "GP25Muzzle"; sleep 2; }; _grenadier forceWeaponFire [_weapon,"hlc_GRD_Red"]; }; sleep 15 }; }; Edit2: This was my latest iteration of testing. Also I will post the outputs of the debug console when I used different commands to output things. Debug Console Outputs Spoiler currentMuzzle player; "GP25Muzzle" weaponState player; ["rhs_weap_ak74m_gp25","GP25Muzzle","Single","hlc_GRD_Red",1] magazines player; [["rhs_30Rnd_545x39_7N10_AK",30],["rhs_30Rnd_545x39_7N10_AK",30],["rhs_30Rnd_545x39_7N10_AK",30],["rhs_VOG25",1],["rhs_VOG25",1],["rhs_VOG25",1],["rhs_VOG25",1],["rhs_VOG25",1],["rhs_VOG25",1],["rhs_VOG25",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_Red",1],["hlc_GRD_green",1],["hlc_GRD_green",1],["hlc_GRD_blue",1],["hlc_GRD_blue",1]] Non-functioning script, throws undefined variable for _unit Spoiler _GrenadierSmoke = { _unit = _this select 0; private _target = assignedTarget _unit; private _weapon = "rhs_weap_ak74m_gp25"; private _range = 400; while {alive _unit} do { if (!isNull _target && _unit distance _target < _range) then { if (currentWeapon _unit != _weapon) then { _unit selectWeapon "GP25Muzzle"; sleep 2; }; _unit forceWeaponFire [_weapon,"hlc_GRD_Red"]; }; sleep 15 }; }; [_this] spawn _GrenadierSmoke; Edited September 10, 2019 by HalfdeadKiller Share this post Link to post Share on other sites
HalfdeadKiller 0 Posted September 10, 2019 I'm beginning to think this problem is a lot more complex than initially thought of. When directly using the forceWeaponFire, the command is looking for a firerate, not a magazine. The firerate for the GP25 is Single. So far I don't see a way to specific the type of grenade, aside from some switch ammo action that requires specific ID numbers of magazines. So to tell the AI to fire the grenade launcher, I had to do " this forceWeaponFire ["GP25Muzzle","single"]; " I have yet to figure out how to tell the AI to change the magazine type of the gp25 muzzle. Share this post Link to post Share on other sites