Functions and For Each Loops...

Get help using Construct 2

Post » Thu Apr 18, 2013 1:04 am

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.







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.
B
5
Posts: 19
Reputation: 713

Post » Thu Apr 18, 2013 2:59 pm

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.

FunctionForEach.capx

Perhaps post the CAPX, or issolate the issue in a smaller example.blackhornet2013-04-18 15:00:15
ImageImage
B
66
S
19
G
121
Posts: 3,195
Reputation: 72,852

Post » Thu Apr 18, 2013 9:13 pm

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.
B
54
S
28
G
18
Posts: 1,520
Reputation: 24,990

Post » Fri Apr 19, 2013 12:55 am

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.
B
5
Posts: 19
Reputation: 713

Post » Wed Jul 03, 2013 1:54 am

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.
B
5
Posts: 19
Reputation: 713

Post » Wed Jul 03, 2013 2:09 am

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.
B
40
S
10
G
2
Posts: 1,148
Reputation: 6,495

Post » Wed Jul 03, 2013 2:29 am

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?
B
5
Posts: 19
Reputation: 713

Post » Wed Jul 03, 2013 3:06 am

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

examplevtrix2013-07-03 03:27:31
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Wed Jul 03, 2013 6:12 am

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.
B
5
Posts: 19
Reputation: 713

Post » Wed Jul 03, 2013 9:11 am

@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.
B
44
S
15
G
9
Posts: 1,096
Reputation: 11,013

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 11 guests