I need different ways to use the dictionary to store unique values for each object in a family

Not favoritedFavorited Favorited 0 favourites
From the Asset Store
Pathfinding AI
$2 USD
80% off Expiring soon
Units do not overlap each other and use different ways if there are several free ways.
  • The only way I've been able to do is

    But I need at least one more way to do a number value. Anyone got anything?

  • That sounds fine to have a value per type in the family. If you wanted a value per instance you could use cards.uid as the key.

    If you wanted multiple values per type or instance you could just use another dictionary or append something like “:value1” to the key or something. For example cards.uid&“:value1”

  • Consider using JSON instead, you can store any amount of data for each card. For example:

    CardJSON set path to CardName
    CardJSON set ".quantity" to (CardJSON.get(".quantity")+1)
    CardJSON set ".price" to 500
    

    To retrieve a value: CardJSON.get(CardName & ".price")

  • That sounds fine to have a value per type in the family. If you wanted a value per instance you could use cards.uid as the key.

    If you wanted multiple values per type or instance you could just use another dictionary or append something like “:value1” to the key or something. For example cards.uid&“:value1”

    Unfortunately UID is itself a number. The key for a dictionary can't be a number.

  • Consider using JSON instead, you can store any amount of data for each card. For example:

    > CardJSON set path to CardName
    CardJSON set ".quantity" to (CardJSON.get(".quantity")+1)
    CardJSON set ".price" to 500
    

    To retrieve a value: CardJSON.get(CardName & ".price")

    idk, I use Construct explicitly when I don't feel like coding, I don't think there was any other ways to do JSONs unless I'm just blind.

  • Just make the number a string with str().

    I don’t understand the statement about json. It’s a plugin you can use.

  • idk, I use Construct explicitly when I don't feel like coding, I don't think there was any other ways to do JSONs unless I'm just blind.

    Think of JSON as a more advanced and flexible dictionary. It's not that complicated once you try it.

    You can of course use a dictionary. There are two options:

    1. Name the keys in the dictionary as CardType&ValueType, for example "DarkKnight_price":10

    2. Create a new instance of the dictionary for each instance of the card sprite. Link it with the card (for example, by UID). Then each card will have its own dictionary.

  • dop2000R0J0hound

    Sorry for only just responding, been busy with work. But uhhh... well the string thing doesn't work with dictionary, it always comes out as 0, it works with JSON but it's not saving. But I'm also not used to working with JSONs so I'm probably doing that wrong. But even just leaving the layout and coming back it's not doing anything, turns to 0 so uhhhhh... what do?

  • I don’t understand what you mean by str() doesn’t work with a dictionary. Sure it requires that the key be a string but you can convert a number into a string with the str() expression and it’s happy. And I’m not just saying that, I’ve done it a lot.

    I’ll defer the json questions to dop as he uses them pretty heavily. I’ve only used them sporadically and have to re figure out how to use them every time.

    If changing the layouts clears values then the object isn’t global. When something isn’t global it gets destroyed when leaving the layout and recreated when returning to it. You can make an object global in the objects settings. So maybe that’s what you’re encountering?

  • I don’t understand what you mean by str() doesn’t work with a dictionary. Sure it requires that the key be a string but you can convert a number into a string with the str() expression and it’s happy. And I’m not just saying that, I’ve done it a lot.

    Wait I'm confused, I did what you're saying, the value is just never increasing, no matter what I do, it says 0.

  • I think we may be talking about different things at this point. I was talking about something along the lines of:

    Mykey = str(sprite.uid)

    Dictionary: add key mykey with value dictionary.get(mykey)+1

    But I may have misunderstood what was being asked.

  • I think we may be talking about different things at this point. I was talking about something along the lines of:

    Mykey = str(sprite.uid)

    Dictionary: add key mykey with value dictionary.get(mykey)+1

    But I may have misunderstood what was being asked.

    Exact same way I did it for Cards.GetObjectType only that one didn't need to start with str()

  • Exact same way I did it for Cards.GetObjectType only that one didn't need to start with str()

    The code on your screenshot works fine for me.

    Are you using "Set key" or "Add key" action? Use "Add key", because "Set key" only works if the key already exists.

  • > Exact same way I did it for Cards.GetObjectType only that one didn't need to start with str()

    The code on your screenshot works fine for me.

    Are you using "Set key" or "Add key" action? Use "Add key", because "Set key" only works if the key already exists.

    I... I could of sworn I hit copy and paste and then tweaked the words... I'm not sure why it was "set key", you are absolutely correct. Thank you for the clarification.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • > Exact same way I did it for Cards.GetObjectType only that one didn't need to start with str()

    The code on your screenshot works fine for me.

    Are you using "Set key" or "Add key" action? Use "Add key", because "Set key" only works if the key already exists.

    Well now that that's working, I'm trying to make a number variable equal to this, it there a way to recall it and turn it back into a number? Like on start of layout set "number" to dictionary.get(str(Cards.UID)) I just don't think it's converting to a number so it's staying 0 since str is in it, is there a way to fix that?

    If it's not that then I'm confused.

    The first one is changing the value, the second one isn't.

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