User Tag List

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Thread: Question about Loops and Nested Loops

  1. #1
    Clicker Fusion 2.5

    Join Date
    Aug 2015
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about Loops and Nested Loops

    Hello! I am still learning a lot and I'm new to posting (I hope this is the correct spot), but I've come across a problem with my code that I can't seem to solve. Here a detailed description of the situation (MAIN ISSUE listed lower):

    I start by spreading IDs through tiles in the map and then assigning a random value to "distance"

    Always spread 0 in ID of "sensor" object.

    I then have a main loop that will cycle through all tiles in the map. When ID = loopindex, it will create 4 "sensors" N S E W of the tile. Next, I run the first nested loop. This loop runs # of "sensors" times. When ID of sensor = loopindex( nested1 ) and sensor is overlapping a tile, take the "distance" value and store it in the sensor.

    Once the loopindex ( nested1 ) = number of sensors, I start a loop "findhighest" # of sensors times. The next bit of code determines which of the four sensors has the lowest stored value (that they got from the tile they are on top of) and erases the others. Once the loopindex ( findhighest ) is equal to the number of sensors, I run 1 loop ( setdirection ). On loop, if ID (tile) = counter-stored loopindex( main loop ) , set direction to look at the one remaining "sensor" and then delete it.


    MAIN ISSUE:

    I told Fusion to run the main loop 1 time (to set up a controlled environment). The code as is causes the desired results. Once I up the main loop (called VECTOR) repetitions to anything higher than one, it skips to the very last tile and then completes the nested loops. If anyone is brave enough, I've attached the project file. I did my best to organize it for easy reading.

    Thanks ahead of time to anyone who can help me out!
    Attached files Attached files

  2. #2
    Clicker Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export Module
    Fusion 2.5 (Steam)
    schrodinger's Avatar
    Join Date
    Nov 2014
    Posts
    3,155
    Mentioned
    27 Post(s)
    Tagged
    1 Thread(s)
    That's a complex piece of code if you've just started using Fusion (welcome!)
    cool!

    If I got it correctly, I think it can be vastly simplified:

    HELP Nested Loops_1.mfa

    though if you have specific questions on nested loops,
    foreach loops, or other mechanics, let us know

  3. #3
    Clicker Fusion 2.5

    Join Date
    Aug 2015
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the fast reply and edit! I am actually not new to Fusion 2.5 as I've been around since MMF2. This is my first time coming to the forums because I've gotten WAY over my head. Essentially this program is supposed to cause each node to point to the one around it with the lowest distance value, like it does in your edit. This is part of the program I'm working on for a goal-based vector "pathfinding" algorithm.

    I have a few more questions.


    The loop will sometimes cause certain tiles to not have a direction (ie pointing to a node). I'll try to work with it but if you have a quick fix off the top of your head I'd love to hear it.

    Previously I used 4 "scanners" but your change only uses 1 and is placed on the tile rather than all around it. Why is that and how does that work so much better?

    This code is "part 2" of the algorithm. "Part 1" scans outward like A* but from the goal, writing increasing distance values as it progresses. Right now, it works but takes about 2-3 seconds to fully scan the frame. I need it to all happen before it draws the frame. In the end, both Part 1 and Part 2 will be stacked so after the distance values are set, it adjusts the vectors (each part can happen on different frames if the strain is too much). If you're willing to take a look, let me know and I'll upload that as well.

    Thanks again! I'm really interested in game design and right now I'm testing different concepts to hopefully use later.

  4. #4
    Clicker Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export Module
    Fusion 2.5 (Steam)
    schrodinger's Avatar
    Join Date
    Nov 2014
    Posts
    3,155
    Mentioned
    27 Post(s)
    Tagged
    1 Thread(s)
    The loop will sometimes cause certain tiles to not have a direction (ie pointing to a node). I'll try to work with it but if you have a quick fix off the top of your head I'd love to hear it.
    You mean in the latest example above?
    I think the only tile not having a "correct" direction should be the "target" itself, because it would point to itself
    but on the other hand - which would be the correct direction to point for this tile?
    You could make an animation with "no direction", like a single center dot without line
    (if this can be of any use for your application of course )


    Previously I used 4 "scanners" but your change only uses 1 and is placed on the tile rather than all around it. Why is that and how does that work so much better?
    The example doesn't use scanners at all, since you were already storing the "distance" value in tiles,
    I've simply cycled through all tiles (with the foreach loop), finding the one with lower distance value,
    and then placing the green tile as a "target" on top of that (this object could be avoided entirely)

    ...though, reading your latest post, I'm not sure the example does what you were aiming to do
    as I'm not sure I understood what you are willing to do:

    this program is supposed to cause each node to point to the one around it with the lowest distance value
    you mean that each "node" (grey tile) should look to the one around it with lowest value in the whole neighborhood (same "cluster" of 100 tiles)
    or only among the 8 adjacent ones.. which the example doesn't do and I think you probably need if you're making a pathfinding algorithm!

    Btw, if you are interested in Pathfinding and willing to look at other "precooked" solutions,
    there's the Pathfinding extension, and a number of Fusion-made implementations (including mine which is cross-platform) you could look into.

    Otherwise, I could modify the example above to do so, without detectors (assuming you'll work with a strict "grid")
    but the better (fastest) way to do this is probably using an array

  5. #5
    Clicker Fusion 2.5 DeveloperiOS Export ModuleSWF Export Module
    Sumo's Avatar
    Join Date
    Jul 2008
    Posts
    642
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hey I'm interested in your pathfinding algorithm that you're working on. A couple years ago I created an A* pathfinding algorithm by scratch and it was pretty fun. This is the first time I'm looking into goal-based vector pathfinding. I know Schrodinger also made his own custom pathfinding algorithm as well in Fusion. I haven't looked at the file yet since I'm at work now, maybe I'll take a look at it later.

    Instead of creating four actives in the cardinal directions when testing for the distance, why not just use one detector and position in four times around the node you're checking using another nested loop. Or better yet, record your distance values in an array that's linked to the grid on your level then just compare the data values from the array using offset X and Y array cells? Just food for thought. When I created my system I used a list to record each node and each line had a bunch of data separated by commas. I used string delimiters to parse the data.

    Another pathfinding example that was pretty good was Sketchy's example from the Daily Click. You can check that one out here: http://www.create-games.com/download.asp?id=8850

    I'm sure other people on this forum can tell me how to improve and optimize my system, but it was definitely fun figuring it out!

    I made a video showcasing it if you wanna check it out.


  6. #6
    Clicker Fusion 2.5

    Join Date
    Aug 2015
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry if I'm not 100% clear. This algorithm I'm making is still new to me and my approach is probably not the most efficient. Here is exactly what is going on in the bigger picture (entire algorithm):


    - Starting from the tile that is the goal, distance values are set based on the main loop index. For example, the starting tile (which is the goal) would be ZERO. Each loop the program adds any unchecked tiles in each cardinal direction (4 directions total for this algorithm's purposes) and then sets their distance value.

    NOTE: I would use 8 directions but I don't want my units to clip over obstacles. If there is a way to get around this I am all ears and willing to learn.

    - Once all the tiles have been checked and updated, the second phase begins. Here, every tile checks its neighbors (the 4 around it) and then finds the one with the LOWEST distance value. Then the tile doing the "looking" stores another value which will "point" to that LOWEST distance value tile.


    Essentially, the program updates the distances and the vectors based on the goal, and then the units "seeking" follow the direction (vector) of the tile they are on top of. This really isn't a true pathfinding algorithm, but accomplishes the same goals. The reason I chose this method instead of true A* is that this will allow for potentially 100s of units to seek the goal at a much faster computation speed than finding paths for each unit using traditional A*.

    Here is a video example of what I'm talking about and trying to replicate in Fusion 2.5. I don't plan on using any code to make the units spread out based on density like his does, though I might consider something like it in the future.


  7. #7
    Clicker Fusion 2.5 DeveloperiOS Export ModuleSWF Export Module
    Sumo's Avatar
    Join Date
    Jul 2008
    Posts
    642
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Does that mean you have to loop through every possible tile in your game to update its distance value relative to the goal? Sounds intensive and it would take awhile to check all of them, maybe a few seconds at most even with the nested loops. I guess it also depends on how big the area is and how big your tiles will be. Maybe you can restrict the search area to lower the requirement, as long as you know it doesn't hinder the search process. As long as I'm not misunderstanding the video.

  8. #8
    Clicker Fusion 2.5

    Join Date
    Aug 2015
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have an example somewhat working right now that is a 640x480 screen with 20x20 tiles that runs the first half of the algorithm. Considering it is a very rough and non-optimized program, it isn't lightning fast but is still considerably speedy. I'll link that file here if you want to see that.

    I've done a lot of reading and I know that in larger maps, you should divide the map into smaller "sections" containing a certain number of tiles. Using that, you only update the values in the section the player is in every frame and only update the other sections to "pathfind" from that section to the next nearest section once if the player is not in those areas.

    So right now I'm just working on a relatively small map. Later I'll work to scale it up.

    I'll post the example. I didn't add in any notations for this one, but using "0" "1" and "2" will cycle through the different view styles so you can see the "scan", the "distance values" and invisible.

  9. #9
    Clicker Fusion 2.5

    Join Date
    Aug 2015
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is Part 1 of the Algorithm: updating tile distance values based on player position.
    Attached files Attached files

  10. #10
    Clicker Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export Module
    Fusion 2.5 (Steam)
    schrodinger's Avatar
    Join Date
    Nov 2014
    Posts
    3,155
    Mentioned
    27 Post(s)
    Tagged
    1 Thread(s)
    Interesting video, the effect looks very cool also,
    though - off top of my head, so I may be more easily mistaken -
    it doesn't seem to be very different from non-vector-field pathfinding?

    The first step is what most pathfinding algorithms actually do (calculating the distance -steps- of each cell from the "target" position)
    the second step is avoided (drawing the vector field)
    but once you have step one, it would be highly unefficient (as Sumo says) calculating vector directions to each adjacent cell
    and generally unuseful, as you can simply calculate those vectors for the single objects lying in the field whenever you need to poll those
    (like, obejct at cell 33,12 wants to move, and so polls for vectors around cell 33,12, reading grid values got on step 1)

    can't see how drawing all the vector field can grant better performances
    unless you use a very large number of objects going pathfind and constantly moving (swarms or particles, like in the video)
    then doing the math on the grid *could* maybe be more efficient than picking single objects of the swarm
    (always speaking of Fusion --- in other languages probably not?)
    and probably in all languages, when the swarm is larger than the number of cells in the grid


    EDIT
    had the audio turned off on first seconds of the video - ok, so it is what the guy says, it is useful for a very large number of objects.
    However, if this is your goal, with similar figures, I'm sorry this may be not possible to do with Fusion,
    you'll very likely have problems when in the order of few hundreds of objects
    don't know if these figures can suit your needs?

    you should divide the map into smaller "sections" containing a certain number of tiles
    this would surely help, for the vector field, but handling a very big number of objects will be a problem nonetheless

Page 1 of 2 1 2 LastLast

Similar Threads

  1. Nested ForEach loops?
    By King_Cool in forum Multimedia Fusion 2 - Technical Support
    Replies: 4
    Last Post: 10th March 2017, 06:05 PM
  2. Nested Foreach loops
    By Pixelthief in forum Fusion 2.5
    Replies: 6
    Last Post: 8th June 2014, 06:52 AM
  3. Nested Loops and cycling properly
    By rubes in forum Multimedia Fusion 2 - Technical Support
    Replies: 1
    Last Post: 26th January 2013, 05:48 PM
  4. Nested loops and get loopindex
    By Shadoku in forum Multimedia Fusion 2 - Technical Support
    Replies: 2
    Last Post: 27th September 2007, 05:13 PM
  5. nested loops
    By westray in forum Multimedia Fusion 2 - Technical Support
    Replies: 7
    Last Post: 14th December 2006, 05:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •