Passing info INTO a Function to Use on Function Call

0 favourites
  • 11 posts
From the Asset Store
Assets for creating mountains and ravines environments
  • Hello, everyone.

    For the life of me, Functions - especially the newer implementation of Functions in C3 - has proven to be a challenge for me. I get the concept, but actually using them, and doing so effectively, still is something I struggle to wrap my head around.

    What I'm trying to do here is...

    When the Virus object with a "Green" designation is set, it generates a "field" of small black squares surrounding the Virus. Using an Array ("Green_SqrsData"), I pre-set values for where to position each black square. I know this works because a previous iteration in the code was working perfectly. Now I'm trying to stuff event lines away into a single Function to shave time off of tick cycles because this game is planning to have a LOT of objects working all at the same time. So faster tick cycles means faster gameplay.

    This brings me to my problem...

    PROBLEM: When a specific Green Virus is ready to generate a distortion field of squares, it calls on the Function. But IN THE FUNCTION, itself, it needs to know WHICH Virus object called on it. I need a way to PASS the Virus.UID to the Function so that the Function knows what Virus to spawn squares around. Now, the interesting thing is that when I run it like it is here, the squares (all 144) do spawn...oddly. They simply do not get positioned correctly per the data in the Array.

    WHAT AM I missing or how do I pass the Virus.UID to the Function?

  • Right-click on the function and select "Add parameter" (or press P). Add a parameter VirusUID. This will allow you to pass UID of the virus instance to the function. Inside the function, add an event "Virus pick by Unique ID =VirusUID" and nest all other events under it.

  • dop2000 - I figured out how to do that after re-reading the documentation and used Pick By Evaluate...

    ...but that didn't work.

    I'll amend it to try it as you suggested and let you know if that works as I'm hoping it to.

  • You can use either "Pick by evaluate" or "Pick by unique ID", it doesn't matter.

    What exactly doesn't work? I don't see any mistakes on your screenshot.

    If you are not sure if the function gets called, add debug output to it - for example Browser Log "Green distortion for "&Virus.UID

    Run the game, press F12 to open Console and check the browser logs.

  • Okay, so, Pick By Evaluate I think is what you meant anyway... I thought you were suggesting a different System Event line. Anyway...

    I tested to ensure that the Virus.UID is getting passed to the Function by having some temporary text be set to the Function Instance Variable which holds the UID info. I know now that the UID is getting passed correctly so I know the Virus instance is getting picked correctly everytime the Function is called.

    The problem, then, is that the Black squares are not being positioned around the Virus object correctly.

    Using the For Loop to declare which cell of the Array I want to pull a value from, and then adding or subtracting that value from the X/Y value of the Virus, I tell it where to position the Black Squares. (It amounts to 144 squares in tight box/grid formation. The Array, itself is made up 36 X/Y values (36 on Z=0 for the X position of the Squares and 36 on Z=1 for the Y position of the Squares).

    As stated earlier, this worked in my pre-Function instructions. But now I can't get the Squares to position correctly. They ALL appear, but only in the original X/Y position of the Virus, itself, upon the Function call.

  • (Also, I don't see a "Pick by UID". I see "Pick Nth Instance", "Pick by Evaluate", "Pick by Random", et al. But no "Pick by UID". Anyway, apparently that's not the issue at hand after all.)

  • It's difficult to tell what can be wrong.

    When debugging complex issues like yours I use Browser Log a lot. I would add debug output to all critical events, printing the array values, instance UIDs, variables etc.

    Then run the game in debug mode, pause before and after the function call, open the browser log, inspect the contents of the array, check positions of the black squares etc.

    Also, I don't see a "Pick by UID".

    It's not under System object. It's Virus -> Pick by unique ID

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yeah...it's strange because nothing has really changed other than that I'm using Functions and one less For Loop. And that calling a spawn object action + setting their positions all in the same even line. (Previously, I used conditions to test where inside the now-non-existent extra For Loop it was and then told it to position the newly spawned squares at that time.)

    I haven't changed the values in the Array - the only thing I do is reference them. They'll always remain the same.

    So, if I'm placing the very first Square, it'll call on Array.At(0,0,0) and Array.At(0,0,1) Both values are 15, respectively. So I would tell it to place the Square 15 pixels to the right and above the Virus object.

    .At(1,0,0) and .At(1,0,1) would be X=45 and Y=15...... Place Square to Virus.X + 45 and Virus.Y - 15.

    And so on. Whatever the value in the specific Array cell, that gets added to the Virus coordinate values to determine where to place the Squares.

    Since the Array is 6x6x2, the two For Loops each being 0-5 should cover the X/Y index values of the Array. The Array is contained with the Virus object, too, so picking the Virus object ought to pick the Array. ... I'll try to implement your suggestions further, but I'm not sure how I'm going to do that - rather, I'm not sure what I'm looking for by doing that.

    It's not under System object. It's Virus -> Pick by unique ID

    Ooooh, okay. Gotcha. I need to remember that. LOL

  • I don't know WHY this made a difference, but instead of doing this within the Function...

    - Virus object spawn Square object

    - Set Square object in a position relative to the Virus object according to data in the contained Array

    ...instead I did this...

    - System create Square object at a position relative to the Virus object according to the data in the contain Array

    I tested that for ONLY the first 36 squares (1 out of the four quadrants surrounding the Virus) and it worked.

    This makes little sense, but it works.

  • You can use either "Pick by evaluate" or "Pick by unique ID", it doesn't matter.

    FYI "pick by unique ID" should be a lot faster. "Pick by evaluate" checks every single instance; "pick by unique ID" directly picks the right instance.

    In general it's best to use the most-specific condition that does what you want, since it may be specialised for the purpose.

  • FYI "pick by unique ID" should be a lot faster. "Pick by evaluate" checks every single instance; "pick by unique ID" directly picks the right instance.

    In general it's best to use the most-specific condition that does what you want, since it may be specialised for the purpose.

    Understood. :) I don't think I have many conditions that would necessitate Pick By UID, but I will try to find ways to make it the way I do things. Thank you, Ashley and Dop. I'm always grateful for you both.

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