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

Object consisting of two others - avoiding double code

Z

Zelda Hyrule

Guest
Hello everyone,

So my question is about this:

Let's say we've got 3 objects, which represent enemies:

1)obj_run (a guy that runs around),
2)obj_shoot (a guy just sitting at his position and occasionally shooting),
3)obj_run_shoot (a combo of the first two, a guy that runs around and occasionally shoots).

They have got the following Create events:

obj_run:
movespeed = 5;
dir = -1;

obj_shoot:
shotRate = 7;
alarm[0] = shotRate;

obj_run_shoot:
movespeed = 5;
dir = -1;
shotRate = 7;
alarm[0] = shotRate

(The third is just the other two put together)
(Ignore the functionality of the variables, this is just an example)

So what would be the most elegant way to avoid writing double code?
Would it be a script (one initializing the variables of obj_run and one for those of obj_shoot), event_perform_object (called in the create of obj_run_shoot), or some sort of parenting?

Any help is greatly appreciated.
Thanks in advance.
 
Last edited by a moderator:
J

JimmyBG

Guest
I'd use scripts for this, with obj_run_shoot calling both of the scripts used for obj_shoot and obj_run.

For example, create event scripts (these contain all the variables you put above):
run_create()
shoot_create()

obj_run would just call the 'run_create' script and obj_shoot would just call the 'shoot_create' script, but obj_run_shoot would call both.

same for step events, draw events etc.
 
Z

Zelda Hyrule

Guest
I'd use scripts for this, with obj_run_shoot calling both of the scripts used for obj_shoot and obj_run.

For example, create event scripts (these contain all the variables you put above):
run_create()
shoot_create()

obj_run would just call the 'run_create' script and obj_shoot would just call the 'shoot_create' script, but obj_run_shoot would call both.

same for step events, draw events etc.
Thanks a lot for the quick reply. You probably noticed I wrote the word function above, which was a mistake, of course I meant script instead 'cause in GM scirpts are the selfmade functions.
I'm just used to functions from C++. Anyway, thank you.
 
Z

Zelda Hyrule

Guest
no worries, let me know if you need anymore help :)
Hello, I'd like to ask another question,

I use the following code pretty often in my enemies:

alarm[n] = irandom_range(1, 3);
(where n is mostly 0 or 1)

Would it be good to put the code in a seperate script like this?:

scr_alarm_set(alarm_num);
alarm[argument0] = irandom_range(1, 3);

On the other hand I use this code about two times:
shoots = irandom_range(1, 3);

Instead of a script for the alarms should I rather have a script returning irandom_range(1, 3)
so that it can be used on both?

Like:
scr_set();
var temp;
temp = irandom_range(1, 3);
return temp;

Or is there any better way to solve this?

Thank you very much.
(Excuse me if this is written unclear or somehow incomprehensible)
 
J

JimmyBG

Guest
Use the scr_alarm_set script you've written above, that's probably you best option for the way you're using it.
Its highly likely that at some point you'll want to use a different range for 'shoot' than the alarms, so its best to keep them separate to avoid changing them all again later.
Perhaps have a second script for shoot? e.g.
Code:
///scr_shoot_set()
shoot = irandom(1,3);
 
Top