• 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!

How do i find object with variable with lowest value?

V

veko

Guest
Hello!
I have objects called "obj_past" and they all have counter variable that each step adds one
create event: counter = 0;
step event: counter ++

And i want to create instance on object with lowest counter value (object is called obj_tail)
Is there a way to do that? Thank you in advance!

And one more thing, is there a way to make that obj_tail constantly follow exactly that object it was created on?
Thank you!
 
D

dj_midknight

Guest
Code:
var current = some giant number;
var test;
with(obj_past){
    if(counter < current){
        test = id;
        current = counter;
    }
}

var new = instance_create(x,y,obj_tail);
new.counter = current -1;
new.follow = test;
Code:
//step event
var xx = follow.x;
var yy = follow.y;
//some code to follow this object
 

sp202

Member
@dj_midknight: Current and test need to be global variables in your example. Also, I'd be careful with using var as it localizes variables to events/blocks of code, not the whole object.
 
D

dj_midknight

Guest
I intended for the variable to be local scope but the follow variable is intended to be an instance scope variable. Since its all in a single code execution should not test and current be available until the end of new.follow's assignment?
 

sp202

Member
You're using with. If current and test are local, the object you are acting through using "with" won't be able to access them.
 

Mick

Member
You're using with. If current and test are local, the object you are acting through using "with" won't be able to access them.
I have a habit of using local variables in "with" statements like in @dj_midknight 's example and it works well, I don't have the latest version of GMS 1.4 though, has this changed?
 
You're using with. If current and test are local, the object you are acting through using "with" won't be able to access them.
I disagree. The documentation says variables declared as "var"are, in fact, available for use within a with{} statement, and I successfully use them this way as well.


with{} documentation

EDIT: I should say I respectfully disagree - Just want to make sure the information here is accurate.
 
Last edited:
V

veko

Guest
I am also trying to figure out a way to make that object constantly jump to position of earliest object created (the one with smallest "counter" value) and when another object is created to jump to position with second smallest value and third with third smallest etc. can someone please help me with that?
 
Top