User Tag List

Results 1 to 9 of 9

Thread: Loop help

  1. #1

    Default Loop help

    Im stuck with loops in GM. In my code I have the following:

    Code:
    for (i=1;i<4;i+=1)
        {
            instance_create(500, 200+(i*40), Add_stats);
        }
    Is this supposed to be in the step event! It only creates one instance when its in the create event! When its in the step event, it doesnt seem to stop creating instances because my PC just freezes up!! Please help :P

  2. #2
    Game.Dev Moderator
    and bettar-rar game developer than Wea-sel
    dislekcia's Avatar
    Gamertag: dislekcia

    Default

    That loop will create 3 instances of Add_stats objects every time it runs.

    Step through the logic:
    -Loop starts: i is 1.
    -Runs statements: Creates an Add_stats object at 500, 240.
    -Tests condition: adds 1, i < 4.
    -Loop continues: i is 2.
    -Runs statements: Creates an Add_stats object at 500, 280.
    -Tests condition: adds 1, i < 4.
    -Loop continues: i is 3.
    -Runs statements: Creates an Add_stats object at 500, 320.
    -Tests condition: adds 1, i !< 4.
    -Loop exits: i is 4.

    If you have less than three Add_stats objects, then they're being destroyed or invalidated in some other way.

    Yes, if you put that loop in a step event it will create 3 instances every frame. Probably a bad idea.

    -D

  3. #3

    Default

    So where should I put it? When I put ti in the Create event and tell it to compile, it loads and then exits straight away?

  4. #4

    Default

    What do you want to happen?

  5. #5

    Default

    What I want it to do is create the object Add_stats, but it must create at 200+(i*40) everytime it loops!!!

  6. #6

    Default

    Well then as Dislekcia said. Put it in the create event then. It will create 3 Add_stats objects.

  7. #7

  8. #8
    Game.Dev Moderator
    and bettar-rar game developer than Wea-sel
    dislekcia's Avatar
    Gamertag: dislekcia

    Default

    Post the code.

    You're probably telling the game to exit somewhere. Are you sure that Add_stats is an object? What events and actions does Add_stats have? Your loop is working fine, chances are the problem is somewhere else.

    Read these links to get you up to speed on loops and how they work too, you seem a little afraid of them (hence blaming them for your app not working):
    http://computer.howstuffworks.com/program7.htm
    http://computer.howstuffworks.com/c8.htm

    -D

  9. #9

    Default

    Lol, ja! I am pretty new to loops so everything I try is new to me! I have deleted the code and started from scratch tough!!! Add stats was an object, the code was purple!!!

Posting Permissions

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