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

Android Swap between multiples objects with mouse click

T

tacha

Guest
Hi!
I'm making an Android game and there's a room in which I will have N numbers of plants that can be swapped within each others. For example, I have 10, I select one and I can swap the position with one of the remaining 9; the idea is to be able to select any of the 10 plants and swap them with any of the remaining plants, not just one that can be swapped with the others.

Also, every plant is a different object with their own create, step, alarm, etc. I need to swap different objects that have different functionalities.

I have been trying this with a controller:

/// Create Event
firstSelected = noone;
secondSelected = noone;

/// Global Left Pressed Event
if (firstSelected == noone)
{
firstSelected = instance_position(mouse_x,mouse_y, all);
}
else
{
secondSelected = instance_position(mouse_x,mouse_y, all);
firstSelectedX = firstSelected.x;
firstSelectedY = firstSelected.y;
firstSelected.x = secondSelected.x;
firstSelected.y = secondSelected.y;
secondSelected.x = firstSelectedX;
secondSelected.y = firstSelectedY;
firstSelected = noone;
}

It works, but I can swap the position with every object in the room and I just want to swap between the plants objects. Any idea how to solve this? Thanks in advance!
 
Last edited by a moderator:

Alice

Darts addict
Forum Staff
Moderator
Well, if the plant object is called "obj_plant", then you need to replace "all" with "obj_plant". The whole code for Global Left Pressed would go like:
Code:
/// Global Left Pressed Event
if (firstSelected == noone)
{
    firstSelected = instance_position(mouse_x,mouse_y, obj_plant);    // note the "obj_plant" here
}
else
{
    secondSelected = instance_position(mouse_x,mouse_y, obj_plant);    // "obj_plant" goes here again
    if (secondSelected != noone)    // making sure you don't try to swap the object with non-existent object
    {
        var firstSelectedX = firstSelected.x;    // using "var" keyword here, so that firstSelectedX/Y
        var firstSelectedY = firstSelected.y;    // variables are freed from memory after the event is handled
        firstSelected.x = secondSelected.x;
        firstSelected.y = secondSelected.y;
        secondSelected.x = firstSelectedX;
        secondSelected.y = firstSelectedY;
    }
    firstSelected = noone;    // the first object is deselected, whether you clicked on a valid swappable object or not
}
 
T

tacha

Guest
Well, if the plant object is called "obj_plant", then you need to replace "all" with "obj_plant". The whole code for Global Left Pressed would go like:
Code:
/// Global Left Pressed Event
if (firstSelected == noone)
{
    firstSelected = instance_position(mouse_x,mouse_y, obj_plant);    // note the "obj_plant" here
}
else
{
    secondSelected = instance_position(mouse_x,mouse_y, obj_plant);    // "obj_plant" goes here again
    if (secondSelected != noone)    // making sure you don't try to swap the object with non-existent object
    {
        firstSelectedX = firstSelected.x;
        firstSelectedY = firstSelected.y;
        firstSelected.x = secondSelected.x;
        firstSelected.y = secondSelected.y;
        secondSelected.x = firstSelectedX;
        secondSelected.y = firstSelectedY;
    }
    firstSelected = noone;    // the first object is deselected, whether you clicked on a valid swappable object or not
}
I'm sorry, I didn't specified that the idea is to be able to select any of the 10 plants and swap them with any of the remaining plants, not just one that can be swapped with the others.
 

Alice

Darts addict
Forum Staff
Moderator
But if all plants are instances of obj_plant, then you can click on any obj_plant to get it selected, and then any obj_plant to swap if the earlier selected one.

Do you know the distinction between the objects and the instances, and that the same object pattern (as defined in Objects resource tree) can be used multiple times to produce different, yet in some regards similar instances?
 
T

tacha

Guest
But if all plants are instances of obj_plant, then you can click on any obj_plant to get it selected, and then any obj_plant to swap if the earlier selected one.

Do you know the distinction between the objects and the instances, and that the same object pattern (as defined in Objects resource tree) can be used multiple times to produce different, yet in some regards similar instances?
Yes, the problem is that every plant is a different object with their own create, step, alarm, etc. I know what you mean, but I need to swap different objects that have different functionalities.
 
T

tacha

Guest
I have been learning about parenting (I'm not a programmer, but I have learned the basics). Can you please explain how can I implement this with parenting? I have the plants parented to a controller, calling the inherited events and having their own events, but I can't wrap it to select just the plants and nothing more.
 

jackquake

Member
I'm not I'm not at a computer right now or I could show you some code. But basically you have a parent object for your plants which would not be the controller object. Each one of your plant objects would then be a child of this parent plant object. Then you could replace your code above instead of all simply use the parent plant object instead in the instance_position code above.

Alternatively if all of the plant objects are very similar then as Alice suggested spawn them all from a single plant object and change their Sprite accordingly. Then you could reference it in your code above as object plant as well. Instead of using all as Alice suggested.
 
T

tacha

Guest
Thanks!
I tried what you suggested and it works, but I get an error if I select one object different from the one I have to select. I did this:

Created obj_macetaswap_planta (plant controller) in which the swap code is there.
Created obj_macetaS_planta which will be always in room (the other plants can be eliminated) and put this object as the one to select (instance_position(mouse_x,mouse_y, obj_macetaS_planta)).
Parented all the other plants to obj_macetaS_planta

Now if I click between the plants they swap and everything's happy, but if I click another object I get this error:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Mouse Event for Glob Left Pressed
for object obj_macetaswap_planta:

Unable to find any instance for object index '-4' name '<undefined>'
at gml_Script_scr_swap_macetas (line 13) - firstSelected.x = secondSelected.x; // script where the code is
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_swap_macetas (line 13)
called from - gml_Object_obj_macetaswap_planta_GlobalLeftButtonPressed_1 (line 6) - scr_swap_macetas(obj_macetaS_planta, obj_macetaS_planta);
 
T

tacha

Guest
Ok, I figured it out, for anyone who wants to know.
I have that code in a script to easily call it from the different objects, and in order to avoid the FATAL ERROR I added an if statement in the else statement:

a = argument0; // plant 1
b = argument1; // plant 2 (in this case the same)

if(firstSelected == noone)
{
firstSelected = instance_position(mouse_x,mouse_y, a);
}
else if position_meeting(mouse_x, mouse_y, b)
{
secondSelected = instance_position(mouse_x,mouse_y, b);
firstSelectedX = firstSelected.x;
firstSelectedY = firstSelected.y;
firstSelected.x = secondSelected.x;
firstSelected.y = secondSelected.y;
secondSelected.x = firstSelectedX;
secondSelected.y = firstSelectedY;
firstSelected = noone;
}

Thanks for the responses, I really appreciate them and the parenting stuff helped!
 
Top