• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED] controlling several object instances

P

pZq10chars

Guest
Hi,

I have a sprite consisting of 8 images of buttons. every 2nd button is off-state and next to i the on-state button.
So sprite
Code:
image1 = button1OFF
image2= button1ON
image3= button2OFF
image3= button2ON
...
image8=Button4ON
Then i have the sprite assigned into a obj_button And i have placed the 4 buttons into an instance layer.
So i have 4 visible buttons on screen.

In the obj_button create event i have the script to turn all buttons to its corresponding off state
Code:
#macro BUTTON1OFF   0
#macro BUTTON1ON   1
#macro BUTTON2OFF   2
#macro BUTTON2ON   3
...

randomise(); // create new random seed on every run.

// find first instance of obj_button and change the image_index of it to defined value. (4 separate withs)
with(instance_find(obj_button,0)) {image_index = button1OFF }
...
with(instance_find(obj_button,3)) {image_index = button4OFF }
And this seems to work ok. All buttons will get each corresponfing image / color that is drawn in the sprite.

Now the problem im having is, that when i try to randomize what button goes on (one at a time delayed by 1 second, the program goes weird)

So i have a step event for obj_button and there i have

Code:
var value = irandom_range(0,3);
with (instance_find(obj_button, value)) {
   // eg. if instance 0 -> the image to show (turn on button for instance 0)
   // we get 0x2+1 = 1 which means BUTTON1ON state sprite image. and so foth
   image_index = (value*2)+1 ;
}
alarm[0] = room_speed * 1;

then i have and alarm 0 event with only a comment and a new alarm[0] = room_speed *1; to get the alarm repeated... so eg
Code:
/// comment
alarm[0] = room_speed *1;
and i know, it could be without the *1 but its there since if i need to change it to a constant or #macro or similar...

So anyways the main problem is. That the buttons will not be turned on one at a time. They are turned on 1 to 4 at once and thats is the problem im trying to understand. Why does this do this ?
When i put a show_message(string(value)); before the with(instance...){} or after it, it will prompt 4 times a value, and after fourth time, the buttons will react based on what values it did randomly give. It may be 1, 1, 2, 3 and thus all other than first button will turn on. and so on. But why. What am i doing wrong ?

and to recap. I want that one button is turned on at a time -> wait 1 seconds. go off, wait 1 second, then an other random button on for 1 second and so on.
 
Last edited by a moderator:

Simon Gust

Member
The step event runs 60 times a second. Your alarm goes off once every second, but I don't see any correlation between your step and alarm events. You coded them to just run ignoring each other.
You should put the entire code in the alarm event or ask if the alarm has counted down to 0 in the step event and then only execute the code.
 
P

pZq10chars

Guest
Okey, now im starting to understand this MAYBE...
since my object has 4 instances, the code maybe is executed also 4 times. Could it be like this ?
At least that would explain why message is shown 4times before something happens.
Maybe i should have some kind of dummy object (1 object) or similar to run the main logic only once and not four times.

Is there any other way to execute scripts other than creating a dummy object and adding scripts to the object ?
 
P

pZq10chars

Guest
So, i created a separate obj_script object which i added to same instance in the room as the obj_buttons went to. I added a create, and 2 alarms.
Alarm1 did this:
Code:
var value = irandom_range(0,3);
   
    with (instance_find(obj_button,value)) {
        image_index = value*2+1;
    }

alarm[1] = room_speed;
and alarm2:
Code:
with (instance_find(obj_button,0)) {
        image_index = BUTTON1OFF;
    }
   
    with (instance_find(obj_button,1)) {
        image_index = BUTTON2OFF;
    }

    with (instance_find(obj_button,2)) {
        image_index = BUTTON3OFF;
    }

    with (instance_find(obj_button,3)) {
        image_index = BUTTON4OFF;
    }
alarm[0] = room_speed;
and in the create event i created one line calling
Code:
alarm[0] = room_speed;
and this did the trick. So create event calls alarm[0] which turns a random button = ON. which in turn calls for alarm[1] which then turns off all buttons and calls again for alarm[0]
 
Top