Legacy GM Opinions...

Gillen82

Member
Hello, GMC

I'm not looking code from anyone, but instead, something more valuable......your wise words and opinions.

Within my TDS I am looking a specific enemy to drop a specific item (keycard). I was thinking of making a duplicate object of the enemy and placing that in the room so I could reference it easily in the code, but, are there any other more efficient ways of going about this to save on the resources being created, as this will be happening throughout the game?

Thanks in advance!!
 

jo-thijs

Member
Hello, GMC

I'm not looking code from anyone, but instead, something more valuable......your wise words and opinions.

Within my TDS I am looking a specific enemy to drop a specific item (keycard). I was thinking of making a duplicate object of the enemy and placing that in the room so I could reference it easily in the code, but, are there any other more efficient ways of going about this to save on the resources being created, as this will be happening throughout the game?

Thanks in advance!!
Personally, I would either use the instance creation code (not to be confused with the object create event).
In the room editor, you can right click on an instance and choose "Creation Code" to insert code that gets executed for that instance only.
You can set a variable like holding to noone in te create event and to obj_keycard in the creation code.
Alternatively, I would just put an instance of obj_keycard on top of the enemy object in the room editor
and have the keycard check for an instance of type enemy colliding with it in its create event
and have it deactivate itself and set some variables in the enemy object it was colliding with if so.

If you need to reference the enemies holding keycards for some cutscene or UI map features or something like that,
you could do it like this:
Code:
// Single enemy instance holding keycard
var target = noone;
with obj_enemy
    if holding == obj_keycard {
        target = id;
        break;
    }
If you want to create new objects for these instances anyway,
I would seriously recommend having those objects be children of their non-keycard-holding versions
and extensively use event_inherited, so that changes to the enemy object does not always need to be applied in 2 objects.
 

CloseRange

Member
Well it really depends how you have your code setup. Is there only one enemy object that changes sprite depending on what kind of enemy it is? if so then in the objects destroy use 'if image_index == ' with the index of that enemy
Or is there multiple enemy objects already that all parent one obj_enemy? then even better in the parent object use 'if object_index == ' and then the name of the specific enemy (like obj_enemy_spider or something).

I'm not sure I read the question perfectly so sorry if I'm spouting random gibberish.
 

Gillen82

Member
Well it really depends how you have your code setup. Is there only one enemy object that changes sprite depending on what kind of enemy it is? if so then in the objects destroy use 'if image_index == ' with the index of that enemy
Or is there multiple enemy objects already that all parent one obj_enemy? then even better in the parent object use 'if object_index == ' and then the name of the specific enemy (like obj_enemy_spider or something).

I'm not sure I read the question perfectly so sorry if I'm spouting random gibberish.
Sorry if I didn't explain right...The first level of the game will consist of one type of enemy (obj_humans) only. As I make my way through the level, I want a specific human to drop a keycard when he is killed, which will allow the player to progress to other parts of the area. I was wondering if creating a duplicate object would be the best option for archiving this, or as @jo-thijs suggested with creation code? I'm open to all suggestions.

Thanks for taking the time to read and respond. Much appreciated!!
 

jo-thijs

Member
Sorry if I didn't explain right...The first level of the game will consist of one type of enemy (obj_humans) only. As I make my way through the level, I want a specific human to drop a keycard when he is killed, which will allow the player to progress to other parts of the area. I was wondering if creating a duplicate object would be the best option for archiving this, or as @jo-thijs suggested with creation code? I'm open to all suggestions.

Thanks for taking the time to read and respond. Much appreciated!!
Actually, it might be even better to not have the enemies keep track of what they're holding, but let the keycard keep track of which instance is holding it.
You can make the keycard invisible and let it keep track of which instance is holding it
and as long as an instance is holding it (it is not noone), you check if the instance still exists.
If so, you teleport the keycard to the instance's position.
Otherwise you make the keycard visible and set heldby to noone.
When heldby is noone, you make the keycard interact with the player.

This way you don't need additional code in enemy objects to deal with carrying objects.
The biggest factor in whether this approach'd be better than my previous suggestion would be how you use polymorphism (child-parent relations).
 

Gillen82

Member
Actually, it might be even better to not have the enemies keep track of what they're holding, but let the keycard keep track of which instance is holding it.
You can make the keycard invisible and let it keep track of which instance is holding it
and as long as an instance is holding it (it is not noone), you check if the instance still exists.
If so, you teleport the keycard to the instance's position.
Otherwise you make the keycard visible and set heldby to noone.
When heldby is noone, you make the keycard interact with the player.

This way you don't need additional code in enemy objects to deal with carrying objects.
The biggest factor in whether this approach'd be better than my previous suggestion would be how you use polymorphism (child-parent relations).
I have just used your first suggestion of using the creation code and it works perfectly. I'll implement your second suggestion, but the additional code won't be an issue. I was thinking a quick script that could be used across different types of enemies would help with the additional code.

Either way, you've come up with a better suggestion than I originally had, so thank you!!
 

zendraw

Member
i usually have an 'events' script which i trigger to do specific things, like for debuggin i have a case to destroy all enemies. in your case simply make a case to create the keycard. you can trigger the script from anywhere and anything + you can access it like a console by making teh cases strings case 'it_keycard': instance create keycard; break;
 
Top