How do I retrieve values from Dictionary instance variables?

Get help using Construct 2

Post » Tue Apr 21, 2015 2:51 pm

Hi,

It's been awhile. I'm trying out the latest beta of Construct 2 and I'm trying to create a virtual QWERTY keyboard that as you enter, will detect Japanese kana characters in romaji and convert them to the right character. Ex: if I type in "ru", the game should detect and translate it to "る". So I'm trying to make use of the Dictionary object. Now, while I was able to input a series of romaji -> kana instance variables, I notice that I'm not able the use the Dictionary.Get('name') command. It always returns a 0. However, when I do get something when I use Dictionary.name.

So for example, Dictionary.ru will return me "る". But Dictionary.Get("ru") will return me 0.

I need to use the first method because my source is a hidden text element or global string variable that detects keyboard input.

Any thoughts what my problem is here? Or maybe there's another way to go about this?

Thanks!
B
7
S
1
Posts: 42
Reputation: 1,058

Post » Tue Apr 21, 2015 3:02 pm

Could you share your events for setting the dictionary?
Either by screenshot or by sharing your capx?
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Apr 21, 2015 3:27 pm

Here you go.

Image

So as you can see, I've tried loading in the key-value pairs as JSON. Still wouldn't work. What I'm doing here is logging text into a text input field first before trying to see if Hiragana.Get (a dictionary) can work with the inputted keys. In this example, Hiragana.tsu works, but since this isn't what I need, .Get is what I'm trying to get working.
B
7
S
1
Posts: 42
Reputation: 1,058

Post » Tue Apr 21, 2015 3:42 pm

Yeah, I see why that doesn't work..

Hirigana.tsu works because you are calling the instance variable.
Not sure why you'd give the dictionary object all those instance variables, but ok..

To do what you are trying it would probably be a lot easier to use an array..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Apr 21, 2015 4:03 pm

Construct 2 doesn't accept just any JSON string, but wants it in a special format
something like this, if I'm not mistaken
{"c2dictionary":true,"data":{"n":"ん","na":"な"}}
and so on

If you have the full version of Construct 2, you can start your layout in debug mode and check out if the keys are filled with your json string's data - I think they aren't.

Also note that on created does not activate for the dictionary if you don't create one via some event. If you don't create it, use "on start of layout" instead to add all the relevant keys to the dictionary from the json string.

@LittleStain: I think Dictionary is the right choice, since you have name -> value pairs. In an array, you would need to do a for each x and then first look up the correct row that contains the key, before you can retreive the value for it.
Last edited by mindfaQ on Tue Apr 21, 2015 4:22 pm, edited 1 time in total.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Apr 21, 2015 4:17 pm

Looks like it's not being loaded? Key count is 0... Hmm.. I'm confused by that special json format. Which gets the double-double quotes?
B
7
S
1
Posts: 42
Reputation: 1,058

Post » Tue Apr 21, 2015 4:21 pm

sorry, it was a mistake by copy paste from another topic and then meedling within, here you can see the right format:
{"c2dictionary":true,"data":{"n":"ん","na":"な"}}

Also note that your on create trigger doesn't fire off, if you don't create another instance of this dictionary within an event of yours (and why would you). Use something like "on start of layout" or compare the keycount and if it is 0 for the dictionary, then do the json loading.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Apr 21, 2015 4:34 pm

Looks like it's allergic to single quotes.. As for the JSON loading, do you know how to load up the JSON file after having imported it into the project files?
B
7
S
1
Posts: 42
Reputation: 1,058

Post » Tue Apr 21, 2015 5:00 pm

Ok, I'm up to the point where I can use Ajax plugin to load up the data into a text box... But for some reason, the Dictionary isn't "importing". I've got:

on start of layout, AJAX -> Request Hiragana-data.json (tag: "hiraganaData")
on "hiraganaData" completed, Hiragana -> Load from JSON string Ajax.lastData

Am I doing the dictionary loading right?
B
7
S
1
Posts: 42
Reputation: 1,058

Post » Tue Apr 21, 2015 5:24 pm

Oh nevermind. I tried your sample and it worked. I thought the c2dictionary part was just a filler example
B
7
S
1
Posts: 42
Reputation: 1,058

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 28 guests