function that replaces self? can scripts run in the create event?

T

Tom_SavePointsGames

Guest
Hello,

I am making a game with a lot of enemies that use similar variables.

examples:
enemy_health = 5;
enemy_damage = 2;
enemy_spd = 3;

I am familiar with inheritence and thought it would be easier to maintain one script with a list of all the enemies and what there initial variables should be instead of editing each individual create event

so in the parent create event for obj_enemy I have the following

scr_build_enemy( object_get_name( self ) ); //the spaces are to make it easier to read they are not in the code. (side note I have also tried self.id and id instead of self.

Then i have a script with the following:

var enemy = argument0;

if (enemy == obj_bug)
{

enemy_health = 5;
enemy_damage = 2;
enemy_spd = 3;

}

if (enemy == obj_flier)
{

enemy_health = 10;
enemy_damage = 3;
enemy_spd = 5;

}

etc.

it's not working. is there a way to get the name of the object for that instance and pass it instead a script as an argument and then set that to a variable that is used to check and set enemy variables?

Thanks
-Tom
 

alexde5th

Member
What you could do is use enemy = object_index and place it in the first line of code. That way it will get the name of the instance calling for it, and you can use it for checking which instance it is.
 
T

Tom_SavePointsGames

Guest
wow thanks that worked

the help files clearly state that object_index doesn't return the name it only returns the number so I never tried it... lol sorry
 
M

maartenvt

Guest
I think you should use object_index instead of object_get_name(self) as the argument of your script.

edit: mb someone already helped you out.
 
Last edited by a moderator:
M

maartenvt

Guest
the help files clearly state that object_index doesn't return the name it only returns the number
Yes, but so does every other reference to a resource. For example if you use obj_enemy in your code, all it does is return the id of the object which is an integer. The same for sprites, fonts etc.
So your if statement is really just comparing two numbers (integers):
if (enemy == obj_bug) // <--enemy and obj_bug are both numbers
 

Weird Dragon

Wizard
GMC Elder
wow thanks that worked

the help files clearly state that object_index doesn't return the name it only returns the number so I never tried it... lol sorry
An additional note or elaboration regarding the object_get_name function. That function returns a string not a resource.
obj_bug is a resource and it is actually a number even it is spelled out with letters. Thus obj_bug returns a number just like object_index.
"obj_bug" is a string and it is the actual letters including the underscore.

Thus your code
var enemy = argument0;
if (enemy == obj_bug) //etc.
will only work if argument0 is a resource number and never if it is a string.

***
A note regarding your second question in your headline:
"can scripts run in the create event?"
Absolutely. You can run a script in any event.
 
Last edited:
T

Tom_SavePointsGames

Guest
wow thank you guys for clarifying. That knowledge will be very helpful for when I build code for other things too.
 
Top