0 Favourites

How do I debug dictionary keys all return 0

  • I've been going in circles trying to figure out why dictionary objects do not work in my project (but work fine in blank projects).

    ALL dictionary values look okay in chrome browser debug and in construct2 native debugger

    dictionary.asjson() works fine as well and shows all values,

    but dictionary.haskey and dictionary.get do not work at all and only return 0 and I have no idea why.

    Here is all of the relevant code from my project:

    It's messy because I've spend the last 8 hours changing random things, commenting things out, and pumping out values to try and figure out why "haskey" and "get" don't work on my dictionaries, but it works fine in a new project. Obviously I need it to work in this project and I have no clue what's going wrong.

    If you look I also had to make buffer variables to hold on to the parameters because the parameters DISAPPEAR!!!!! if I try to use them in a sub event. I am completely not understanding scope or how this works or something.

  • skelooth I once used dictionary, honestly I don't really like it because of things that happen to you, once happened to me. I would like to suggest you to use array instead, no particular reasoning to this it just my preference. I rather use dictionary to store simple things only.

    Similar functionality you can exploit:

    Dictionary: haskey; Array: [condition:contain value]

    Dictionary: get; Array: array.at(array,indexof("xxxx"),#)

  • I even bought the c2 data editor out of desperation....

    here is all the relevant code using nothing but the functions given by the c2 data editor! but again it works fine in a blank object. -_-

    In this example, plundermoon0 is the key in a dictionary that c2dataeditor functions load... asjson still works as normal. I'm kinda losing my mind

  • Is there a chance that your miserable situation caused by the c2dataeditor? Probably tag the seller would be a good idea to ask his assistance. I haven't use browser.ExecJS before because I don't know how, so I can't tell whether those parameters are properly forwarded or not.

    It's rare to see advanced question like this though, hopefully better users can give some hint.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • This is going to kill. I just switched everything out to arrays.

    The capx is already kinda big with a lot of files, but I'm stuck.

    The problem is on the event sheet "commlink"

    On buttoncommlink press, it calls create data from the c2 data editor then calls my function "open dialog" right below.

    Open dialog just needs to be able to read a value out.... that's, all. -_- I do not have any idea of why it's not working

    Oh damn, it's too big to upload here

  • No, I don't think it's the c2 data editor's fault at all. There's something I'm not understanding about scope or something, and why it's being clobbered. Literally all of the code involving what I'm trying to do is in those screen shots, I know b/c I went around and Ctrl F'd everything a few times

  • If you look I also had to make buffer variables to hold on to the parameters because the parameters DISAPPEAR!!!!! if I try to use them in a sub event. I am completely not understanding scope or how this works or something.

    Well this is an easy one. Functions are triggered events, and function parameters are only good during that triggered event. But when you use a wait action, everything that happens after it is no longer inside that triggered event. It has been scheduled to run at a later time, and then does so. It does not matter that the wait time is 0.0 seconds, it will still kick the following actions and sub-events out of the triggered event and cause function parameters to fail.

    As for your dictionary problem, my recommendation is to get very friendly with your browser's javascript debugger. Run your project in preview mode, and try to find the javascript functions that correspond to the condition and expression that are failing. Set a breakpoint in them right before you trigger something that you know isn't working properly. Then try to use the available information in the debugger to see why it isn't working.

  • Hey ggibson1 Have you ever heard of this behavior, or see anything glaringly wrong with the code above? I can't seem to get arrays or dictionaries to admit they have values, even though their asjson expression output correctly. I don't think it's the c2 data editor's fault, but I figured you'd probably know better than most.

  • >

    > Well this is an easy one. Functions are triggered events, and function parameters are only good during that triggered event. But when you use a wait action,

    >

    Wait function was temporary and was removed, it was there hoping maybe the object was not in scope and would be available in the next frame. That was not the case.

    [quote:2qrgvczp]my recommendation is to get very friendly with your browser's javascript debugger. .

    If you see the above code, the majority of it is eval statements and console logs. Me and the chrome debugger spend many hours together every day. All javascript objects are populated correctly, but return values all return '0' for all arrays/dictionaries on get/haskey. Unless you can tell me how to specifically watch the process where the C2 engine is getting this 0??

  • Can you isolate the issue in a capx and share it with us ?

  • I am reading over this thread now... I want to start off though by mentioning the functions that come with C2 Data Editor are for searching DataSets which are C2 Arrays that are setup in a specific way. Dictionaries only have a key/value so they do not need all those CRUD functions.

  • No, because it works when I isolate it. Gimmie a few minutes, lemme make a copy and strip all the resources

  • If you see the above code, the majority of it is eval statements and console logs. Me and the chrome debugger spend many hours together every day. All javascript objects are populated correctly, but return values all return '0' for all arrays/dictionaries on get/haskey. Unless you can tell me how to specifically watch the process where the C2 engine is getting this 0??

    Well lets start off by finding the code for the Dictionary 'Get' expression. It would, of course, be in the plugin for the dictionary. That means it will be in Dictionary_plugin.js (in preview mode). Looking through the file, you can see all kinds of function definitions for the conditions, actions, and expressions. Eventually you will see this:

    Exps.prototype.Get = function (ret, key_)
    	{
    		if (this.dictionary.hasOwnProperty(key_))
    			ret.set_any(this.dictionary[key_]);
    		else
    			ret.set_int(0);
    	};
    [/code:19c4bika]
    
    So if it can't find the key, it returns 0. Why can't it find the key? A quick google search tells me that hasOwnProperty is a built in javascript function that tells you if a value exists on an object. So how do we know if the value exists? Set a breakpoint on the line that calls hasOwnProperty, and trigger your broken event (Firefox seems to be really bad at setting breakpoints, use Chrome for this). The browser will stop the game when it calls the Dictionary Get expression (from any part of your event sheet). At this point, hover your mouse over the 'this.dictionary' text, and it will show you all the values that are in it, and if you hover over _key, it will tell you what value it is trying to get. You may be looking at any one of the calls to this function that your event sheet triggers, so it may or may not be the one you are looking for. If not, tell the browser to continue, and it will show you the next call. What you do from here depends on what you find out.
  • I wasn't able to upload it via the forum, I thought deleting resources would lessen the file size but apparently not the capx is at

    and the problems all surround the events in commlink when trying to access the dictionary or array in the openDialog function.

    I've been looking at it so long I was hoping it was something stupid in my code I was over looking.

    Johncw87 thanks! I'll try some breakpoints now

  • Ok look at this CAPX and pay special attention to how

    1) I created a Dictionary

    2) Saved the UID for the Dictionary

    3) Re-"picked " that same Dictionary by UID later on and got its data

    There are other ways of doing things but this is how I always do it... basically in my mind the UID is a pointer to a specific instance like in C++ coding.

    https://dl.dropboxusercontent.com/u/277541264/ShowDictionaryWorking.capx

    [attachment=0:3g9god6e][/attachment:3g9god6e]

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