View Full Version : Loop help
CiNiMoDZA
11-10-2007, 12:06 AM
Im stuck with loops in GM. In my code I have the following:
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
dislekcia
11-10-2007, 12:49 AM
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
CiNiMoDZA
11-10-2007, 10:44 AM
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?
Tr00jg
11-10-2007, 12:06 PM
What do you want to happen?
CiNiMoDZA
11-10-2007, 12:39 PM
What I want it to do is create the object Add_stats, but it must create at 200+(i*40) everytime it loops!!!
Tr00jg
11-10-2007, 12:55 PM
Well then as Dislekcia said. Put it in the create event then. It will create 3 Add_stats objects.
CiNiMoDZA
11-10-2007, 01:08 PM
But it just exits as I run it???
dislekcia
11-10-2007, 01:43 PM
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
CiNiMoDZA
11-10-2007, 02:03 PM
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!!!
Powered by vBulletin® Version 4.2.4 Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.