How do I Retrieve from Dictionary based on Number Range?

0 favourites
  • 13 posts
From the Asset Store
Explore and retrieve all Magical Books from the hidden Orc's Lair in this "PacMan-styled" game.
  • I'm quite new to Construct 3 still, so I'm just dabbling on some little projects to learn.

    I have a dictionary with two instance variables: Excellent and Awful. They both are just a sentence of text.

    I am randomly generating an integer between 50-100. I have a text box that I would like to populate with my Excellent instance variable text if the random integer is 80-100 and would like to populate the text box with my Awful instance variable text if the random integer is 50-79.

    Thanks for the help!

  • I have a dictionary with two instance variables: Excellent and Awful.

    Do you mean with two keys?

    You can use "System is between values" condition:

    set r to int(random(101)) <- notice it's 101, not 100
    
    Is r between 50 and 79 : Set text to dictionary.get("Awful")
    Is r between 80 and 100 : Set text to dictionary.get("Excellent")
    
    
    
  • Thank you! I am a bit confused where I actually add in the "is r between..." parts in the event sheet. Learning curve!

  • For example, this is what I have (and it's not working, obviously). I want this all to happen with the button click, but when I move the system number checks up, they don't line up with the correct action. I must be misunderstanding how it works.

  • 1. You want to turn the value checks into a subevent of the on clicked event

    2. You need to split those up. Right now the number would have to be both between 50-79 AND between 80-100 which is of course impossible.

  • Definitely getting closer. I was surprised that this did not work.

    Side question: I see how I can add comments like you have that sort of appear overtop of the action, but how do you "hide" the comment if you want to review the code?

  • The conditions are still not separated in your screenshot. They need to be each in an individual block. Pressing B will create an empty subevent of the current highlighted event. Very useful shortcut :)

    I don't think there is a way to hide comments actually.

  • Ok, so now when I run it, the WriteUp textbox only shows 0.

    I am wanting it to show one of these dictionary pulls, based on the number:

  • So you did mean instance variables! Is there a reason why you store those texts in instance variables on the dictionary object, and not in the dictionary itself?

    If you need to access instance variable value, just use Dictionary.Good or Dictionary.Excellent

  • Definitely no reason. I know nothing about how these dictionary entries work and thought I had to do it that way. How do I properly set them up in the dictionary then? Taking my time to get a good base of understanding working. The FAQs I've been reading don't seem to connect to what I'm trying to do.

  • The most simple, basic way is to call the "add key" action (maybe on start of layout)

    construct.net/en/make-games/manuals/construct-3/plugin-reference/dictionary

    You'd call it twice, once for "good" and once for "excellent", then the dictionary.Get() will work properly.

    If you wanna go more into depth, you'd load the data into the dictionary via AJAX. An example for that can be found here editor.construct.net

  • Thank you!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • mikeadamski26 Dictionaries are used when you need to store a lot of data, say, an entire dialogue or inventory system in the game. It may also be useful when you need to access the data dynamically, for example, to get a specific dialogue line for NPC_Name&"_greeting", where NPC_Name is a variable.

    If you just need to store two strings of text, a dictionary would be an overkill. You can use two global variables (GoodMessage and ExcellentMessage). Or local variables inside the event where you display the text. Or even hard-code the text in the "Set text" action, which is not a good programming practice, but definitely the easiest way to do this.

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