Existence of Variables in some cases

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1329
Joined: Sun Feb 14, 2010 7:11 pm

Existence of Variables in some cases

Post by Bugala »

@Andreas

I keep bumping into this question of is some Variable existing or not quite often and would therefore request that you would make a longer chapter about the existence of variables.

I am giving an example about something that I am thinking about right now and I am not sure how this goes. This is taken from actual code, but I have changed the variables into simpler names.

Code: Select all

1: Card = Deck:DrawTopCard()

2: ActionQueue.Add("Sequential", Function ()
3:				Card.TimerID = StartTimer(Nil)
4:				ActiveAnimations.Add(Function () MoveCardToXY(Card, X, Y, TX,TY, Easings.Ease_In_Out_Position, "Card1") EndFunction, 
5:					"Card1", Function() ActionQueue.Message_ActionFinished() EndFunction)
6:							EndFunction)
...							
Function GameSpecific.Animation.MoveCardToXY(Card, StartX, StartY, EndX, EndY, EasingFunctionToUse, AnimationName)
7:	Local TimePassed = GetTimer(Card.TimerID)							
What this one does is that first it takes a CARD table from the MatchDeck.

Then this adds an ITEM to ActionQueue, which is actually a function. Idea is that when it becomes this ITEMs turn to be processed in the Queue, it is actually going to be executing another function, which will create add another function to ActiveAnimations to be executed every cycle.

And the function in this ActiveAnimations to be executed is the GameSpecific.Animation.MoveCard


So what I am confused now is what CARD table is being used in all the places

In line 1 it is clear, it is the Card Table I get from the Deck:DrawTopCard()

But what about Line 3. As this line wont be executed until ActionQueue Interval is executed:

Code: Select all

Function ActionQueue.SetInterval()
	ActionQueue.IntervalID = SetInterval(Nil, ActionQueue.Interval, 50)
EndFunction
Is this Card now the same card as I get in Line 1, or will it be whatever happens to be assigned to Card?

As in, this specific location of code is actually taking 5 cards from deck, and intention is for each card to have this same animation to be executed through ActionQueue and ActiveAnimation.

So when I am repeating 5 times these first lines, will i be at line 3 having 5 different Card Tables?

Similarly in line 4 idea is to send reference to this exact line 1 Card Table, so that later in line 7 (and forward) I am specifically dealing with this line 1 card.

But is this now going to be 5 different references to 5 different Card Tables (as is my intention) or will it be so that when I start executing ActiveAnimations every cycle, I will be accessing same Card Table 5 times, instead of 5 different Card Tables?
Post Reply