3
votes

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?

4
In AS3 you can only pass objects by reference. Only primitive types can be passed by value (those cannot be passed by reference unless you put them inside more complex objects and pass those).xxbbcc
Objects are passed by reference and primitives are passed by value in AS3. This is same in Java, contrary to the statement in your questionKejml
Where and how are these cards used, and what is your intent on swapping them?Aaron Beall
about pass by reference and value I think it really depends on how you define pass by reference. javadude.com/articles/passbyvalue.htmFail Flash
The cards are used in a battle/card game and the swap function is used before and during the battle. I'm trying to make classes reusable for my future games and thus all these troubles :(Fail Flash

4 Answers

5
votes

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).

0
votes

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

0
votes

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.

-1
votes

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.