Home   Help Search Login Register  

Author Topic: Building a tent  (Read 1507 times)

0 Members and 1 Guest are viewing this topic.

Offline gjle

  • Members
  • *
Building a tent
« on: 12 Oct 2007, 01:06:34 »
Hi there fellow scripters,

I don't know squat about scripting. But I wanted to give it a try. This is what I wanted to do.

I wanted the player to get the action "build tent"; which would allow the player to place a tent near him. As soon as the tent was placed, I wanted to remove the action. This would prevent 8923402 tents being built all over the map. When the build tent action was deleted I wanted to add a new action to the newly built tent: "remove tent".

You guessed it.. after the tent is removed.. the action "build tent" should reapear in the player's action list.

this is how I tried to create it:

in the players initfield:

Quote
this addaction ["build tent", "tent.sqs"];

tent.sqs

Quote
tentplayer = "ACamp" createVehicle getpos player;
player removeaction 0;
tentplayer addaction ["remove tent", "tentremove.sqs"];

tentremove.sqs

Quote
deletevehicle tentplayer;
player addaction ["build tent", "tent.sqs"];

At first this all seems to work fine, but then 2 actions "build tent" appear, and after a while even more actions "build tent" fill the actions list.

What the *beep* am I doing wrong? I hope somebody knows, cause this is driving me nuts.




Offline BuhBye

  • Members
  • *
  • I'm a llama!
Re: Building a tent
« Reply #1 on: 12 Oct 2007, 01:24:13 »
I use a variable to control that. If the variable = 1 the addaction script wont run, it will exit.

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Building a tent
« Reply #2 on: 12 Oct 2007, 12:26:43 »
What you are not realising is that each action that is added to the player has an incrementing number, even if the previous action has been removed. You are only removing action #0, so this will only remove the first action ever placed; subsequent actions will not be removed. The way to deal with this, in your case, is to know that the 3rd parameter sent to the file run by addaction is the action number. You can also simplify the code by knowing that the 1st parameter to the file is the object that the action is on. See the addAction command for more details on the parameters passed to the action file.

(by the way, there is a code tag that you should use instead of quote, for code blocks in your posts. You can even use code=tent.sqs in the square brackets to show what file you are meaning).
Code: (tent.sqs) [Select]
_actionIndex = _this select 2;

player removeAction _actionIndex;
_tent = "ACamp" createVehicle getPos player;
_tent addaction ["Remove tent", "tentremove.sqs"];

Code: (removeTent.sqs) [Select]
_tent = _this select 0;

deleteVehicle _tent;
player addaction ["Build tent", "tent.sqs"];
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline gjle

  • Members
  • *
Re: Building a tent
« Reply #3 on: 14 Oct 2007, 02:34:14 »
thanks! it works :) oh and I didn't know about the "script" option on this forum.. I'll use it from now on.

I still don't really know what "_this select 0" and "_this select 2" really does. If someone could point it out to me.. i'd greatly appreciate it. Oh and why does every variable have to start with an underscore? why is that..  ???

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: Building a tent
« Reply #4 on: 14 Oct 2007, 02:46:44 »
When calling a script it often happens that you pass values to it if itr needs them, for example.

[variable1,variable3,variable3] exec "mymegascript.sqs

in the script you can refer to these passed variables as 'select 0, select 1 or select 2'

select 0 being the first item in the array of variables that was passed, select 1 being the second...and so on...


Variables that start with an underscore are local variables, meaning they are local to the script that is running and cease to exist when the script exits, these variables cannot be accessed from outside the script because they are local to the scope of the script.

Global variables do not start with an underscore and can be accessed by any script.


Planck
I know a little about a lot, and a lot about a little.

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re: Building a tent
« Reply #5 on: 15 Oct 2007, 13:43:33 »
People should also be aware that if you pass only one argument to a script, then

instead of:

[player] exec "script.sqs"

; ...and inside the script
(_this select 0) setPos [0,0,0]


do:

player exec "script.sqs"

; ...and inside the script
_this setPos [0,0,0]


as obviously, we should aim to write our scripts so that only minimum amount of scripting commands are executed. Creating an array for only one item, and then using select to pick that item from the array certainly doesn't follow that ideology.

Also, a way to shorten a little bit the removeTent.sqs:

Code: [Select]
deleteVehicle (_this select 0);
player addaction ["Build tent", "tent.sqs"];

as you can see there is no need to create another variable to hold the tent, as it already is in the _this array created by addAction.

All this talk is for reference. It is perfectly okay for you to just not get tangled up in what I said, and return to it later when you have gained more experience.