• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

referring to an instance stored in a variable

Hey guys, Im trying to have my game pick a random instance of an object (enemypar) and then store that random instance to a variable.
Then, I'd want to refer to that variable in this a statement, for example:

<variable here>.distance_to_object(playerpar) < 100

So that the randomly picked 'enemypar' instance's distance to 'playerpar'-object would be calculated.

Now i can manage storing an object to a variable but it gives me a dead end in the part where i want to refer to that variable in, for example, the distance_to_object() code. How do i refer to it there?
'inst.distance_to_object(playerpar) < 100' for example throws an error.
I want to make it so that if the distance between this random instance of 'enemypar' and 'playerpar' is less than 100, stuff happens.
thanks beforehand!
 

Yal

🐧 *penguin noises*
GMC Elder
'inst.distance_to_object(playerpar) < 100' for example throws an error.
use distance_to_object(inst.playerpar) < 100 instead. The dot operator binds with the variable, and not any further. (So you should see thing.variablename as a single variable reference, not two separate things)
 
GML:
inst.distance_to_object(playerpar) < 100 // will return an error

// I think if you convert it like below

var dist = distance_to_object(playerpar)

inst.dist < 100

// or like below:

inst.(distance_to_object(playerpar)) < 100 // notice extra brackets

// it should work... but i cant say for sure. just my understanding.
Something to do with not being a value when wrote out like you have but converting it to a variable like you did with inst should help.

Someone can explain better just figured I'd give it a go.
 
use distance_to_object(inst.playerpar) < 100 instead. The dot operator binds with the variable, and not any further. (So you should see thing.variablename as a single variable reference, not two separate things)
hey, it gives an error in the script, says 'Variable name expected' if i write it as 'distance_to_object(inst.playerpar) < 100 '

GML:
inst.distance_to_object(playerpar) < 100 // will return an error

// I think if you convert it like below

var dist = distance_to_object(playerpar)

inst.dist < 100

// or like below:

inst.(distance_to_object(playerpar)) < 100 // notice extra brackets

// it should work... but i cant say for sure. just my understanding.
Something to do with not being a value when wrote out like you have but converting it to a variable like you did with inst should help.

Someone can explain better just figured I'd give it a go.
hey, thanks, but still no luck... it doesn't seem to recognize the 'inst.' in any case...

You can't use dot notation for a function. (At least, not in 2.2 or with built-in functions.) You need to use with(inst).
okay, as in:

with(inst) {
if distance_to_object(playerpar) < 100 {
// stuff happens?
}
}

does the 'with' statement only stand for the following statement (in this case the 'if distance_to_obj') or does it go beyond that?
 

Yal

🐧 *penguin noises*
GMC Elder
hey, it gives an error in the script, says 'Variable name expected' if i write it as 'distance_to_object(inst.playerpar) < 100 '
Do you set inst before using it? Also, "variable name expected" usually means you're trying to use a constant (e.g. the name of a resource) instead of a variable. Is any of your objects named inst or playerpar?
 
thanks for replies,
my code in Step Event now looks like this:

GML:
if canfind = 1{
inst = instance_find(enemypar, irandom(instance_number(enemypar) - 1))
canfind = 0}

if global.renown >= 70{
if instance_exists(inst){
with(inst){
if distance_to_object(playerpar) < 256{
instance_create(x,y,obj_aura)
}}}
So, I have to have the upper line (the one where the instance is stored in the variable 'inst') in here in the Step event, not Create, right?
Since having it in Create gave errors about the variable having not been set.
But since it's in Step, I should have the variable 'canfind' to ensure it doesnt constantly store a new random instance to the variable in every Step, or am i right?

The code still gives errors though, about 'var 'inst' not set before reading it', but for some of the children of the 'enemypar' object now. Like, if I test and the var inst is assigned to, say, object 'obj_monster1' the error reads

obj_monster1.inst(100060, -2147483648) not set before reading it.

So do I need to give a blank 'inst' variable to every object under enemypar?

You've got curly braces there {}. That tells you exactly what it covers.
ofc, silly question. thanks,
Also, if i want to have an instance_create() event create an instance of another object in the location of the 'inst' variable, how do I write that?
Since
GML:
instance_create(inst.x,inst.y,obj_aura)
gave error, and so did
with(inst) instance_create(x,y,obj_aura)
?
So i want to have 'obj_aura' created in the spot where 'inst' is.
 

Yal

🐧 *penguin noises*
GMC Elder
In enemypar's create event, set inst to noone (which is a special value representing no valid object). In every children of enemypar, run event_inherited() as the first line of the create event. This means any variables set in the parent's create event gets set to those values, even if you add new ones later. Really useful for default values (you can always set them to new stuff in the rest of the childrens' create events)
 
thanks for replies,
my code in Step Event now looks like this:

GML:
if canfind = 1{
inst = instance_find(enemypar, irandom(instance_number(enemypar) - 1))
canfind = 0}

if global.renown >= 70{
if instance_exists(inst){
with(inst){
if distance_to_object(playerpar) < 256{
instance_create(x,y,obj_aura)
}}}
So, I have to have the upper line (the one where the instance is stored in the variable 'inst') in here in the Step event, not Create, right?
Since having it in Create gave errors about the variable having not been set.
But since it's in Step, I should have the variable 'canfind' to ensure it doesnt constantly store a new random instance to the variable in every Step, or am i right?

The code still gives errors though, about 'var 'inst' not set before reading it', but for some of the children of the 'enemypar' object now. Like, if I test and the var inst is assigned to, say, object 'obj_monster1' the error reads

obj_monster1.inst(100060, -2147483648) not set before reading it.

So do I need to give a blank 'inst' variable to every object under enemypar?
Looking solely at that code, again I could be wrong but I'm sure you need:

GML:
var inst = // everything else on this line of code
Unless you have inst = 0 in the create event...
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
You can use a local variable for _inst in the Step event...

GML:
var _inst = noone;
if canfind = 1
{
_inst = instance_find(enemypar, irandom(instance_number(enemypar) - 1));
canfind = 0;
}

if (global.renown >= 70)
{
if instance_exists(_inst)
    {
    with(_inst)
        {
        if (distance_to_object(playerpar) < 256)
            {
            instance_create(x,y,obj_aura);
            }
        }
    }
}
Also note, you should really end all statements with a semi-colon. While GML is forgiving, this can cause issues with the YYC and is also generally good practice. ;)
 
thanks for advices, all of you!
and yeah, semicolon has honestly been a mystery to me. been using it here and there.
my code is now like this:

GML:
var _inst = noone;
if canfind = 1
{
_inst = instance_find(enemypar, irandom(instance_number(enemypar) - 1));
canfind = 0;
}

if !instance_exists(obj_aura)
{
if canfind = 0
{
if (global.renown >= 70)
{
if instance_exists(_inst)
    {
    with(_inst)
        {
        if distance_to_object(playerpar) < 256
            {
            instance_create(x,y,obj_aura);
            }
        }
    }
}
}
}
aaand we've run into yet another problem.
for some reason, the distance is the issue now; its a Step event so it be checking constantly until the conditions are fulfilled, but it seems to only check it once; that is, if at the start of the room, the var 'inst' is inside the distance_to_object (256). if I test it by making the player character go all around the room, near enemy objects, (going through ALL of the enemies in the room) the conditions never seem to fulfill and the instance_create() action never happens... it only happens if i change the distance into the width of the room, and then test the room: THEN the action happens since now all the enemypar-objects are inside the distance_to_object.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
The issue suggests that one of the variables "canfind" or "global.renown" are not being set as you think they should be. This is where the debugger is your friend! Add a breakpoint to the code then step through it a line at a time and see what the values are for those variables.
 
The issue suggests that one of the variables "canfind" or "global.renown" are not being set as you think they should be. This is where the debugger is your friend! Add a breakpoint to the code then step through it a line at a time and see what the values are for those variables.
hm, nothing came up... weird. have to keep testing. Im lost here... seems like something in the code prevents it from being checked every Step, or so...
 
Wait... where do you set canfind back to 1?
It never sets back to 1, the event is only supposed to happen once maximum per room.
I did manage to find a workaround tho, so this topic is, in a way, solved!
Still bothers me tho, my failure to solve this 😬 it feels like the answer would be so simple. then again, aint the 1st time i'm taking ages to resolve a supposedly simple issue... maybe I'll get back to this sooner or later just to crack the code...

unless one of you guys manages first🤣 do tell me!!
thanks for all help here tho
 
Just don't set canfind to 0 until you've found an instance.
I thought it finds the instance 'immediately' in the Step event so I made it set the canfind back to 0 right after, but yeah, I can put it so that it only sets canfind = 0 once instance_exists(inst) or smth like ...
 

Nidoking

Member
Oh, pfft. I'm looking at it again, and you intended _inst to be an instance variable that retains its value from one step to another. Don't declare it as a var then. Just do that part in the Create event - choose an instance to target, store it permanently, and drop the canfind variable entirely.
 
Oh, pfft. I'm looking at it again, and you intended _inst to be an instance variable that retains its value from one step to another. Don't declare it as a var then. Just do that part in the Create event - choose an instance to target, store it permanently, and drop the canfind variable entirely.
Right, thats what I thought, but I think it gave me an error 'Variable name expected' in the Step event, when I declared the variable 'inst' in Create - as in it didn't recognize it from the Step event? But I'll try again, since the code I originally used in Step has changed somewhat from when I first posted this thread! so i might be wrong.
 

Nidoking

Member
You can't use "var _inst" in the Create event either. Don't put var. var means "forget this the moment the script or event we're running right now ends" (among other things).
 
You can't use "var _inst" in the Create event either. Don't put var. var means "forget this the moment the script or event we're running right now ends" (among other things).
Ah! now thats new to me. thanks a ton. learn something new every time here!! So i already have come up with a workaround but still feel clunky about it, so imma probably test this when i can!
 

Nidoking

Member
If this is new to you, then I recommend looking up unfamiliar terms in the Manual before using them. It may be tedious at first, but you'll learn a lot very quickly.
 
Top