roboscan's Forum Posts

  • thanks for the suggestion... I just switched from C2 to C3 last night so I didn’t even know there were any new random number tools to try. I will try that.

    (The only reason I didn’t post my code is because it’s very much a prototype at this point with lots of other issues that I was afraid would cloud the issue I was trying to get help with :-). I’m going to try some other options and will fall back to this if I’m not successful. I will post a cleaned up version of my non-working event sheet)

    Thanks again for your input!

    J

  • Hi,

    I am trying to make a unique random number picker for a project.

    The issue I seem to be having is that the loops I created to carry out this process only seem to be iterating one time and not "looping." I am wondering if I am not setting the loops up properly (meaning physically, nesting the condition and sub-events properly in the event sheet).

    My intentions are to repeatedly pick a random number, compare it to the members of a picked-number-log array. If it matches a member of the array, the program will pick another number and try again. If it does not match anything in the array, the program will add that number to the array and return that value as "thisIssue."

    (I am using the result of this as an Index to pick some JSON data.)

    Currently, I created a function ("issuePicker") that contains a Loop (I've tried both WHILE and REPEAT with the same result) that should repeat until a "unique" condition is satisfied (unique < 1).

    When the WHILE/REPEAT loop starts, it picks a random number (assigns it to the var "thisIssue").

    I then run a ForEach loop on a "issueLog" array and compare each member to "thisIssue".

    If a match is found, it adds 1 to the variable "unique" (unique = unique+1).

    If no matches are found, "unique" remains at 0.

    In the next condition, I check If unique < 1 and if so, Stop Loop.

    What I believe should be happening is that for each iteration of the WHILE/REPEAT loop, the ForEach Loop inside of it should execute for each number of elements in the "issueLog" array. If any of the ForEach checks fail, the WHILE/REPETE loop should iterate again and so on until an actual unique random number is found.

    What seems to be happening, however, is that the For Each loop is only executing one time and the WHILE/REPEAT loop is also executing one time. Even if I screwed up the exit process for the WHILE/REPEAT loop, (I think) I should be seeing the For Each loop iterate for at least the number of times there are elements in the "issueLog" array. Correct?

    (I added a test text box and have some "append texts" that should be appending the LoopIndex for the ForEach and the counting and displaying the loops for each WHILE/REPEAT iteration. Both of these text boxes only show a single entry for each of these every time I run the "issuePicker" function.

    When I look at debug, I can see that the random numbers ("thisIssue") is getting added to the "issueLog" array. (In addition I put some dummy entries in the "issueLog" array to start with just for testing... so the For Each should be running multiple times, but it is not. I even tested my WHILE loop by adding the condition "unique < 10000" (just to see if it would hang up my program for 10000 loops) there was no difference.

    Does anyone have any ideas? Does it have something do with my putting the loop inside of a function? Am I just not nesting the conditions and sub-events properly?

    Any help would be greatly appreciated.

    Thank you,

    J

  • Sorry I didn't reply sooner, but I just wanted to say thanks again for your help!

    I made that last "game" for my fiance who is an elementary school teacher doing "distance learning" activities with her students like so many others. I am in the process of making a few more interactive activities for her classes using Construct and what you showed me is making that process a bit easier for me than my first version :-). So thanks for helping educate an (early) middle-aged guy and for helping some little kids learn, too!

    J

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi AllanR, thank you so much for your help / time!

    What you did is certainly what I am looking for (my EventSheet had 26 "events" on it so yours was, in fact slightly more efficient :-))

    Besides some of your logic being way more efficient / dynamic than mine, if you wouldn't mind, could you let me know if I am understanding this correctly as this is key to what I was asking:

    1. I see you used a FOR loop in the beginning to spawn new instances of the "Objects" in which you set their initial frame, position, comment and image-point instance variables you created. - This give me a much better understanding of how I can utilize instance-variables, thank you! (I wanted to use a loop to do some initial setup on my version, but since I was using multiple objects I couldn't - well, not without giving them object names ending in numbers and doing int->string conversions in my loop... seemed to complicated so I didn't bother).
    2. The "Objects on DragDrop drag start" is picking up any instance of "object."
    3. Then the object is Dropped, you are performing the actions of final positioning of the Object, adjusting the score and displaying the text all based on the object's Instance Variables???

    The last part is what I was the big one I was missing - my question here is: if I were writing Javascript, I would use "this." to reference an Object's properties... Does the script here "capture" (not sure if that's a good word) the "this." of the Object I was dragging just by dragging that specific object? If so, that is exactly what I have been missing. I was trying to figure out how to "tell" the script to use the properties of the specific object that was being clicked / dragged / touched without having to specifically code for that object.

    If I got that right, THANK YOU for enlightening me! (If I didn't, please let me know.)

    (I also didn't even know about the tokenat() function which will also come in handy for future use.)

    Again, thank you very much for taking your time to help. I really appreciate it!

    J

  • Hi, I am not sure if I am asking the question the best way, or if this answered and I can't find it or just don't understand...

    I've made a handful of small games with Construct 2 and can't seem to figure out how (unless it can't be done) to program functions or set variables dynamically based on objects that meet given criteria. (eg object clicked, or object in a specific position, etc). It seems like I have to re-write code over and over for individual, like objects.

    So for example, say I have multiple buttons and I want some actions to take place for each one that is clicked. Some of those actions may be the same for all (maybe a sound effect, or they open a menu or something) while others would be different (eg the specific action associated with that button).

    Conventionally, I would think do it this way:

    If any button was clicked:

    Determine which button it was;

    Perform some "you clicked any button action"

    Perform some other "you clicked any button action"

    Somewhere later in the code, I could have something like:

    Switch (id of button that was clicked before){

    case A:

    do something specific

    case B:

    do something else

    Case C:

    do something different

    }

    Currently, It seems I have to go about it a different way, repeating nearly all the code for each button:

    If specific button A was clicked:

    Perform some "you clicked any button action"

    Perform some other "you clicked any button action"

    Do something specific

    If specific button B was clicked:

    Perform some "you clicked any button action"

    Perform some other "you clicked any button action"

    Do something else specific.

    If specific button C was clicked:

    Perform some "you clicked any button action"

    Perform some other "you clicked any button action"

    Do something else specifically different.

    (My above example isn't that dramatic, but note the lines of repetitive code)

    of course, I have "cheated" by creating global variables that are set when each button is clicked and then later perform action based on the value of that global variable, but that still feels like a work-around, rather than a proper solution.

    To use an actual example, I recently created a simple kids game where they have to drag words from the right column over pictures on the left. When the word is over the correct picture, something happens. I created the game and it works just fine, but I know I didn't go about it the most "code-efficient" way. (see it in action here: http://jasonlipset.com/testmatch)

    I made the "words" from a sprite-sheet with the thought that I could randomize the order of the words on game start. So, I created the word objects as sprites from a sprite sheet, made 6 instances of them and had each start on a different frame - It didn't work. All the buttons started on the same frame.

    I tried giving each button an instance variable and using that as an identifier but couldn't seem to figure out how to select by the instance variable.

    Secondly, when it came to dragging-and-dropping - when I dragged one, I was dragging all of them at the same time because it seems that I could only reference the draggable object by Object Name (not the instance variable).

    So the logic of the game as it stands, is that you drag the word-object on the right and when you you "drop" it within a specified distance from the position of the correct blank-rectangle-object on the left, it performs a bunch of actions (snaps the word-object into position, colors in the graphic, plays a sound effect, adds a point).

    My logic is fine, but I ended up creating multiple clones of the my word-objects, each with a different object name (deleted the instance vars) and multiple clones of the blank-box-objects. (I could have just used X/Y coords. without actual blank-box objects, but didn't think of that until afterwards.) Then I copied and pasted (editing the relative object names accordingly) each section of code that carries out my functions for each one of the words. This does not make for very efficient scalability.

    In other programming environments, I would have probably used some kind of loop, array, If/else or switch statement that would set/select all the variables regarding position, graphic manipulation, and sound file, based on which item I was dragging in the first place and then just fired off the "function" or lines of code that perform the actions using those variables. But I can't seem to do that in the Construct 2 environment.

    If this is a limitation of the Construct 2 environment, so be it, but if I am going about things the wrong way, any help and suggestions would be greatly appreciated!

    Thank you,

    J