recursive function picking wrong sprite instance?

0 favourites
  • 5 posts
From the Asset Store
Template for maze generation using recursive backtracker
  • 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?

  • Do you pass your card UID so you can pick the correct card in your function ?

  • 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?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • Good to know.

    Easy enough to fix, just wondered why.

    Thanks very much for the explanation!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)