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

Calling an instance

Zymeth

Member
I struggle with instances. I've written below what kind of feature I am trying to implement, but in short - I don't know how to call for variables of an instance and modify an instance by its properity outside it.
Please, write me a short code that would call a value from a specific instance, so I can use it in a code. I understand little from the manual in this topic and it is very unintuitive for me, so please explain it as simple as possible.
Thank you a lot for any help.

I've made a grid, in which objects can be placed. Each grid element has a separate id (whole numbers between 1 and 16, depending on the grid size).
To not confuse myself, I avoid using built ".id" thing for now.
Each grid element should be free=false when an element is place in it, which is not an issue for me.
I need to check which first grid element (with the loswest ID) is free to take an element in order to send it there, if the player tries to put a placable object outside the grid.
I got the objects numbered correctly in order as I wanted, but how do I call and modify their properties separatley?
My plan is to make function returning coordinates of an object that is free (empty) with the lowest ID.
I'd do that with a loop, checking for each grid object if it is empty.
 

Nidoking

Member
Please, write me a short code that would call a value from a specific instance, so I can use it in a code.
iid.varname

I mean, that's literally as simple as it gets. If your problem is that you don't know how to put an instance id in a variable, that's a different thing altogether.
 

Nidoking

Member
Fix.

I mean, really... you have to DO something with it. Otherwise, it's like just having a single word where a sentence should be. What do you want the iid.grabbed to DO?
 

Zymeth

Member
Lets say I want to make it change some value if its specific value is true. I've wrote empty ifs and the programm could still launch without an error, but in this case it looks like this:
1628554761376.png

I would something like this to work - where I can refference to a properity of a specific instance by its properity (worst case scenario, by its built in ID):
1628554953638.png
 

Nidoking

Member
Okay... so how are you identifying the ObjGridRosterWar that you want to check? Is the "grid" you spoke of earlier a ds_grid?
 

TheouAegis

Member
You are typing 6.grabbed. That is not proper syntax. You might be able to get away with (6).grabbed, but the proper method is something like ID.grabbed. I'm not sure waht you're trying to do with that ObjGridRosterWar[ID=6] line of code, though. Do you mean just ObjGridRosterWar[6].can_store_warrior? Or do you mean something more like
Code:
for(var i=0; i<16; i++;) {
    if ObjGridRosterWar[i].can_store_warrior {
        //do something
        break;
    }
}
 

Zymeth

Member
Well - this looks like it and I thought it would work, but I get errors - either before the launch or as the script tries to go off (nothing that is red worked).
Through show_debug_message(id), I get to know objects ids - but I still don't know how to utilize it in the code.

I am not sure if I understand the question - I try to call a single instance. The grid I've created is not a ds_grid, just a set of objects created through another object, except for the first block placed manually which I kept, so I can position whole grid easily.

Grid creation from an object
Code:
// ROSTER GRID

global.RosterWarMax = 16;

var start_spacing_x2 = ObjGridRosterWar.xstart+global.width;//46
var start_spacing_y2 = ObjGridRosterWar.ystart+global.height;//112
var _xoffset2 = 0
var _yoffset2 = 0

for(i = 0; i < global.RosterWarMax-1 ; i++)
{
var rowcut = 0;
var hmod = 0;
if i > 2
{
    rowcut=1
    var hmod =(4*global.width)
}
if i > 6
{
    rowcut=2
    var hmod =(8*global.width)
}
if i > 10
{
    rowcut=3
    var hmod =(12*global.width)
}


var _cur_grid_x2 = start_spacing_x2 + _xoffset2 + (i * (global.width)) - hmod;
var _cur_grid_y2 = start_spacing_y2 + _yoffset2 + (rowcut*global.height) - global.height;
instance_create_layer(_cur_grid_x2, _cur_grid_y2, "INSTAN", ObjGridRosterWar);
}
Function called on left pressed on ObjWarrior (whose parent is ObjCommander)
Code:
if (grabbed)
{
    global.grabbedtype="warrior"
    Back()
    grabbed=false
    global.grabbed=false
}
else
    {
        grabbed=true
        global.grabbed=true
        global.grabbedtype="warrior"
    }
Script
Code:
function Back(){
var inst;
inst = instance_position(mouse_x, mouse_y, ObjGrid);
show_debug_message(inst);
show_debug_message(inst==noone);
show_debug_message(global.grabbedtype);
show_debug_message("-------------------------");

var canmove = 0;
if global.grabbedtype="warrior" and instance_position(mouse_x, mouse_y, ObjGridRosterWar)
    canmove = 1
//if global.grabbedtype="archer"
//if global.grabbedtype="priest"

if (inst == noone) and !canmove and global.grabbedtype="warrior"
{
with( instance_position(mouse_x, mouse_y,ObjWarrior)) // WORKS - moves the object to the roster
  {

/*
for(var i=0; i<16; i++;) {
    if ObjGridRosterWar[i].isfree
    {
x = ObjGridRosterWar[i].x
y = ObjGridRosterWar[i].y
        break;
    }
} */

/*
if ObjGridRosterWar.100005.isfree
{
x = 100005.xstart+32
y = 100005.ystart+32
}
*/

/*
if ObjGridRosterWar[10005].isfree
{
x = ObjGridRosterWar[10005].xstart+32
y = ObjGridRosterWar[10005].ystart+32
}
*/

x = ObjGridRosterWar.xstart
y = ObjGridRosterWar.ystart

//x = ObjFreePosSprite.xstart+32
//y = ObjFreePosSprite.ystart+32


show_debug_message("THIS")
return 0;
  }
}


if (inst == noone) and !canmove // CAN'T MOVE, IF CAN"T STORE THIS TYPE OF UNIT
{
x = xstart
y = ystart
}
else
{
Snap()
    }
}

function Snap(){

{
move_snap((global.width),(global.height))
}}
for(var i=0; i<16; i++;) {
if ObjGridRosterWar.isfree
{
x = ObjGridRosterWar.x
y = ObjGridRosterWar.y
break;
}
}

if ObjGridRosterWar.100005.isfree
{
x = 100005.xstart+32
y = 100005.ystart+32
}

if ObjGridRosterWar[10005].isfree
{
x = ObjGridRosterWar[10005].xstart+32
y = ObjGridRosterWar[10005].ystart+32
}


When I click outside the grid, I'd like the ObjWarrior to get to the first free position in the grid - but I don't know how to do it, so for now it always go to the same place.
1628598376469.png[/CODE]
 
Last edited:

Nidoking

Member
var start_spacing_x2 = ObjGridRosterWar.xstart+global.width;//46 var start_spacing_y2 = ObjGridRosterWar.ystart+global.height;//112
object dot variable syntax here...

instance_create_layer(_cur_grid_x2, _cur_grid_y2, "INSTAN", ObjGridRosterWar);
... but you're creating more instances of that object here.

You don't seem to understand the fundamental concept of the difference between objects and instances. You must learn this if you want to make games.
 

Zymeth

Member
I don't know what you mean by "object dot variable syntax here..." , but I suppose you are trying to say I've created multiple objects instead of instances.
And I suppose I am fine with that, but the issue remains the same - I still don't know how to check for each properity of an object separatley, or whatever I create with this code.
I am stuck with it 3rd day and I've looked for built in variables or functions to make this work, reading what you've sent leaves me quite confused.

Ah, I think I know what u mean - but well, this is not an issue I suppose. I manually place the first grid block, so it is easier for me to control its position in the UI layout.
 
Last edited:

Zymeth

Member
I've made a workaround, but this video seems introducing functions that I wanted from this thread.
However, I did not test them :
 
Top