Functions and For Each Loops...

0 favourites
  • 14 posts
From the Asset Store
40 ambient sound loops. A wide selection of looping ambient sounds for different locations.
  • Hey everyone, so I am having an issue I was hoping some of you may be able to help explain to me. I created a function in my project. Inside the function, there is a For Each Object Loop which goes through every instance of the object type I'm looking at and runs some code. The problem I am having is that when I run the game the For Loop only fires 1 time if it is part of the Function call. If I take the For Loop out of the function call entirely and make it a separate event it works perfectly and does exactly what I need it to do other than the fact it is running at all times and not just when I use the function. However, once I re-insert the function into the For Each Loop the problem reappears. Does anyone know why this might be happening? For reference I have included some screenshots of the function so you can get a better idea of what I'm saying.

    <img src="" border="0" />

    <img src="" border="0" />

    <img src="" border="0" />

    Any help on this would be really appreciated as I think I had this issue with other functions in previous projects and Ive never been able to make it work. Is there something about the way a For Each Loop works inside of a function that I need to be aware of, or is it something else entirely?

    Anyway, thanks for any help you canprovide.

  • We'd need to see more of the code. Here's a sample of running various combinations of functions and ForEach. All work as expected.


    Perhaps post the CAPX, or issolate the issue in a smaller example.

  • Yeah where are you calling the function? If you're calling it in the same event that the blocks are created in then it won't work.

  • Okay, I finally got it working. Ramones was correct, it was having an issue because I was trying to call the function which caused the for loop in the same event which created the blocks. I had to use a somewhat stupid workaround to fix it, but now that I understand the reason for the issue and got it fixed, I should be able to spend some time thinking of a more elegant solution. Thanks for the help.

  • Okay so I am reviving this question because I would like more clarity on what ramones said. What is the relevance of me creating the Blocks in the same Event as the one in which I call the Function? I have been working on a better version of the game I was originally working on which handles much of the code more efficiently and I find that since I don't really understand what the problem was to begin with, I can't actually create a better solution for it. I know that the issue can't be as simple as not being able to edit an object in the same event you create it since I do that all the time. If anyone could explain to me why ramones made his original comment, it would be a huge help. If you still need the code I can attach it, but I don't see a reason to since ramones' comment was made without knowledge of how the code was structured before, and I am really just looking for clarification on that. Also, how would I tag ramones so that he himself could see this post and hopefully chime in?

    Thanks for any help you can provide.

  • I think what ramones refer to is that if you create objects like that, they are actually not available until next top level as far as I understand, so when you manipulate the object, in the same event, it doesn't actually exist, even though it sometimes work anyway. I personally find it pretty confusing as well, and sometimes things that seems very simple, takes a long time, because you spend so much time trying to make workarounds to it.

    There should be a built in functionality of some sort, that created a temporary object that you could actually manipulate, and then at first possible opportunity it should copy the temporary object to the correct one or something, so even though it doesn't exist at that time, you could still work with it, without even knowing it weren't the correct object. But anyway think that its something like that ramones refer to.

  • So basically, until I have officially launched a new event, the objects are not available? This makes sense I guess, but it still doesn't explain why I can manipulate the objects in some ways the moment after I create them, and not in other ways within functions which are called after the objects are created. Also, I had previously attempted to put the creation of the objects into one Function and the manipulation into another and then call each function separately within the same function. Shouldn't this solve the problem? In theory once the function which creates the objects has run its course they should then be available to any action, whether it is called by the same event or not. Isn't this correct, or is there still something I am not understanding?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • functions dont keep the objects that are selected when calling a new function, you have to pass them with a parameter and repick,

    parameter(0) = object.uid and then repick object.uid = parameter

    adjusting objects after creation should work (have no troubles with it)

    there is a special case now but i dont understand it

    ps: ok yes calling function right after creation loses the selected/created objects, but with parameter it works


  • I actually am using parameters in mine. I create the object, then I pass the object into the function. I have tried this with both the UID as the parameter and by creating an instance variable within the Block that I had complete control over and using that as the parameter and neither works.

  • DaveSilver

    to ask someone specifically use the symbol in front of their name.

    I am just starting to use functions for the first time and despite everything looking logical to me sometimes they just dont work. Like you i have produced workarounds but its a lot of trial and error.

  • At this point it will probably help if I just post the capx file so people can look at my code and see if they can determine the issue.

    The function where I am having the issue is the "CreateStartingBlocks" function. In this function I am creating a grid of Blocks and after I create each Block I assign it a BlockID. I then attempt to call the "RemoveSpawnedMatches" function with the number I assigned as the BlockID as the parameter for the function.

    This function is supposed to take in a BlockID, check if there is currently a Block with that BlockID, and if there is it is supposed to ensure that the Block is not neighboring any Block which is the same color as it is.

    I know that RemoveSpawnedMatches works because earlier I tried making a separate Event which goes through each Block one by one and sends them into the RemoveSpawnedMatches function and it worked perfectly. Right now I am trying to determine why it won't work in this specific scenario. Any help would be greatly appreciated.Here is the download link

  • I cant actually run your program due to some third party addon.

    But have you tried instead of making a function called "RemoveSpawnedMatches" just to make it as a On create block and then do the tests there?

    Also a workaround, if it suits you program, is to add an Boolean to the blocks, and if its true, that block need to be tested.

    So you can just add an event:


    Block.need_tested = true

    For each block

    Function call "RemoveSpawnedMatches"


    And then just turn off/on the Boolean if you need to test a block or not.

  • Okay so it seems I have been wrong about what exactly the problem was for a while. Originally I thought that the problem related to the RemoveSpawnedMatches function not firing within the function that creates the Blocks, when in-fact it is firing. The issue is that for some reason the function is never detecting the matches when it fires. Now this is very strange since I know the function works and detects matches perfectly, and have tested it in other scenarios within this same game.

    I think the problem has something to do with the fact that the Blocks are being made while it's trying to check for matching neighbors to remove simultaneously and somehow that is preventing it from working. I'm not sure why this would be an issue since it is making the same way you would populate a 2d array and thus even if not every block is made yet it should be able to detct matches perfectly with any existing blocks, but it is an issue nonetheless.

    nimos100, I attempted to implement both of the solutions you've suggested but they both seemed to fail for the same reason that mine does. I even went as far as putting all of the stuff in the RemoveSpawnedMatches function directly into the function which makes the Blocks(on a side note I'm not sure why I didn't do this to begin with) and I still ran into the issue I mentioned above where it gets into the initial part of the function where it knows it should perform the check, but never finds any matching neighbors it needs to modify.

    You can download the version which has it setup the way I just mentioned at the link below. You'll notice it also has some disabled code which can act as a workaround to the issue. For now I am just using the workaround until I can find a better solution, but if anyone can figure out why it's not working without the workaround I'd be incredibly grateful.

    Also, nimos100 did you mean that I am using a plugin which prevents you from running it, or that you are using a plugin which prevents you from running it? I only ask because as far as I know I have no plugins in my C2 and it would be a potential issue if I did.

    Thanks again for any help that anyone can provide.

  • "I attempted to implement both of the solutions you've suggested but they both seemed to fail for the same reason that mine does."

    The last solution should work, you might have to change some code etc, as I use a solution like that in my own program. But you might have to move it above the other event, so it doesn't trigger before next tick.

    However its to difficult to figure out, whether it suits your solution or not. As mine is designed to work with such, so its probably better that you use your own if it works for you. :)

    "did you mean that I am using a plugin which prevents you from running it, or that you are using a plugin which prevents you from running it? I only ask because as far as I know I have no plugins in my C2 and it would be a potential issue if I did."

    I got the information from your program, that I couldn't start it. I don't use, and have never used any plugins. Even though I think a lot of them could be useful, I don't want the troubles with an update to C2 suddenly not working with a plugin I used and then my program doesn't work etc etc. :D

    (But ill try running it again and post what it says, just to be sure :))

    EDIT: Hmm weird I started it now without any problems. So it must have been a glitch of some sort.

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