Maguila.gm 0 Posted September 1, 2023 I want to make a VLS to engage 18 targets so i made this code: west reportRemoteTarget [t1, 90000]; t1 confirmSensorTarget [west, true]; vls1 fireAtTarget [t1, "weapon_vls_01"]; sleep 30; west reportRemoteTarget [t2, 90000]; t2 confirmSensorTarget [west, true]; vls1 fireAtTarget [t2, "weapon_vls_01"]; sleep 30; west reportRemoteTarget [t3, 90000]; t3 confirmSensorTarget [west, true]; vls1 fireAtTarget [t3, "weapon_vls_01"]; sleep 30; west reportRemoteTarget [t4, 90000]; t4 confirmSensorTarget [west, true]; vls1 fireAtTarget [t4, "weapon_vls_01"]; sleep 30;... It works but is time consuming an not optimized. I asked ChatGPT to optimize so he gave me this, But it is not working. for [{_i = 1}, {_i <= 10}, {_i = _i + 1}] do { private _targetVar = format ["t%1", (_i)]; west reportRemoteTarget [_targetVar, 90000]; _targetVar confirmSensorTarget [west, true]; vls1 fireAtTarget [_targetVar, "weapon_vls_01"]; sleep 25; }; Does anyone know what is wrong Share this post Link to post Share on other sites
Ramsen V 1 Posted September 1, 2023 I would do this maybe? _targetArray = [t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18]; { west reportRemoteTarget [_x,90000]; _x confirmSensorTarget [west,true]; vls1 fireAtTarget [_x,"weapon_vls_01"]; sleep 30; } forEach _targetArray; 2 hours ago, Maguila.gm said: I asked ChatGPT to optimize so he gave me this You mean... 'it' gave me this. 1 Share this post Link to post Share on other sites
Harzach 2517 Posted September 1, 2023 3 hours ago, Maguila.gm said: Does anyone know what is wrong _targetVar is a string, reportRemoteTarget takes an object. @Ramsen V's solution is probably the simplest method in terms of ease of understanding. Your original pseudocode can be fixed using missionNameSpace and getVariable. We'll replace the second line with two lines of code: for [{_i = 1}, {_i <= 10}, {_i = _i + 1}] do { private _targetVar = "t" + str _i; // Parse the name of the variable as a string _targetVar = missionNamespace getVariable [_targetVar , objNull]; // Get the object variable west reportRemoteTarget [_targetVar, 90000]; _targetVar confirmSensorTarget [west, true]; vls1 fireAtTarget [_targetVar, "weapon_vls_01"]; sleep 25; }; Not tested, but should work. 1 Share this post Link to post Share on other sites
Ramsen V 1 Posted September 3, 2023 On 9/2/2023 at 12:07 AM, Harzach said: @Ramsen V's solution is probably the simplest method in terms of ease of understanding. In the context of the code maybe so, but your code is the more intelligent way and the way I would choose! I was trying to think of a way of creating t1 > t18 by converting them to strings, edit with text format then convert them back again and the use a forEach loop, but this would require much more code and you reduced all of it to just two lines! Genius!! lol The only thing I could not understand from chatGPT thingy is the top line? Why this? for [{_i = 1}, {_i <= 10}, {_i = _i + 1}] do {code}; Why could one not do: for "_i" from 1 to 18 do {code}; or _val = 18; for "_i" from 1 to _val do {code}; Share this post Link to post Share on other sites
Harzach 2517 Posted September 3, 2023 3 hours ago, Ramsen V said: Why this? It's a more "function rich" syntax of for: Quote for [init, condition, codeToExecute] You are correct to question it, as you are simply iterating through a known value. The AI used it because whatever script it scraped it from used it. AI does not understand context. 1 Share this post Link to post Share on other sites