[HELP] I want to replace an object with another when clicked at.

P

ProductivInc

Guest
I made a script: when I click at the selected object it replaces it with another object. However, It doesnt work and I am a complete beginner at GML. Can you please explain what I'm doing wrong and show me exactly?

Script:

if mouse_check_button(mb_left)
{
if position_meeting(mouse_x, mouse_y, oTower1) instance_create_depth(mouse_x,mouse_y,-1,oTowerUPGRADE);
}


OTower1 is the object I want to click at so it can replace it with OTowerUpgrade.

This is a tower defense game where I'm trying to make a simple upgrade system...
 

CloseRange

Member
does the current script not do anything?
or does it create oTowerUPGRADE and just not delete the oTower?

I assume it's the ladder.
Code:
if mouse_check_button_pressed(mb_left) {
     var o = instance_position(mouse_x, mouse_y, oTower1);
     if(o) {
          instance_create_depth(o.x, o.y, -1, oTowerUPGRADE); // you used mouse_x and mouse_y for the new position but i think you wanted this
          instance_destroy(o);
     }
}
 
P

ProductivInc

Guest
does the current script not do anything?
or does it create oTowerUPGRADE and just not delete the oTower?

I assume it's the ladder.
Code:
if mouse_check_button_pressed(mb_left) {
     var o = instance_position(mouse_x, mouse_y, oTower1);
     if(o) {
          instance_create_depth(o.x, o.y, -1, oTowerUPGRADE); // you used mouse_x and mouse_y for the new position but i think you wanted this
          instance_destroy(o);
     }
}
It doesnt do anything.
 

CloseRange

Member
yeah. A script will never run unless you call it....
if the script was called scr_change
then run it like this:
Code:
scr_change();
 

FrostyCat

Redemption Seeker
Is it normal these days for tutorials to not teach the importance of events? Code needs to have the right content AND be in the right place to work. Like other input-related code involving functions starting with keyboard_check and mouse_button_check, the script should be called from the Step event.
 
Top