Home   Help Search Login Register  

Author Topic: Refering to ammo boxes in a script .... HOW???  (Read 669 times)

0 Members and 1 Guest are viewing this topic.

Red Ant

  • Guest
Refering to ammo boxes in a script .... HOW???
« on: 06 Sep 2004, 16:21:10 »
Hi all,

I'm new to OFP scripting, but I've been playing the game ever since it got released. Anyway, I've recently written a minelaying script. Well, basically I nicked it off someone's tutorial and then added new stuff myself.
This script causes an engineer to move to any amount of marker positions on the map and place a mine at each of them.  However, we all know that ngineers usually carry only 3 mines, so I want to provide the user of the script with the option to pass in a vector of object references/id's/whatever referring to ammox boxes where the engineer can replenish his arsenal of mines and then continue putting mines. I've tried to do this by giving each ammo box a name in the mission editor and then passing these strings into the script, but this approach was rendered useless by the fact that there doesn't appear to be a way tp extrapolate an object refernce from a string containing that object's name ... unless I've missed a serious detail somewhere.  ??? I also tried passing a vector of object ID's instead and then deducting an actual object reference via the object function, but apparently ammo boxes aren't assigned any id's in the mission editor. :(

Anyhow, here's the code:

Code: [Select]
; Cause a mine equipped unit to place AT-mines at predefined positions.
; Arguments  : _oUnit      - The unit to do the mine laying.
;              _vMarkerPos - A vector of strings containing the names of marker positions on the map.
;              _vMineBoxes - A vector containing ID's of ammo boxes with fresh mines that the unit can go to
;                            to replenish its outfit of mines.
; Last Change: Monday, 09/06/2004

_oUnit       = _this select 0
_vMarkerPos  = _this select 1
_vMineBoxes  = _this select 2
_strUnitName = name _oUnit      
_nPosits     = count _vMarkerPos
_nMineBoxes  = count _vMineBoxes
_iMarker     = 0
_iAmmoBox    = 0
_nMines      = 0
_DbgString   = ""

; Wait a little to let init.sqs finish.
~4

; Figure out how many mines this unit is carrying.
_nMines = [ _oUnit, "Mine" ] call countMagazines

; Debug output.
_DbgString  = format[ "DEBUG: Got %1 mines. There are %2 positions to mine.", _nMines, _nPosits ]
TitleText[ _DbgString, "plain down" ]


; Bail out now if we ain't even got a single mine.
if ( _nMines == 0 ) then { Hint "DEBUG: Got no mines at all!", exit }

; DEBUG check.
if ( _nMines < _nPosits ) then { _string = format [ "DEBUG: %1 has only %2 mines. Can't mine %3 posits.", _strUnitName, _nMines, _nPosits ]; Hint _string }


#LayMines
_DbgString = format[ "DEBUG: #LayMines, pass nr. %1", _iMarker ]
TitleText[ _DbgString, "plain down" ]
~2

; Get the next marker posit name.
_strMarkerName = _vMarkerPos select _iMarker

; Order the unit to move there.
_oUnit doMove GetMarkerPos( _strMarkerName )

; Wait until the unit has reached the marker posit.
@UnitReady _oUnit

; Lay a mine!
_oUnit Fire[ "Put", "Mine" ]

; Increment index.
_iMarker = _iMarker + 1

; Decrement mine count.
_nMines = _nMines - 1

if ( _nMines > 0 ) then { goto "LayMines" }

; If we're out of mines but there're still places left to mine, send our unit to an ammo box, have it take
; more mines and continue mining.

#CheckNextAmmoBox
~2
TitleText [ "#CheckNextAmmoBox", "plain down" ]
; When all ammo boxes have been dealt with ...
if ( _iAmmoBox == _nMineBoxes ) then { goto "EndOfScript" }

; Get the next ammo box from the vector.
_nAmmoBoxId = ( _vMineBoxes select _iAmmoBox )

; Doesn't work. Ammo boxes don't have id's.
_oAmmoBox = object _nAmmoBoxId

; Move unit to ammo box.
_oUnit doMove getPos _oAmmoBox

; Debug output. Have we really acquired the ammo box?
_DbgString = typeOf _oAmmoBox
Hint _DbgString


~2
_DbgString = format [ "Current ammo box: %1.", _oAmmoBox ]
TitleText [ "#CheckNextAmmoBox", "plain down" ]

~2

; Wait until the unit has reached the ammo box.
@UnitReady _oUnit

; See how many mines there are in this ammo box.
_nMinesInBox = [ _oAmmoBox, "Mine" ] call countMagazines

; If none, go to next box.
if ( _nMinesInBox == 0 ) then { _iAmmoBox = _iAmmoBox + 1; goto "CheckNextAmmoBox" }

; Take mines. TODO: Make sure we only take as much as we have free space.
_oUnit action[ "STR_ACTION_TAKEWEAPON", "Mine" ]
_oUnit action[ "STR_ACTION_TAKEWEAPON", "Mine" ]
_oUnit action[ "STR_ACTION_TAKEWEAPON", "Mine" ]
_nMines = 3

; Alright, we're ready to mine more places.
goto "LayMines"


#EndOfScript
exit

Anybody know a workaround?

EDIT: Script edited to incorporate mcguba's suggestions.
« Last Edit: 06 Sep 2004, 16:57:02 by Red Ant »

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Refering to ammo boxes in a script .... HOW???
« Reply #1 on: 06 Sep 2004, 16:44:56 »
What you're looking for should be doable.   There is probably a minor error of syntax somewhere.

I'm not much of a scripter, but on quick glance I'd suggest a doMove command rather than a move command for the engineer:  doMove is for units, move is for groups.

Also try some brackets:-

_nAmmoBoxId = (_vMineBoxes select _iAmmoBox)
Plenty of reviewed ArmA missions for you to play

Red Ant

  • Guest
Re:Refering to ammo boxes in a script .... HOW???
« Reply #2 on: 06 Sep 2004, 16:54:38 »
Oh, the script doesn't throw any errors. The soldier places his mines and all just fine. So the syntax isn't the problem here. The problem is that I can't figure out a way to derive an object reference to an ammo box from either it's name or its id in the editor. :(

I will change my move to doMove, though, if you think that's better.


P.S. Ahem ... I guess I haven't really expressed myself very clearly. What I mean is that this line ...
Code: [Select]
_oAmmoBox = object _nAmmoBoxId
would probably work beautifully __IF__ ..... if I actually __had__ an id to pass to the script. Yanno what I'm saying? ;) Thing is, I don't. Because when I click on the ShowId's button (or whatever it's labelled in the mission editor) it shows id's for all kinds of things .... roads, houses, even them friggin trees! But not for my ammo boxes. They don't seem to HAVE an id actually.  ??? So that's why my entire "pass an array of object id's" concept is flawed, and I'm asking if anybody has a clue if there are other ways to address a specific ammox box in a script.


« Last Edit: 06 Sep 2004, 16:55:41 by Red Ant »

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Refering to ammo boxes in a script .... HOW???
« Reply #3 on: 06 Sep 2004, 17:03:06 »
Object IDs only exist for items that belong to the map itself.   Ammo crates are objects which you insert in the editor.    You  need to name each ammo box.   Let's say you have two, then somewhere you would have

_ammoBoxArray = [ammo1, ammo2]

Then somewhere else

_ammoBox = (_ammoBoxArray select _i)


Syntax not guaranteed but you get the picture.  

You don't always get error messages with syntax errors.  Sometimes the game just quietly doesn't work and never even tells you that it doesn't understand what to do.

You could pass an array of IDs that exist.  For example if you figured out what the IDs were of all the fuel stations that exist on the map you could get somebody to go to the nearest fuel station to refuel.


Edit:  you don't even need to name the ammo boxes individually.   If you place and name the first, then copy and paste, the game will give the rest sequential names automatically.  
« Last Edit: 06 Sep 2004, 17:04:42 by macguba »
Plenty of reviewed ArmA missions for you to play

Red Ant

  • Guest
Re:Refering to ammo boxes in a script .... HOW???
« Reply #4 on: 06 Sep 2004, 17:09:51 »
I see. :) Thanks, I'm definitely gonna try that! On a sidenote, what's the difference between:

Code: [Select]
_ammoBox = (_ammoBoxArray select _i)
and the version without the parentheses ...
Code: [Select]
_ammoBox = _ammoBoxArray select _i
??
Because from what I've seen people normally address individual elements in an array without using parentheses in the statement.

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Refering to ammo boxes in a script .... HOW???
« Reply #5 on: 06 Sep 2004, 17:14:49 »
There may be no difference.    There probably isn't - it's usually obvious when you need brackets.    However, there are occasional exceptions and when a bit of code doesn't work it's one of my standard debugging techniques.  
Plenty of reviewed ArmA missions for you to play

Red Ant

  • Guest
Re:Refering to ammo boxes in a script .... HOW???
« Reply #6 on: 08 Sep 2004, 17:27:43 »
Okay, I got it working so far. :) HOWEVER ..... I've run into another problem :p. That being that the magazines command doesn't seem to work on ammo boxes, so I have no way of counting the number of mines actually in the ammo box. Ah, I wish things would ever be easy ...

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Refering to ammo boxes in a script .... HOW???
« Reply #7 on: 08 Sep 2004, 17:42:06 »
No you can't.    :(   I spent a lot of time on that one and never came up with anything worthwhile.
Plenty of reviewed ArmA missions for you to play

Red Ant

  • Guest
Re:Refering to ammo boxes in a script .... HOW???
« Reply #8 on: 08 Sep 2004, 18:10:09 »
Oh well, not a biggie. Guess I'll just have to execute the STR_ACTION_TAKEWEAPON command regardless and then check if it worked (i.e. if there were any mines left in the box) by counting all mines the unit has after trying to pick some up from the ammo box.
Thanks for your help! :)

Red Ant

  • Guest
Re:Refering to ammo boxes in a script .... HOW???
« Reply #9 on: 14 Sep 2004, 18:04:42 »
Right, this stupid ammo box is driving me nuts! The STR_ACTION_TAKEWEAPON  command just doesn't seem to work. My soldier walks to the mine box and I'm pretty sure the script runs the STR_ACTION_TAKEWEAPON  command. I am also positively sure that there are actually mines in that box, but he never seems to be able to pick them up? Because counting the soldier's mines after he's picked some up from the box reveals that .... he really hasn't picked up diddly squat!  ??? Arghh!!!

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Refering to ammo boxes in a script .... HOW???
« Reply #10 on: 14 Sep 2004, 18:50:30 »
I have a feeling that that command just makes him do the action - you have to add the mags manually with an addMagazine command.




You can tell an OFP mission designer in the street by the patchy baldness.
Plenty of reviewed ArmA missions for you to play

Red Ant

  • Guest
Re:Refering to ammo boxes in a script .... HOW???
« Reply #11 on: 14 Sep 2004, 19:14:18 »
Well, that would suck if it's true.  :(

Quote
You can tell an OFP mission designer in the street by the patchy baldness.

lol :D