Jump to content
Sign in to follow this  
CombatComm1

Help with setpos from script to global object

Recommended Posts

Hey all, I am trying to spawn items from a script. It works fine in the sense that every spawns as it should at the given markerpos. When using ONLY global variables in the editor the setpos works flawlessly and the item is spawn right where I want it on top of the table. As soon as I spawn these items from a script as local it no longer works. Here is my code...

_intel1 = "Box_East_Ammo_F" createVehicle (getmarkerpos "intel2");
_intel1 addaction ["investigate","agia1.sqf"];  
_intel2 = "ARP_Objects_computer" createVehicle (getmarkerpos "intel2");
_intel2 setpos [getpos tab2 select 0, getpos tab2 select 1 (_intel2 select 2) +.8];
_intel2 addaction ["investigate","agia2.sqf"];  
_intel3 = "ARP_Objects_Folder" createvehicle (getmarkerpos "intel3");
_intel3 setpos [getpos tab3 select 0, getpos tab3 this select 1 (_intel3 select 2) +.8];
_intel3 addaction ["investigate", "agia3.sqf"];
_intel4 = "ARP_Objects_toughbook" createvehicle (getmarkerpos "intel4");
_intel4 setpos [getpos tab4 select 0, getpos tab4 this select 1 (_intel4 select 2) +.8];
_intel4 addaction ["Investigate","agia4.sqf"];
_intel5 = "ARP_Objects_Folder" createvehicle (getmarkerpos "intel6");
_intel5 setpos [getpos tab5 select 0, getpos tab5 this select 1 (_intel5 select 2) +.8];
_intel5addaction ["Investigate","agia5.sqf"];

Any help and discussion on the setpos command so that I may better understand it would be greatly appreciated!

Share this post


Link to post
Share on other sites

Firstly, I recommend 'setPosATL', 'getPosATL', and 'createVehicle array' as they execute faster.

http://community.bistudio.com/wiki/setPosATL

http://community.bistudio.com/wiki/getPosATL

http://community.bistudio.com/wiki/createVehicle_array

Secondly, what is tab2, tab3, etc?

Thirdly, what is 'this select 1'? Are they any arguments to this function? You must use '_this' to get arguments.

Fourthly, the 'setpos' statements are either in 2d or missing a comma before the third element.

Fifthly, what error message appears in-game? What exact error does it name? What line does it name?

Sixthly, how are you calling this function?

Lastly, what is in 'agia1.sqf', 'agia2.sqf', etc.? Can you see the actions? Do they work?

Knowing all that would help, but here you go anyway:

private ["_intel1", "_intel2", "_intel3", "_intel4", "_intel5"];

_intel1 = createVehicle ["Box_East_Ammo_F", (getMarkerPos "intel2"), [], 0, "NONE"];
_intel1 addAction ["Investigate", "agia1.sqf"];

_intel2 = createVehicle ["ARP_Objects_computer", (getMarkerPos "intel2"), [], 0, "NONE"];
_intel2 setPosATL [((getPosATL tab2) select 0), ((getPosATL tab2) select 1), (((getPosATL _intel2) select 2) + 0.8)];
_intel2 addAction ["Investigate", "agia2.sqf"];

_intel3 = createVehicle ["ARP_Objects_Folder", (getMarkerPos "intel3"), [], 0, "NONE"];
_intel3 setPosATL [((getPosATL tab3) select 0), ((getPosATL tab3) select 1), (((getPosATL _intel3) select 2) + 0.8)];
_intel3 addAction ["Investigate", "agia3.sqf"];

_intel4 = createVehicle ["ARP_Objects_toughbook", (getMarkerPos "intel4"), [], 0, "NONE"];
_intel4 setPosATL [((getPosATL tab4) select 0), ((getPosATL tab4) select 1), (((getPosATL _intel4) select 2) + 0.8)];
_intel4 addAction ["Investigate", "agia4.sqf"];

_intel5 = createVehicle ["ARP_Objects_Folder", (getMarkerPos "intel6"), [], 0, "NONE"];
_intel5 setPosATL [((getPosATL tab5) select 0), ((getPosATL tab5) select 1), (((getPosATL _intel5) select 2) + 0.8)];
_intel5 addAction ["Investigate", "agia5.sqf"];

I assume you want to create each object, move it to a height of 0.8, and then add an action to it. That function should do that, but you might want to check that your marker and object names match up.

Share this post


Link to post
Share on other sites
Firstly, I recommend 'setPosATL', 'getPosATL', and 'createVehicle array' as they execute faster.

http://community.bistudio.com/wiki/setPosATL

http://community.bistudio.com/wiki/getPosATL

http://community.bistudio.com/wiki/createVehicle_array

Secondly, what is tab2, tab3, etc?

Thirdly, what is 'this select 1'? Are they any arguments to this function? You must use '_this' to get arguments.

Fourthly, the 'setpos' statements are either in 2d or missing a comma before the third element.

Fifthly, what error message appears in-game? What exact error does it name? What line does it name?

Sixthly, how are you calling this function?

Lastly, what is in 'agia1.sqf', 'agia2.sqf', etc.? Can you see the actions? Do they work?

Knowing all that would help, but here you go anyway:

private ["_intel1", "_intel2", "_intel3", "_intel4", "_intel5"];

_intel1 = createVehicle ["Box_East_Ammo_F", (getMarkerPos "intel2"), [], 0, "NONE"];
_intel1 addAction ["Investigate", "agia1.sqf"];

_intel2 = createVehicle ["ARP_Objects_computer", (getMarkerPos "intel2"), [], 0, "NONE"];
_intel2 setPosATL [((getPosATL tab2) select 0), ((getPosATL tab2) select 1), (((getPosATL _intel2) select 2) + 0.8)];
_intel2 addAction ["Investigate", "agia2.sqf"];

_intel3 = createVehicle ["ARP_Objects_Folder", (getMarkerPos "intel3"), [], 0, "NONE"];
_intel3 setPosATL [((getPosATL tab3) select 0), ((getPosATL tab3) select 1), (((getPosATL _intel3) select 2) + 0.8)];
_intel3 addAction ["Investigate", "agia3.sqf"];

_intel4 = createVehicle ["ARP_Objects_toughbook", (getMarkerPos "intel4"), [], 0, "NONE"];
_intel4 setPosATL [((getPosATL tab4) select 0), ((getPosATL tab4) select 1), (((getPosATL _intel4) select 2) + 0.8)];
_intel4 addAction ["Investigate", "agia4.sqf"];

_intel5 = createVehicle ["ARP_Objects_Folder", (getMarkerPos "intel6"), [], 0, "NONE"];
_intel5 setPosATL [((getPosATL tab5) select 0), ((getPosATL tab5) select 1), (((getPosATL _intel5) select 2) + 0.8)];
_intel5 addAction ["Investigate", "agia5.sqf"];

I assume you want to create each object, move it to a height of 0.8, and then add an action to it. That function should do that, but you might want to check that your marker and object names match up.

@Zenophon, thanks a lot for your response.

Firstly, thanks for suggesting better commands.

Secondly, Tab 1,2,3, etc are tables. (Objects in the game I put down for these spawned items to sit on top of) I guess I understand the importance of //Descriptions for others reading my scripts now. lol

Thirdly, I have NO IDEA what this select 1 is to be perfectly honest. It is just copied from the BI wiki as I am not familiar enough with the function to omit anything unfortunately.

fourthly, yes I realized I was missing a comma after posting. With a comma I get NO ERRORS, it just fails to work.

Sixthly I am calling this function through a script activated based on the position of a HVT (high value target) who is linked to random markers in the editor. The script calls and runs just fine, the code inside, i am afraid dosent though.

7th-ly, agia.sqf are various investigate actions and yes they all are added to the spawned items and work. They basically play various intelligence gathering information scripts.

And lastly, your correction of my original script still fails to place the spawned items on top of the various editor placed camping tables (.8) AGL. Of course this is no doubt to my confusing you and not explaining the script well. Thanks again for your help.

Share this post


Link to post
Share on other sites

Thank you for answering all of my questions, I did not mean to overwhelm you. You could confirm that the code is running placing something like 'hint "Spawning Intel";' just above the code to create the objects.

The 'createVehicle' statements could say 'getPosATL tab#' instead of 'getMarkerPos "intel#"'. You would then not need the intel markers (unless you are using them elsewhere in your mission). You could also replace '(((getPosATL _intel5) select 2) + 0.8)' with '(((getPosATL table1) select 2) + 0.8)'. These two changes will make 'table#' your only global variable so that it is easier to debug.

I recommend starting in a blank editor mission to test creating just a single object. Place a player unit and a table named 'table1', then in the debug console, execute this:

_intel1 = createVehicle ["land_suitcase_f", (getPosATL table1), [], 0, "NONE"];
_intel1 setPosATL [((getPosATL table1) select 0), ((getPosATL table1) select 1), (((getPosATL table1) select 2) + 0.8)];
_intel1 addAction ["Investigate", "agia1.sqf"];

There should be a table with a suitcase on top that has an action to 'Investigate' (the action may take a little while to appear for some odd reason). I used a suitcase to make sure there are no issues with the objects you are trying to spawn. The action will not do anything, but this confirms that the action would work if you had the script in your mission folder.

If you can get the above to work, then just duplicate it for all the tables and change the object classname to what you want. If you confirm that the script is actually running and test after every change, you will be able to see what causes the problem.

Share this post


Link to post
Share on other sites
Thank you for answering all of my questions, I did not mean to overwhelm you. You could confirm that the code is running placing something like 'hint "Spawning Intel";' just above the code to create the objects.

The 'createVehicle' statements could say 'getPosATL tab#' instead of 'getMarkerPos "intel#"'. You would then not need the intel markers (unless you are using them elsewhere in your mission). You could also replace '(((getPosATL _intel5) select 2) + 0.8)' with '(((getPosATL table1) select 2) + 0.8)'. These two changes will make 'table#' your only global variable so that it is easier to debug.

I recommend starting in a blank editor mission to test creating just a single object. Place a player unit and a table named 'table1', then in the debug console, execute this:

_intel1 = createVehicle ["land_suitcase_f", (getPosATL table1), [], 0, "NONE"];
_intel1 setPosATL [((getPosATL table1) select 0), ((getPosATL table1) select 1), (((getPosATL table1) select 2) + 0.8)];
_intel1 addAction ["Investigate", "agia1.sqf"];

There should be a table with a suitcase on top that has an action to 'Investigate' (the action may take a little while to appear for some odd reason). I used a suitcase to make sure there are no issues with the objects you are trying to spawn. The action will not do anything, but this confirms that the action would work if you had the script in your mission folder.

If you can get the above to work, then just duplicate it for all the tables and change the object classname to what you want. If you confirm that the script is actually running and test after every change, you will be able to see what causes the problem.

Thanks again for all the help!! It works! However when I try and use the "randomness" oportunities of the createvehicle array command as you can do with the markers to have it spawn at a random marker it does not work using the object. Here is the manipulated code.

_intel1 = createVehicle ["land_suitcase_f", (getPosATL table1), [getPosATL table11,getPosATL table12,getPosATL table13,getPosATL table14], 0, "NONE"]; 

This is probably not possible because I am setPos it to 1 and only table huh?? I am trying to make this as random as possible by having multiple tables any one piece of intel can spawn at.

Share this post


Link to post
Share on other sites

The 'setPos' command will move an object regardless of where it was originally spawned, so adding those arguments to 'createVehicle' would not change anything. If you want different intel items to appear on different tables, you can pick a table at random to use. You could try a loop like this:

_tableArray = [table1, table2, table3, table4, table5];
{
   _table = (_tableArray select (floor (random ((count _tableArray) - 0.0001))));
   _tableArray = _tableArray - [_table];

   _intel = createVehicle [_x, (getPosATL _table), [], 0, "NONE"];
   _intel setPosATL [((getPosATL _table) select 0), ((getPosATL _table) select 1), (((getPosATL _table) select 2) + 0.8)];

   _intel addAction ["Investigate", (format ["agia%1.sqf", (_forEachIndex + 1)])];
} forEach ["land_suitcase_f", "land_bucket_f", "Land_SurvivalRadio_F", "Land_BottlePlastic_V1_F", "Land_Basket_F"];

This code should keep the order of objects the same, so that the action assigned to them uses the correct script, the tables should be used in a random order. There can more tables than there are objects, if you really want to randomize it (players won't know which tables to go to each time). But you cannot have fewer tables than objects, that would break the script. This is tested and working with those objects and both 5 and 6 tables; the actions also work (I just used test scripts to make sure the numbers match).

Share this post


Link to post
Share on other sites

Bro, you are a scripting genious! I have to admit I am only slightly familiar with what you are doing here so its all magic to me. Thank you very much. It works great! With 2 exeptions, on occasions the script will spit out on error for line 1 saying it expected type string and not an array and then all the items will spawn as suitcases.

The other issue which is more problamatic for me is that when I change the classnames to look like this

 forEach ["land_suitcase_f", "Land_File1_F", "Land_FilePhotos_F", "Land_Notepad_F", "Land_Laptop_unfolded_F"];  

the only one that wants to sit nice and preatty on top of the table is the suitcase. I have NO idea why this might be. Everything else just falls right through the table to the floor. Even if I spawn it higher it falls straight through and to the floor.

Again, I am a very BASIC scripter so far and am learning thanks to fockers great scripting tutorial. Your script is great and works but is very advanced so I cant exactly understand the error or why after customizing the class names they sink through the table.

Share this post


Link to post
Share on other sites

For the objects falling through the table, that is a side effect of them being physics-enabled when they spawn. Add these lines at the end the forEach loop:

_intel enableSimulation false;
_table enableSimulation false;

That should disable the physics, and the object will probably never move. I tested this on a hill and neither the table nor the object fell down the hill. I also tested the action and it still worked.

Regarding the error, it means that some command was expecting a string, but got an array. I cannot reproduce that error no matter how many times I run the function. The only string used in the loop is the current element of the array (defined as '_x'). Ensure that all of the tables exist in the editor (you would get a different error if they did not, but it doesn't hurt to be thorough). I assume that the error appears when using different object classnames, so try spawning each one like this to confirm that is it not the objects themselves:

_intel = createVehicle ["Land_File1_F", (getPosATL player), [], 0, "NONE"];

If all of the objects spawn without errors, then try to determine if the error is completely random or if there is some pattern. Run the mission 5 or so times every time you change anything and see how many times the error appears.

It could also be some sort of variable scope issue (I have seen some strange ones). You might want to define '_x' as another variable and declare all variables private for the function. I am not sure how you are defining and calling this function, but I recommend that you place it in your init.sqf like this:

RandomIntel = {
   private ["_tableArray", "_table", "_intel", "_intelClassname"];
   _tableArray = [table1, table2, table3, table4, table5];
   {
       _intelClassname = _x;
       _table = (_tableArray select (floor (random ((count _tableArray) - 0.0001))));
       _tableArray = _tableArray - [_table];

       _intel = createVehicle [_intelClassname, (getPosATL _table), [], 0, "NONE"];
       _intel setPosATL [((getPosATL _table) select 0), ((getPosATL _table) select 1), (((getPosATL _table) select 2) + 0.8)];

       _intel addAction ["Investigate", (format ["agia%1.sqf", (_forEachIndex + 1)])];

       _intel enableSimulation false;
       _table enableSimulation false;
   } forEach ["land_suitcase_f", "land_bucket_f", "Land_SurvivalRadio_F", "Land_BottlePlastic_V1_F", "Land_Basket_F"];
};

I have included the changes that I suggested above in that code. Also, I would call it like this:

0 = [] call RandomIntel;

If none of this works, then I am at a loss for what is causing the error. If the function works in an empty editor mission with just tables, then something else in your mission could be conflicting with the function somehow.

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
Sign in to follow this  

×