How do I add to sprite's instance variable? (SOLVED!)

0 favourites
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Greetings all. I'm having the worst brain cramp on this and need some help. I've gone through demo's, tutorials and previous questions. Mental block still there.

    Essentially, I want to increase the number of a sprite's instance value. In the example below, I'm adding 10 to the "leaves" (instance variable) to a tree (sprite). But all the trees are getting the 10 when I add them. Below is the latest version of the code and any help getting me through this would be appreciated.

    I put two trees on the layout (created one, copy/pasted to make a second), added them to the Trees family and created 3 instance variables.

    Thanks everyone!

  • I would guess you are changing your currently selected tree somehow.

    Open your game in Debug mode and pay close attention to where your code breaks down.

    • Can you successfully click a tree and store the Tree's UID in Tree_ID (bad naming convention going from UID to ID but I didn't notice any mess-ups from this in the code you showed).
    • Does the UID match the tree it should match?
    • Can you click on the upgrade button and keep that same UID saved?
    • When you click the upgrade button does a different tree gain leaves? Does Tot_leaves increase?

    These will all help you realize where your code is breaking down

  • If those are the only events I don't see how it can go wrong

  • I would guess you are changing your currently selected tree somehow.

    First, thank you for taking the time to reply. I appreciate it.

    I have the code set so that when I left click a tree, the Tree_ID global variable is set to the Tree_sprite.UID Then when I left click the up arrow, the Tree_ID is used to add 10 leaves. Maybe I'm not selecting the tree correctly?

    Open your game in Debug mode and pay close attention to where your code breaks down.

    • Can you successfully click a tree and store the Tree's UID in Tree_ID (bad naming convention going from UID to ID but I didn't notice any mess-ups from this in the code you showed).

    Absolutely fair. As late in the night as it was and with the frustration with the other project, I went with the dirty to try and figure out why I was struggling. But, fair point and one that I will take better caution wit.

    Does the UID match the tree it should match? It does. The text box is updated every second and the UID matches as I click back and forth between the trees.

    Can you click on the upgrade button and keep that same UID saved?

    When you click the upgrade button does a different tree gain leaves? Does Tot_leaves increase? When I click tree with UID 2, the leaves instance variable will increase however when I click on UID 3, it does not. However the Tot_leaves does increase regardless of which tree I select, which it shouldn't if UID 3 is "broken".

    These will all help you realize where your code is breaking down

    I'll try some other things but you don't see anything in the coding that would prevent it from running as expected? Or do you have a different approach that you would suggest?

  • If those are the only events I don't see how it can go wrong

    I appreciate your time and appreciate a vet like you replying but it is going wrong which I can't explain. Do you see anything programming wise that should be done different?

  • So I went back and deleted all of the sprites from the layout. I then added one tree and copy/pasted it 3 times to have 4 trees total. I can replicate the issue over and over. The original tree will have the instance variable increase however the 3 trees I copied will not have their instance variable increase. The global variable Tot_leaves also increase when I click each tree.

    Is the issue with the way I'm adding sprites (copy/paste)? Using C3 302r stable

    The Tree_ID (gv) changes as it should when I click the tree however the UID remains at 2.

  • The events look fine to me. Tree_ID changes, that's fine. Tree_UID you never use that variable so I wouldn't expect that to change.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ah I see the problem, maybe. You're confirming this from the text object? That's only going to show the stats for one instance. If you look in the debug viewer you'll probably see variables are correct.

  • Tree_ID is the global variable which is set when I left-click one of the trees and capture the sprite's UID [tree_sprite.UID]

    I would have thought the the tree_sprite.UID would change as I select different sprites/trees.

    How would I capture the value of the sprite so I can increase just that sprite's instance value (leaves) and not the leaves value on any of the other sprites?

  • Your code works correctly - I only gave you the list of ways to verify where your issue is because the code you showed us is correct.

    Lionz hit the nail on the head, you are not picking a specific tree when you right the text out. Open your code in Debug mode and you should see each tree holding its own correct values.

  • I would have thought the the tree_sprite.UID would change as I select different sprites/trees.

    It does - but your Global variable Tree_UID is never used.

  • Ah I see the problem, maybe. You're confirming this from the text object? That's only going to show the stats for one instance. If you look in the debug viewer you'll probably see variables are correct.

    I think I'm following you here and took these steps along with screenshots.

    First step, selected the first tree, clicked up arrow to add 10 to instance variable "leaves". Global variable Tot_trees also increased 10.

    Next, selected a different tree, clicked up arrow to add 10 to "leaves" (iv) and no increase occurred however as designed, Tot_trees (gv) went up 10.

    Last, selected first/original tree, clicked up arrow to add 10 to leaves (which it did) and Tot_trees went up 10 as well.

    Only the original tree has the leaves (iv) increase, not any of the copies.

  • > I would have thought the the tree_sprite.UID would change as I select different sprites/trees.

    It does - but your Global variable Tree_UID is never used.

    I thought I did in steps 4 and then 5. Did I not use the pick value correctly?

  • Alright at this point you'll have to share the project

  • I see what you're saying. There's an issue with the selection process.

    Here is what is shown in the debug screen when I left click on the tree.

    and what is shown when I click on the #2 in the debug screen

    So the UID isn't being captured by the left-click command (I'm guessing).

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