Multiple instances of objects as switches
#1
Guest_deepseaengine_*
Posted 09 February 2012 - 12:23 PM
I'm trying to add multiple switches to a map which, once all have been activated, will open the room exit. Simple enough, but object placement from the drag-n-drop room creator means each instance of the switch is identical - one activation flips all of the switches. I imagine I could add multiple objects (switch ID1, switch ID2, etc) but was hoping there was a simpler way?
This dynamic will be repeated for each room - would I need to assign IDs and 'create' the switch objects at specific co-ordinates for each switch in each room, can I simply auto-assign an ID to each switch placed by drag-n-dropping and code the game to recognise 'switch ID x has been activated'?
Many thanks in advance.
#2
Posted 09 February 2012 - 01:06 PM
In the event where you switch the instance (a key press event?), do this
var otherSwitch = collision_rectangle(x,y,bbox_right, bbox_bottom, obj_switch, true, true); if(otherSwitch) otherSwitch.activated = !otherSwitch.activated;collision_rectangle returns the collided instance's ID, and an ID can be used just the same way as an object's index but only affecting the specific instance rather than all of them.
I believe if its in a collision event, you can just use the keyword other for the same effect, but won't be toggle-able without checking for a keypress:
other.activated = true; /*OR DO THIS*/ if(keyboard_check_pressed(vk_space)) other.activated = !other.activated;
#3
Posted 09 February 2012 - 01:09 PM
#4
Guest_deepseaengine_*
Posted 09 February 2012 - 01:27 PM
Mr Game, thank you, but I'll do some trial-and-error first and post my results for dissection. I find problems help me learn better.
Edited by deepseaengine, 09 February 2012 - 01:28 PM.
#5
Posted 09 February 2012 - 06:26 PM
Every time a switch is flipped, add +1 to a variable in the DOOR object (the one that opens when all the switches are flipped). Call it num_switches_flipped or something.
In the door object, use the "Test Instance Count" on the switch object (to see how many switches are in the room) and compare it to num_switches_flipped. If they are equal, the door opens.
This way is a little simpler and a one-size-fits-all for every room.
#6
Posted 09 February 2012 - 09:35 PM
You can check the same thing like this with code
var i;
with(obj_switch) {
if(activated)
other.i += 1;
}
if(i == instance_count(obj_switch)) {
//Open door
}
#7
Posted 09 February 2012 - 09:51 PM
define_Switch():
{
if(!variable_global_exists("Switch"))
{
globalvar Switch;
Switch = object_add();
object_event_add(Switch,ev_create,0,"create_Switch();");
}
}
create_Switch():
{
state = 0;
action_script = -1; //id of script to execute when switch is toggled
}
toggle_Switch():
{
if(action_script != -1) script_execute(action_script);
}
Then create several instances and set their action_script = room0_OnToggle; *NOTE: do not put () at the end - you aren't calling the script here but giving a ref to it. Then write the room0_OnToggle script...
room0_OnToggle():
{
if(switch_0.state == 0) exit;
if(switch_1.state == 0) exit;
...
//if it reaches this point, all switches have been flipped.
open_door(); //replace with relevant code
}
This is a more generic solution, so you should be able to apply it to many different situations with ease.
#8
Posted 09 February 2012 - 10:03 PM
@Caracol - It is no more one-size-fits all than any other method...
Yeah, I was assuming he wanted a drag-and-drop method, my comment was meant to say "it's still one-size-fits-all even though it doesn't use GML".
Anyway, if you want to use GML, it'll be slicker and better in the long run. More power to you!
#9
Guest_deepseaengine_*
Posted 09 February 2012 - 10:27 PM
Caracol - thank you for your non-GML solution. I'll try that, but I'm hoping to learn GML through this project, so I'll probably go for the coding instead.
#10
Guest_deepseaengine_*
Posted 11 February 2012 - 12:43 PM
In obj_CHAR:
IF obj_CHAR collides with obj_SWITCH
swACTIVATE=1;
In obj_SWITCH:
On keypress 'F'
if(obj_CHAR.swACTIVATE=1)
{
swON=1;
obj_SWITCH.image_index=1;
obj_SWITCH.image_speed=0;
}I just want the collided instance of obj_SWITCH to change its variable 'swON' from 0 to 1 and for the sprite to change from image 0 to image 1. Both of these things do happen, but to all instances of the obj_SWITCH rather than the one obj_CHAR is colliding with. I don't want to reference specific instance IDs - I'd rather just do a 'the-currently-collided-with-instance' reference. I bet it's a really obvious solution... any ideas?
#11
Posted 11 February 2012 - 01:49 PM
if(obj_CHAR.swACTIVATE && place_meeting(x,y,obj_CHAR)) {
swON=1;
obj_SWITCH.image_index=1;
obj_SWITCH.image_speed=0;
}
#12
Guest_deepseaengine_*
Posted 11 February 2012 - 02:37 PM
if(obj_CHAR.swACTIVATE && place_meeting(x,y,obj_CHAR)) {
swON=1;
obj_SWITCH.image_index=1;
obj_SWITCH.image_speed=0;
}I'd tried an instance_position variant of that which didn't work. Unfortunately, this doesn't either. Any other suggestions?
#13
Posted 11 February 2012 - 04:33 PM
if(obj_CHAR.swACTIVATE && place_meeting(x,y,obj_CHAR)) {
swON=1;
image_index=1;
image_speed=0;
}You were setting it for all of the switches... that's what was wrongPutting obj_name.var_name will change var_name's value in all instances obj_name
Edited by thegame, 11 February 2012 - 04:34 PM.
#14
Guest_deepseaengine_*
Posted 11 February 2012 - 04:46 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











