Jump to content


Photo

Storing clicked instance's ID


  • Please log in to reply
12 replies to this topic

#1 nodgene

nodgene

    GMC Member

  • GMC Member
  • 85 posts
  • Version:GM:Studio

Posted 26 February 2012 - 05:08 PM

Hello!

This is probably a very simple problem. I want to know how (in code) to store an instance's ID for use as a variable.

Currently I have it so on a "Left (mouse) released" event, the object in question does this:

global.object_selected = self.instance_id


In the Step, it then runs an if query to determine if the global ID is the local ID, and then to change the sprite(/colour) to show that the player can use the clicked item.

if global.object_selected == self.instance_id
{
//do stuff
sprite_index = sprite2
}
else if global.object_selected != self.instance_id
{
sprite_index = sprite1
}

But it isn't working and I must have done something very wrong. I want to be able to store the clicked instance ID in a global variable to allow the player to manipulate the individual instance they have just clicked.

Any help, greatly appreciated.
  • 0

#2 RangerX

RangerX

    GMC Member

  • GMC Member
  • 926 posts
  • Version:GM8.1

Posted 26 February 2012 - 05:29 PM

Not sure what to make of your code there but I can suggest your a simple way of storing an instance ID by clicking.

Check the function "collision point". This returns and instance ID. What you would need to do is a collision point at the mouse's position when you click. Something like:

StuffIClicked=collision_point(mouseX,mouseY,all,1,1)

You get what happens there? The variable "StuffIClicked" will contain the instance ID that the collision point function will return.
  • 0

#3 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5400 posts
  • Version:GM:Studio

Posted 26 February 2012 - 05:44 PM

you will never have any use for self in gamemaker .. in other programming languages you would.

global release event
global.object_selected = id


if global.object_selected == id
{
//do stuff
sprite_index = sprite2
}
else
{
sprite_index = sprite1
}

another way is to have another object check global.object_selected and draw something over it if it is selected:
with(global.object_selected){ // you should set the variable to the value noone as default
  draw_sprite(select_spr,0,x,y)
}

EDIT: instance_id which you tried to use is accually a array list of all the instances in the room, it does however seem like your code should have worked never the less..

Edited by FoxInABox, 26 February 2012 - 05:47 PM.

  • 0

#4 nodgene

nodgene

    GMC Member

  • GMC Member
  • 85 posts
  • Version:GM:Studio

Posted 26 February 2012 - 05:47 PM

Thanks (both of you)! I didn't quite understand my own code either. Anyway, that seems to do it.

Edited by nodgene, 26 February 2012 - 05:49 PM.

  • 0

#5 nodgene

nodgene

    GMC Member

  • GMC Member
  • 85 posts
  • Version:GM:Studio

Posted 26 February 2012 - 06:06 PM

Wait just a moment. This may be becoming an unrelated issue... but when I retrieve the value for one object (that exists as a sort of turn button in the top left) it always returns with "1000005". When this object is pressed it creates a randomly placed object, and when I select ANY of any number of those created instances, they apparently are ALL "-4" :| And when I click on one of them, it changes all of them.

Is it that they are all assumed to be the same instance unless I somehow define them as unique individuals (ideally each with individual properties) which can be selected singularly? I may for instance create ten, but I only want it to register an individual instance when I click on that one.

Edited by nodgene, 26 February 2012 - 06:08 PM.

  • 0

#6 RangerX

RangerX

    GMC Member

  • GMC Member
  • 926 posts
  • Version:GM8.1

Posted 26 February 2012 - 06:13 PM

Nope. Each instance is having its own ID no matter what you do. Its something else that your code does. I don't know what it is though because I can't follow you in that post there. I would need to know more about what your game is and when you're actually trying to do there.
  • 0

#7 nodgene

nodgene

    GMC Member

  • GMC Member
  • 85 posts
  • Version:GM:Studio

Posted 26 February 2012 - 06:29 PM

Nope. Each instance is having its own ID no matter what you do. Its something else that your code does. I don't know what it is though because I can't follow you in that post there. I would need to know more about what your game is and when you're actually trying to do there.


Well I'm trying to slowly experiment/learn my way to turn based functionality, a bit like chess or the civilisation series or somewhere in-between. I'm presently trying to grasp how to be able to click on, and then tell individual instances of objects to move and so on. I presumed there needs to be some way of logging what individual instance has been clicked on, in order to then tell that instance to move, etc.
  • 0

#8 RangerX

RangerX

    GMC Member

  • GMC Member
  • 926 posts
  • Version:GM8.1

Posted 26 February 2012 - 06:49 PM

Ok, as soon as you have the instance's ID into a variable (done when you clicked it, as I showed you earlier), it is still possible to address this variable directly, as long as you're still in the same "then"...
But an instance ID is just an ID. It won't tell you if you're clicking a building or a character. Therefore, you will need to give some extra IDs to your objects. Per example, if your object is a house, have a variable in its create event going like:

ObjectID=house;


This will let you give the right orders to the right instances. Let's have a little go at it...


StuffIClicked=collision_point(mouseX,mouseY,all, 1,1)

if (StuffIClicked>0) // basically, this checked if you clicked an instance at all...
then
{
if (StuffIClicked.ObjectID=house)
then { do what you have to do as a house}
}



You get where I am going?
If you have many different object IDs, you might want to use a switch.

Edited by RangerX, 26 February 2012 - 06:50 PM.

  • 0

#9 nodgene

nodgene

    GMC Member

  • GMC Member
  • 85 posts
  • Version:GM:Studio

Posted 26 February 2012 - 07:45 PM

:o Yes, I think I do. But I'm having some probably very nooby problems.

Firstly, when you refer to "StuffIClicked.etc", I had it as in my case: "global.object_selected". But adding (global.object_selected).objectID brakes everything, and having no global. prefix also is unliked by GameMaker. I'm sure it's just a convention I'm not understanding here which is why I'm confused.

Secondly, I see what you mean about allocating a specific, let's say unit type variable into the creation even of a unit. (Minus the above issue). But I don't understand how this will differentiate between, for argument sake; two or more otherwise identical houses. They are both created as just houses. They maymay not be, say, tanks; but if I select one it'll still react by treating all houses as the same since there's nothing different between them besides that. Perhaps what is needed is another variable to log a unique number or something in that case, for individual houses, etc?

The player may create more than one tank or house or etc, and needs to be able to tell individual ones what tile to move to, etc. And of course they need unique properties like health and movement turns, etc. Just to give you an idea of what I'm thinking. Each house, etc, must be treated individually.

Edited by nodgene, 26 February 2012 - 07:48 PM.

  • 0

#10 RangerX

RangerX

    GMC Member

  • GMC Member
  • 926 posts
  • Version:GM8.1

Posted 26 February 2012 - 08:24 PM

Even if it's 2 houses, they will have alot of different properties and state. As soon as you talk to one of them (when you click it), you can change any of its states or properties. I'm not exactly seeing what you can't do with that...
  • 0

#11 nodgene

nodgene

    GMC Member

  • GMC Member
  • 85 posts
  • Version:GM:Studio

Posted 26 February 2012 - 11:04 PM

RIGHT. I have reworked the whole thing (added a switch too!) and introduced a custom cursor to in theory make life better... but now it doesn't work at all (at least it isn't giving me errors). I'm very lost.

Object: customCursor:

Create
global.turn_number = 0;

object_selected = 0;

Step
x=mouse_x;
y=mouse_y;

Left Pressed
object_selected = collision_point(mouse_x, mouse_y, all, 1,1)

switch(object_selected)
{

case 1: 
    global.turn_number=global.turn_number+1;

    xpos=random(450)
    ypos=random(450)
    instance_create(xpos,ypos,object1);
    break;

case 2: 
    sprite_index = sprite2;
    break;

}

Object: object0:

Create
object_selected = 1;

Object: object1:

Create
sprite_index = sprite1

object_selected = 2;

Edited by nodgene, 26 February 2012 - 11:42 PM.

  • 0

#12 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5400 posts
  • Version:GM:Studio

Posted 26 February 2012 - 11:49 PM

this test file got 2 rooms, just switch the order of them to test the other one
test_selection.gmk

the collision function returns the id of the instance.. you can use the id to access the variables:
ins =  collision_point(mouse_x, mouse_y, all, 1,1)

switch(ins.object_selected){
.. but the extra variable to make a number uniqe to each object is unessesary since you can check object_index for such a number:
switch(ins.object_index){

case object0:

  • 0

#13 nodgene

nodgene

    GMC Member

  • GMC Member
  • 85 posts
  • Version:GM:Studio

Posted 27 February 2012 - 01:27 AM

Thanks everyone! Finally got it to do what I wanted :D

Left Pressed
object_selected = collision_point(mouse_x, mouse_y, all, 1,1)

if object_selected = noone exit; // to awoid error

switch(object_selected.object_index){

case object0: 
    global.turn_number=global.turn_number+1; // end the turn

    xpos=random(450)
    ypos=random(450)
    instance_create(xpos,ypos,object1);
    global.obj = 0; // clear the last selected object
        break;

case object1: 
    global.obj = object_selected // log the object ID for reference
        break;
}

So, now when I press the turn button it spawns objects randomly, and then critically; when I click one of them it logs their ID to a global variable for reference! So that's a way of controlling an individual object as "selected". I think!

Edited by nodgene, 27 February 2012 - 01:28 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users