recursive function picking wrong sprite instance?

Get help using Construct 2

Post » Wed Jul 23, 2014 2:30 pm

I'm working on a card game and I was trying to make a function that sets 7 attributes to the card sprites 7 variables as each card is dealt.

If I do it manually (without using a function) it works fine, but if I take the code and make it a function ("setcard") it ends up picking and setting the variables to the card instance before (one less than) the one it should set.

I'm not on my computer, but it looks something like this:

function "deal cards"
if counter < 5
spawn card sprite
set card sprite randomly from deck array
*right here is the issue. without using the function it's something like
set var 1 from array
set var 2 from array (and so on..)
wait 0.4 seconds (so it doesn't deal them all simultaneously)
I then add one to the counter variable (so it'll know when to stop dealing)
and then I recall "deal cards"

this works perfectly.

but as I'm going to be setting those 7 attributes each time a card is dealt, I wanted to replace
set var1 from array
set var 2 from array (and so on..)
with a function ("setcard") which contains the exact same code

but when I do that, it ends up setting each card with the variables that should be on the card next to it, and the last card dealt's variables all end up unset (0).

Could someone explain to me why calling a function filled with the code, and replacing the function call with the exact same code is not the same thing in this case?
B
86
S
43
G
71
Posts: 601
Reputation: 43,619

Post » Wed Jul 23, 2014 2:52 pm

Do you pass your card UID so you can pick the correct card in your function ?
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Wed Jul 23, 2014 3:09 pm

I thought about doing that, but I don't understand why I should have to if it can choose the correct one without using the function (and is only 1 off with the function).

Am I incorrect in thinking that replacing a function call with the exact same code from the called function should do the exact same thing?
B
86
S
43
G
71
Posts: 601
Reputation: 43,619

Post » Wed Jul 23, 2014 3:52 pm

The SOL (selected objects list) doesn't transpose into function calls. You have to redo all pickings. You can either pass your newly created card UID to the function and add a pick by UID condition, or create the card inside your function, since an implicit picking is done on "create object" calls.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Wed Jul 23, 2014 3:59 pm

Good to know.
Easy enough to fix, just wondered why.
Thanks very much for the explanation!
B
86
S
43
G
71
Posts: 601
Reputation: 43,619


Return to How do I....?

Who is online

Users browsing this forum: David181, evillair and 58 guests