Home   Help Search Login Register  

Author Topic: Format within addAction  (Read 1953 times)

0 Members and 1 Guest are viewing this topic.

Offline Trexian

  • Members
  • *
Format within addAction
« on: 21 Jun 2008, 20:22:55 »
Hey there! :)

Here's what I'm trying to do:
- pass a number from the execVM in a trigger
- use that number to set the max limit in a counter
- create addActions with the same format, calling the same script, passing the number of the counter

So, here's basically what I have now.

Code: [Select]
_num = _this select 0;  // from the [] execVM in the script
_c=0;   // the start of the counter

_num = _num +1;

while {_c < _num} do
{
sleep .5;
_c = _c +1;
  format ["_addAct"%1, _c] = player addAction format [["Selection %1", _c], "Select01b.sqf", _c, 10, false, false, ""];
sleep .5;
};

I think part of the problem is that the first format is defining a string, basically, not a variable.

Is this even possible, what I'm trying, or what do I need to do differently? :)

TIA!

Edit:
Re-searched, and I think this is part of the solution, eh?
http://www.ofpec.com/forum/index.php?topic=31141.0

Quote
Your confusion stems from believing that you can have variable references in SQF, which you can't.
So, I start looking at the array solution, no?
« Last Edit: 23 Jun 2008, 17:31:11 by Trexian »
Sic semper tyrannosauro.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Format within addAction
« Reply #1 on: 21 Jun 2008, 21:49:45 »
Not sure what are you trying to do, anyway:
Code: [Select]
_accidx = player addAction [format ["Selection %1", _c], "Select01b.sqf", _c, 10, false, false, ""];
call compile format ["_addAct%1 = %2", _c, _accidx];

Offline Rommel92

  • Members
  • *
Re: Format within addAction
« Reply #2 on: 22 Jun 2008, 03:19:16 »
Code: [Select]
nul = [] spawn {player addaction call compile format["['%1, this is an action with your name in it','script.sqf']",name player];}
In your players init line will add the action:
"Trexian, this is an action with your name in it"

For your code you want:
Code: [Select]
_num = _this select 0;  // from the [] execVM in the script
_c = 0;   // the start of the counter

_num = _num + 1;

while {_c < _numFloors} do
{
sleep 1.0;
_c = _c + 1;
player addAction format["['Selection %1', 'Select01b.sqf', _c, 10, false, false, '']", _c];
};

EDITED: For Plancks anti-nil statement  :P
« Last Edit: 22 Jun 2008, 03:26:30 by Rommel92 »

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: Format within addAction
« Reply #3 on: 22 Jun 2008, 03:23:42 »
Incidentally, 'nil' is the name of a scripting command, so it would be better not to call a variable 'nil'.   :P


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

Offline Trexian

  • Members
  • *
Re: Format within addAction
« Reply #4 on: 22 Jun 2008, 04:29:11 »
Oof, Mando, that was perfect! :)  :clap:

Thanks for the other idea, too, after reading up on it, I see how that would work, too.

By way of background, this is basically just a situation where a guy walks onto a trigger, where he can choose between a set number of destinations, determined by the mission builder (as passed from the execVM.

The next trick is removing the addaction index entries for the different selections when the player moves off the trigger.  I have a hunch that is a scope issue, so I'm going to search and study that aspect some more.  :thumbs:

Edit: been around here long enough not to post a reply to myself so soon. ;)

Realized a couple things.

First, in the compile, I need to drop the underscore, so it creates a global variable, right?  Because I'll be using the removeaction in the Select01b.sqf, but if it is local to the originating script, it won't work. :)  Which means my _num (the number passed from the trigger activation) should be num (global) because I'll need that in the Select script, too.  Or, in a related note, can I pass 2 variables [_c and _num] in that 3rd addaction part of the array?

Second, the compile for the removeaction would be something like:
Code: [Select]
// num is the global set in the earlier script

_c = 1;   // the start of the counter

while {_c < num} do
{
sleep 1.0;
call compile format [player removeAction "_addAct%1", _c];
_c = _c + 1;

};

Or... honest advice from people who know :) - better to do this with an array?

TIA.
« Last Edit: 23 Jun 2008, 17:30:48 by Trexian »
Sic semper tyrannosauro.

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Format within addAction
« Reply #5 on: 23 Jun 2008, 18:37:00 »
If you can use an array, then this is always the preferred method. Can't actually think of any circumstances when you can't use an array rather than using dynamic variables like this (especially now when you can use publicVariable with arrays. Before 1.09, the only effective way to public an array of objects was using a similar method of dynamic variables with numbers in their names).

I think in your code example, you mean to do this:
Code: [Select]
call compile format ["player removeAction _addAct%1", _c];

But there are quite a lot of other problems with your code where you misunderstand how to use actions. E.g. you have to record the action indexes as they are added and remove them later. You can't just assume that the first action is 0, because it often isn't.

Anyway, with arrays:
Code: (addActions.sqf) [Select]
_num = _this select 0;

addedActions = [];

// Loop from 1 to _num. You may prefer "0 to (_num - 1)" instead.
for "_i" from 1 to _num do
{
    _index = player addAction [format["Selection %1", _i], "Select01b.sqf", _i, 10, false, false, "]"];
    addedActions = addedActions + [_index];
};

Code: (removeActions.sqf) [Select]
{ player removeAction _x } forEach addedActions;
addedActions = nil;

EDIT: Corrected syntax of removeAction command.
« Last Edit: 23 Jun 2008, 19:33:53 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Trexian

  • Members
  • *
Re: Format within addAction
« Reply #6 on: 23 Jun 2008, 19:09:07 »
Thanks Spooner (esp. for checking back for the edit). :)

WRT the addaction index, yeah, in a "regular" addaction, it returns the index, which is placed in a variable by using the:
index = player addaction blah blah blah

While looking at your array, I think I see my conceptual problem.  You are making the index the value of the array.

So, let's say the first addaction starts at index 3.

My thought was that:
the addaction command would return the value 3, to be stored in addedaction
addedaction [1] = 3
(next iteration)
addedaction [2] = 4

and so on, so the removeaction addedaction[1] would be read as removaction 3

What it appears from your suggestion is:
addaction returns 3, stored in index
addedaction[index] would be addedaction[3] for the first one, then addedaction [4] and so on

So, then the removeaction for/each loop just looks at what the values are for each added action? :D

Dynamically creating the variable name, for some reason, made more sense to me in an iterative way. :)  It seemed "cleaner" somehow.

But, after getting into this a bit more, (thanks to you guys) I think the array is really the better way to go.

Sorry to belabor the point.  It has been WAY too long since I studied programming in any conventional setting.  It really helps it "stick" when I understand things.

(One point about the removaction.  Should it be?
{player removeaction _x} forEach addedaction};

Or, for that matter, a variable instead of player?

Thanks SO much, again.
Sic semper tyrannosauro.

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Format within addAction
« Reply #7 on: 23 Jun 2008, 19:33:12 »
Oops, yes, it should be { player removeAction _x }  :-[ Sorry, I was thinking more about the arrays than the other code!

Remember that arrays in most languages, including SQS/SQF, start at index 0, not index 1.
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Trexian

  • Members
  • *
Re: Format within addAction
« Reply #8 on: 23 Jun 2008, 21:50:45 »
Right. :)  :good:

(I would thank you, but only if someone else posts after me, in case I need to post again.) ;)

(Oh.  Wait.  Ok.  So, please check back here in case I need to edit.)  :shhh:
Sic semper tyrannosauro.