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

Per instance code [SOLVED]

F

finn_bueno_

Guest
Hello!

For a small game I'm making I need buttons, and I got everything up and running, most works fine. But now I've come to what the button should do when it's pressed, and each button should do something different. The idea of having a different object for each button seems extremely messy to me (even with parenting). Is there a way to define code for each instance and call it when I want to?

Thanks in advance.

- Finn
 

Alexx

Member
Yes, there's a few ways of doing this, two options of many are below.
One is to send a different value to each button when creating it, and then use that value to determine what that button does.
For example create from another object:
button=instance_create(x,y,obj_button);
button.my_id=1;
button=instance_create(x,y,obj_button);
button.my_id=2;

Then perform checks on my_id to do what you want.

You could use the room editor and make creation code for each.

The best method you use really depends on how many buttons, and what each will do.

Some more information on your buttons and what you'll do with them will help.
 
Last edited:

jo-thijs

Member
If you've only got 16 different codes per object and you're not using the user defined events for anything else, you can use those.
Otherwise, you can create a script for every separate code (or have a large switch structure in the object itself deciding which code to execute, but that might not be the nicest looking).

You can then decide for every instance indiviually what code to execute by:
- assigning the identifier for the code (number from 0 to 15 for user defined, script name for scripts and own value for switches) to an instance variable
in the instance creation code (right click on the instance in the room to edit) for instances that are created in the room editor
- assigning the identifier to an instance variable through a "with" structure if the instance is created through code:
Code:
with instance_create(..., ..., ...)
    mycode = ...;
You can execute scripts stored in variables using the function script_execute.

PS: Alexx responded while I was writing this, but I feel like I give some alternatives worth considering.
 
F

finn_bueno_

Guest
Yes, there's a few ways of doing this, two options of many are below.
One is to send a different value to each button when creating it, and then use that value to determine what that button does.
For example create from another object:
button=instance_create(x,y,obj_button);
button.my_id=1;
button=instance_create(x,y,obj_button);
button.my_id=2;

Then perform checks on my_id to do what you want.

You could use the room editor and make creation code for each.

The best method you use really depends on how many buttons, and what each will do.

Some more information on your buttons and what you'll do with them will help.
Thanks! I don't think I'll use this method, but I'll definitely keep it in mind for when it suits my needs better. :)

If you've only got 16 different codes per object and you're not using the user defined events for anything else, you can use those.
Otherwise, you can create a script for every separate code (or have a large switch structure in the object itself deciding which code to execute, but that might not be the nicest looking).

You can then decide for every instance indiviually what code to execute by:
- assigning the identifier for the code (number from 0 to 15 for user defined, script name for scripts and own value for switches) to an instance variable
in the instance creation code (right click on the instance in the room to edit) for instances that are created in the room editor
- assigning the identifier to an instance variable through a "with" structure if the instance is created through code:
Code:
with instance_create(..., ..., ...)
    mycode = ...;
You can execute scripts stored in variables using the function script_execute.

PS: Alexx responded while I was writing this, but I feel like I give some alternatives worth considering.
Thank you too! I'll be using the last method you mentions, I had no idea that's possible, and it's exactly what I'm looking for.

Thanks for the help, solved :)
 
Top