Home   Help Search Login Register  

Author Topic: How to make an object always be in front of the player? / Carry objects  (Read 2225 times)

0 Members and 1 Guest are viewing this topic.

Offline Undeceived

  • Members
  • *
    • My missions and campaigns
Hey

I wanted to make a situation where the player has to carry some crates from one place to another.

I managed to "lift up" a crate and it also does stay up there by:

Code: [Select]
while {certain condition} do
{  
crate setpos [(GetPos player Select 0) +0.7,(GetPos player Select 1) + 0.9,(GetPos player Select 2) +0.55];

sleep 0.001;
};
But since this Select 0,1 and 2 stuff is adjusted to the cardinal directions (north, south, west and east) the crate always stays in one of these directions - in this case to the north-east. :D So if the player turns to the south, the crate still is floating in his back.


Is there possibly a way to let the crate always be in front of the player, even when he turns around?
That would be awesome!
Thanks for your help!
« Last Edit: 23 Sep 2009, 13:59:30 by Undeceived »
Current project: Black Lands (Arma 3)

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: How to make an object always be in front of the player?
« Reply #1 on: 23 Sep 2009, 03:03:14 »
Using attachTo instead of a "setPos loop" should solve this problem.
try { return true; } finally { return false; }

Offline i0n0s

  • Moderator
  • *****
Re: How to make an object always be in front of the player?
« Reply #2 on: 23 Sep 2009, 04:14:14 »
And an obsolete answer:
modelToWorld will give the position in front of the player.

Offline Undeceived

  • Members
  • *
    • My missions and campaigns
Re: How to make an object always be in front of the player?
« Reply #3 on: 23 Sep 2009, 09:40:53 »
:D You know what?
After writing this question yesterday night I went to sleep frustrated and suddenly it came to my mind: "Wasn't there this attachto command introduced in A2?" :)
But thank you very much for your answers, guys! Gonna check it out.


Edit:
It worked very well! Cool command! (attachto)


But since crates are pretty heavy... Is there a way to prevent the player from running with them? :-[
« Last Edit: 23 Sep 2009, 10:13:33 by Undeceived »
Current project: Black Lands (Arma 3)

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: How to make an object always be in front of the player?
« Reply #4 on: 23 Sep 2009, 10:35:58 »
I think if you "force" the anim for the player "object" it will not move.

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: How to make an object always be in front of the player?
« Reply #5 on: 23 Sep 2009, 12:23:51 »
You could try the speed command -> if the speed is faster than X, deattach the object. That'll force the player to walk.

And yes, attachto is groooovyyyyyy :D

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline Undeceived

  • Members
  • *
    • My missions and campaigns
Re: How to make an object always be in front of the player?
« Reply #6 on: 23 Sep 2009, 13:58:51 »
Hey, that's a great idea, Wolf! :) I'll try that.



Ok, now I think I only have three more questions about this crate-carrying-issue.
I hope that it's ok to ask it here, since it partly doesn't have much to do with attachto, etc.



1. Let's say I have 7 crates for the player to carry. The crates are named crate1, crate2, etc.
The player can lift up a crate by the following addAction in a trigger around the crate:

Code: [Select]
carrycrate = player addAction ["Lift crate","scripts\liftcrate.sqf"];In liftcrate.sqf then there comes the attachto command:
Code: [Select]
crate1 attachTo [player, [0,0.9,1]];
My problem now is that I don't know how to make this addAction to work with all of the crates, since I can't (-> don't know how to) put an array (?) with all 7 crates into this addAction command.

A possible but very dirty workaround would be to create a script for every single crate (liftcrate1.sqf, liftcrate2.sqf, etc.), but I finally want to learn how to script better.


2. A very noobish sqf problem: :confused:
When I attachto the crate to the player, it directly jumps into its position, which is pretty ugly.
How would a sqf script look like where in every loop some centimeters of height are added until the final height is reached? I tried it with some standard while-do scripts but didn't understand how to work with the height (how to add height)...


3. When I detach the crate, it does stay in the air. How can I make it fall to the ground?
I guess that this could be done the same way than in question 2, am I right? Or any other idea / script?


Thank you very much for your precious help!!
« Last Edit: 24 Sep 2009, 02:13:13 by Undeceived »
Current project: Black Lands (Arma 3)

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
You can, as seen in the addAction entry pass arguments into the addaction command. Personally I still think the addAction command is ancient and should be changed to work like the addeventhandler commands, but whatever:

Code: [Select]
carrycrate = player addAction ["Lift crate","scripts\liftcrate.sqf", crate1];
crate1 is then changed to crate2 etc for the others. As to the slowly lifting, well...untested, but why not:
Code: [Select]
private ["_crate", "_user", "_h"];
_crate = _this select 3;
_user = _this select 1;

_h = 0;
for "_i" from 0 to 9 do
{
_h = _h + 0.1;
_crate attachTo [_user, [0,0.9,_h]];
sleep 0.1;
};

Basically what that should do is lift the object 0.1 meters (still using attachto) every 0.1 seconds. The for ... do is basically the same as a while ... do, except a lot more convenient in most cases: in this case, it will simply iterate through the loop 10 times, which should lift the crate to the 1 meter height you've specified. Note that this is untested: I'm not sure if you're supposed to detach and reattach the object every time, or if you can change its position simply by adding a new attachto command. If not, simply add a "detach _crate" before every attachTo.

Should work?

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline Undeceived

  • Members
  • *
    • My missions and campaigns
Yes, it works. :good: Thanks, mate! No need to deattach every time, you can just put a new attachto command and the object moves there.


One more problem came up for me with that argument in the addaction command... I need not only the crate1 in there but also triggercrate1. And for this I had no success...
Mandoble says in the comments of the addAction page that for more than one values I would need to pass them as an array. And this I wasn't able to do.
I tried

Code: [Select]
carrycrate = player addAction ["Lift crate","scripts\liftcrate.sqf", crate1,triggercrate1];or
Code: [Select]
carrycrate = player addAction ["Lift crate","scripts\liftcrate.sqf", [crate1,triggercrate1]];and some other alternatives but the action never came up. Can you help?

And please also help me how to take on these both in the liftcrate scripft. I know that crate1 is "_this select 3" but don't know what triggercrate1 would be. I guess "_this select 4" wouldn't be right, would it?

Sorry for the language barriers in this post, don't know how to express certain things (out of noobishness too :D).

Thanks again! I think that I have it complete after this one. :)
« Last Edit: 24 Sep 2009, 18:50:04 by Undeceived »
Current project: Black Lands (Arma 3)

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Mandoble is correct. Passing an array into the script is exactly the same as using arrays for other scripts. Consider this common way of launching a script:

Code: [Select]
[crate1, cratetrigger1, 5] execvm "cratescript.sqf"
How do you start your cratescript.sqf in this case? Yep, with selects, _Crate = _this select 0 etc. This is how you access things inside an array, using select. When it comes to the addaction command, it gets a little more complicated since it already gives the script an array (which contains the one the action was added to, the one who used the action, the action's ID, the custom variables passed and maybe other things). However, all you have to do is pass an array and then use select again to get the individual parts of it out:

Code: [Select]
carrycrate = player addAction ["Lift crate","scripts\liftcrate.sqf", [crate1,triggercrate1]]
Code: [Select]
private ["_array", "_crate", "_trigger", "_user", "_h"];
_user = _this select 1;
_Array = _this select 3;
// Array passed contains two parts, 0 and 1. Assigned local variable names below.
_crate = _array select 0;
_trigger = _array select 1;

_h = 0;
for "_i" from 0 to 9 do
{
_h = _h + 0.1;
_crate attachTo [_user, [0,0.9,_h]];
sleep 0.1;
};

See? Although I just realized you don't necessarily need to do this: the script -already- knows about the crate (_this select 0 == the object the addaction is attached to), so in principle you could get _crate through that and just pass the trigger :)

Anyway, good luck!

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline Undeceived

  • Members
  • *
    • My missions and campaigns
Aah, now it works perfectly! :D I can carry all crates now.

Thank you very much for your patient help, man! :clap:

The only odd things left are that the player has no carrying animation (the crates are carried with the normal walking animation - no problem in 1st view though) and the that crates have no collision-(...) when they're attached to. I can move them through all objects until I put them on the floor again. But this is not a big problem for me.

Thanks again!
« Last Edit: 25 Sep 2009, 09:16:09 by Undeceived »
Current project: Black Lands (Arma 3)