Define several textFields, as one variable in ActionScript 3 - actionscript-3

I am wondering if it is possible to define several textfield/boxes as one variable?
For example: I have five textFields, all with different names - "show1" & "show2" etc.
Can I for instance define them all as one variable?
var key;
key = show1 && show2 ... etc.
key.visible = false;
Please help me.
Thank you so much for answer.

Sometimes in as3 or javascript or other programming language I store several variables inside just one variable using separators.
For example if you have your 5 textfields and want to put them inside only 1 variable. I would do this;
var key;
key = show1 + '|||' + show2 + '|||' + show3;
so there you will have all texts inside a variable separated by |||
to use that separated then you will have to split that var, for example:
array = key.split('|||');
I usually do that when I need to communicate an array like variable between platforms, AS3 to PHP through POST or GET, or ajax with PHP etc.

Related

in Kdb, how do I assign a list of symbols each to a list of values

For example, I want to pass a dictionary with 10 pairs into a function to bypass the 8 valence limit. I then want the keys of the dictionary each to be assigned as a local variable to be assigned to their value and use that as the parameters for my function. Alternatively, is there a better way to pull this off?
I am afraid there is no way to functionally assign value to local scope variable. E.g.
{eval parse "a: 10";}1b
creates global variable a.
You may fix some scope name, e.g. .l, keep variables there and clear scope before function return, e.g.:
{
eval each (:),'flip (`$".l.",/:string key x;value x);
r: .l.a + .l.b + .l.c;
delete from `.l;
r
}`a`b`c!1 2 3
But getting values directly from input dictionary (like x[`a]) seems easier and clearer approach.
apply helps to simplify invoking other functions, using dictionary values as parameters. This may be what you are looking for. E.g.
{
f: {x+y+z};
f . x`a`b`c
}`a`b`c!1 2 3

How to emulate an hash table in SQF?

FYI: SQF is a programming language for the computer game series Arma.
The main data types of SQF are documented and the list does not include an hash table (or dictionary).
One way to have a hash table is to create a Game Logic in the mission.sqm (e.g. named logic1), and use setVariable and getVariable on it, e.g.
logic1 setVariable ["variable1", 1];
_a = logic1 getVariable "variable1";
However, this requires an extra array associated with it to track the list of used keys, e.g.
logic1Vars = [];
logic1 setVariable ["variable1", 1];
logic1Vars pushBack "variable1";
logic1 setVariable ["variable1", nil];
logic1Vars = logic1Vars - ["variable1"];
(or is there a way to get the list of variables?)
Another way (that is possible but I haven't tried) is to implement an hash table. That obviously takes an extra effort because implementing a good table is not easy.
But maybe I am missing something: is there an idiomatic way to have an hash table in SQF?
You can use allVariables to retrieve the Number of keys in a Namespace.
To create a Namespace you can use a Logic or a Location or a SimpleObject. See how CBA does it https://github.com/CBATeam/CBA_A3/blob/master/addons/common/fnc_createNamespace.sqf.
Generally a Location or SimpleObject is more Performance friendly than using a GameLogic. You should keep that in Mind.
But what you are probably searching for is the allVariables command which returns all Variables in a Namespace(Hashtable).
You can also use getVariable ARRAY to set a default Value in case the Namespace doesn't contain the Key you want to read.
CBA also has Hashes they behave like a Map. Not like a hashTable (The keys are not hashed) also it's SQF code instead of Engine code so it is slightly slower.
Also (don't have enough reputation to comment)
You don't need all of this:
_vars = _logic getVariable "_VARIABLES";
_vars pushBack "variable1";
_logic setVariable ["_VARIABLES", _vars];
_vars will be a reference to the Array and pushBack will add an element to that Array you are referring to. so pushBack is already modifying _VARIABLES. No need to set it again.
Though this is an old question I'd like to list a new answer that you can now natively create a HashMap in SQF since Arma 3 version 2.01 using the createHashMap command.
One way to create an hash table without having to create it in the mission.sqm is to create it scripting. Specifically, it is possible to write
allHashes = createGroup west; // somewhere once; `west` or `east` does not matter here.
_logic = allHashes createUnit ["LOGIC", [0,0,0], [], 0, "NONE"];
_logicVars = [];
this still requires the list of variables, and thus does not encapsulate the whole hash table in a single object. One way to achieve the hash table logic in a single object is to use
_logic = allHashes createUnit ["LOGIC", [0,0,0], [], 0, "NONE"];
_logic setVariable ["_VARIABLES", []];
and use
_logic setVariable ["variable1", 1];
_vars = _logic getVariable "_VARIABLES";
_vars pushBack "variable1";
_logic setVariable ["_VARIABLES", _vars];
This can be encapsulated in a function, but still needs 4 lines of code to get the whole thing...

How to Find Certain Areas of a Variable AS3

I do not have a project or anything, I just wanted to know if this was possible.
Let's say I have a variable that is a string,
var code:String="hello there"
is there any way possible I could keep that variable the same, while using it for only the first 10 letters (or any number of letters)?
For example, if I had 2 dynamic textboxes, could I assign one the first four letters of the variable, and the other one the last four letters of the variable?
Also, could I recognize a charCode and make that the endpoint? For example, could I recognize when a space occurs, and do all letters before that?
Thanks in advance.
var parts:Array = code.split(" ");
Will do just like it says: split the string at all occurrences of the delimiter, which in this case is space.

AS3: how to pass by "object"

I was actually looking for a way to pass by reference in AS3 but then it seemed that adobe and lots of people's understanding of pass by reference is different from what I have been taught at the university. I was taught java was pass by value and C++ allowed pass by reference.
I'm not trying to argue what pass by value and reference are. I just want to explain why I'm using pass by object in the question...
Back to the question, I would like to do something like:
public function swapCard(cardA:Cards, cardB:Cards) {
var temp:Cards = cardA;
cardA = cardB;
cardB = temp;
}
...
swapCard(c1, c2);
EDIT: adding two examples on how I'm using the swapCard function
1) in the process of swaping a card between player1 and player2's hand
swapCard(player1.hand[s], player2.hand[t]);
2) in the process of swaping a card between player1's hand and deck
swapCard(player1.hand[s], player1.deck[rand]);
In C++, we only need to add a symbol before the parameters to make it work (and we call THIS pass by reference). But in AS3, cardA and cardB are just pointers to the formal parameters. Here in the function, changing the pointers does not do anything to the formal parameters :(
I have been searching for hours but I couldn't find a way to without knowing all the properties of the Cards.
If I have to change the properties of the cards one by one then maybe I should change swapCard to a static function in class Cards? (because I don't want to expose everything to another class) I'm not sure if this is a good practice either. This is like adding a swap_cars function into class Cars. If I let this happen, what will be next? Wash car, lend car, rent car... I really want to keep the Cards class clean and holds only the details of the card. Is there a possible way to do this properly in AS3?
The kind of swap function that you're trying to implement is not possible in AS3. The input parameters are references to the input objects but the references themselves are passed by value. This means that inside the function you can change the cardA and cardB but those changes will not be visible outside the function.
Edit: I added this portion after you edited your question with sample usage.
It seems like you're trying to swap two objects in 2 different arrays at given array positions in each - you can create a function for this in AS3 but not the way you attempted.
One possible implementation is to pass the arrays themselves and the positions that you're trying to exchange; something like this:
// Assumes arrays and indices are correct.
public function SwapCards(playerHand:Array, playerCardIndex:int,
playerDeck:Array, playerDeckIndex:int):void
{
var tempCard:Card = playerHand[playerHandIndex];
playerHand[playerHandIndex] = playerDeck[playerDeckIndex];
playerDeck[playerDeckIndex] = tempCard;
}
Note that you still exchange references and the arrays themselves are still passed by reference (and the array references are passed by value - you could, if you wanted, change the arrays to new arrays inside this function but you wouldn't see new arrays outside). However, because the array parameters refer to the same arrays inside and outside the function, you can make changes to the contents of the array (or other array properties) and those changes will be visible outside.
This solution is faster than cloning the card because that involves allocating memory for a new Card instance (which is expensive) and that temporary instance will also have to be freed by the garbage collector (which is also expensive).
You mentioned in a comment that you pass cards down to lower levels of code - if you don't have a back reference to the arrays (and the positions of the cards), you will not be able to easily swap cards - in AS3, all input parameters are copies (either the copy of the value for primitive types or the copy of the reference for complex objects - changes to the input parameters in a function will not be visible outside).
EDIT: renaming the function from clone to copyFrom as pointed out by aaron. Seems like clone is supposed to be used as objA = objB.clone()
At this point, I'm adding a copyFrom() function in the Cards class such that
var temp:Cards = new Cards(...);
var a:Cards = new Cards(...);
...
temp.copyFrom(a);
...
temp will be copying everything from a.
public function swapCard(cardA:Cards, cardB:Cards) {
var temp:Cards = new Cards();
temp.copyFrom(cardA);
cardA.copyFrom(cardB);
cardB.copyFrom(temp);
}
I will wait for a week or so to see if there are any other options
You have some good answers already, but based on the comments back-and-forth with me, here's my suggestion (I use "left" and "right" naming because it helps me visualize, but it doesn't matter):
function swapCard(leftCards:Array, leftCard:Card, rightCards:Array, rightCard:Card):void {
var leftIndex:int = leftCards.indexOf(leftCard);
var rightIndex:int = rightCards.indexOf(rightCard);
leftCards[leftIndex] = rightCard;
rightCards[rightIndex] = leftCard;
}
Now you can swap the cards in the two examples you posted like this:
swapCard(player1.hand, player1.hand[s], player2.hand, player2.hand[t]);
swapCard(player1.hand, player1.hand[s], player1.deck, player1.deck[rand]);
However, note that while this swaps the cards in the arrays, it does not swap direct references to the cards in those arrays. In other words:
var a:Card = player1.hand[0];
var b:Card = player2.hand[0];
swapCard(player1.hand, a, player2.hand, b);
// does not change the references a and b, they still refer to the same card
a == player2.hand[0];
a != player1.hand[0];
b == player1.hand[0];
b != player2.hand[0];
Typically, this sort of thing is handled by dispatching a "changed" event so that any code that cares about the state of a player's hand array will know to re-evaluate the state of the hand.
There's a deep misunderstanding going on here. The question is about object reference but the PO is not trying to swap any Object reference at all.
The problem comes from the fact that the PO does not understand the difference between variable and objects. He's trying to swap variable/object reference which is not dynamically possible of course. He wants with a function to make the variable holding a reference to Object A, swap its object reference with another variable. Since Objects can be passed around but not variables (since they are just holders (not pointers)) the task is not possible without a direct use of the given variable.
To resume:
variables are not Objects!
variables hold a reference to an object.
variables cannot be passed in function or referenced in functions because THEY ARE NOT OBJECTS.

How a linked list is practically possible in AS3.0

I stumbled across this page : Create Linked List in AS3
Since AS3.0 is a scripting language. I wonder how come linked list is possible in AS3.0 ? Isn't pointer ( a way to access memory location ) mandatory to create a linked list. That ultimately makes array of data faster in performance ?
In AS3 you have object references, you don't have pointers exactly, but you can achieve a linked list using the references in a very similar way. The advantage of a Linked List (in general) is in insertion and deletion within the list (you don't have to shift all elements as in using an array). You still get this benefit using object references.
Note: Objects in AS3 are passed by reference, primitives are passed by value.
Effectively all scripting languages do work with pointers.
They only decided to call them differently (most times they call them "references") and to hide the complexity (or even possibilities) of managing the memory allocation and releasing.
Having said that the simplest way to create a linked list in ActionScript (or JavaScript) would be
var node1 = {value: 1};
var node2 = {value: "foo"};
var node3 = {value: "bar"};
//of course this code should be localices within a separate class
//with some nice API
((node1.next = node2).next = node3).next = null;
//and then use like that e.g.
var n = node1;
while (n) {
trace(n.value);
n = n.next;
}