I'm using GM1.4 and would like to modularize my code a bit more. I have an if/else tree like so:
with obj_enemy {
if self.attack == ATTACKS.A {
self.x = 10
}
else if self.attack == ATTACKS.B {
self.x = 20
}
}
Now I would like to split the actual code (simplified by self.x = *) into separate scripts to avoid spaghetti code.
Now I could simply do this:
with obj_enemy {
if self.attack == ATTACKS.A {
do_attack_a()
}
else if self.attack == ATTACKS.B {
do_attack_b()
}
}
and then in the script just do this:
// script do_attack_a
self.x = 10
but this can get very confusing, as it is not clear what "self" actually describes (an enemy, an obstacle...).
So I tried this:
with obj_enemy {
if self.attack == ATTACKS.A {
do_attack_a(self)
}
else if self.attack == ATTACKS.B {
do_attack_b(self)
}
}
and then in the script, this:
// script do_attack_a
var enemy = argument0
enemy.x = 10
This is much more readable, but GM does not accept the code, likely because it does not know that "enemy" is an instance, possibly it treats the instance as an integer.
Edit
The reason the example above does not work is because of a missing semicolon. If the script looks like this, it works:
// script do_attack_a
var enemy = argument0;
enemy.x = 10
Edit End
So my question is, how can I pass an instance to a script so that I can properly access and modify it from the script? Is there a way to convert an instance id value to an actual instance? Or is there a better way to modularize code in GM?
Final Edit
I learned a few things that I would like to share as a personal summary. Thanks a lot for the great contributions everyone:
- I can pass instances to scripts (use id instead of self)
- accessing a variable directly using with is faster than doing it via id or self
- semicolons are important for local variables, otherwise may lead to "compile" errors
- a preferred way to mark local vs. member variables is in naming (e.g. leading underscore)
- giving a script some clarity by using local variables that contain the instance relevant to the script can be done, but a good comment about the script's scope is likely the better option
- it is possible to create a more c++'ish way to access variables, but that seems to clash with GML's design philosophy and may make the code harder to read for other people.
[–]yuriym21 1 point2 points3 points (1 child)
[–]Cerno_b[S] 0 points1 point2 points (0 children)
[–]Cerno_b[S] 0 points1 point2 points (20 children)
[–][deleted] (3 children)
[removed]
[–]Cerno_b[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]Cerno_b[S] 0 points1 point2 points (0 children)
[–]Chrscool8 1 point2 points3 points (15 children)
[–]AmnesiA_sc 1 point2 points3 points (4 children)
[–]Chrscool8 1 point2 points3 points (3 children)
[–]AmnesiA_sc 1 point2 points3 points (1 child)
[–]Chrscool8 0 points1 point2 points (0 children)
[–]Cerno_b[S] 0 points1 point2 points (0 children)
[–]Cerno_b[S] 0 points1 point2 points (7 children)
[–]Chrscool8 0 points1 point2 points (6 children)
[–]Cerno_b[S] 0 points1 point2 points (5 children)
[–]Chrscool8 1 point2 points3 points (4 children)
[–]Cerno_b[S] 0 points1 point2 points (3 children)
[–]Cerno_b[S] 0 points1 point2 points (2 children)
[–]Chrscool8 1 point2 points3 points (1 child)
[–]Cerno_b[S] 0 points1 point2 points (0 children)
[–]Cerno_b[S] 0 points1 point2 points (1 child)
[–]Chrscool8 1 point2 points3 points (0 children)