GML (Game Maker 8)Damage and knockback enemies when an object they're standing on is destroyed

Status
Not open for further replies.

Build Man

Member
Greetings again! Build Man has resumed opreationing! I have nearly finished Zone 1-3, but there is still one thing left to do!

The thing is, one of the enemies is standing on a breakable, I want it so when an object breaks, enemy takes damage and knockback, as if it was hit by an attack.

Normally, I would of come up with something of my own. However, I have a theory that here, in GameMaker Community forums, are people that can handle it better than me, so here I am. Providing latest build:


Skip straight to Zone1_3. It is the only instance where enemy standing on the objects can be found(Hint: It is Elder Rattleshell on crates). The rest is up to you.
 

Build Man

Member
I have made an attempt. I wrote a code for a wooden box(the only breakable object as of currently), so that when it is destroyed by an attack, it will also demege enemies standing on it and apply force to them upwards. Here is the code:
GML:
if place_meeting(x,y-1,(Fungus || Rattleshell || Saurian))
{
with other{event_perform(ev_other,ev_user1)}
}
Then, I have made a proper User Event 1 for every enemy, that gets triggered by said boxes being destroyed. An example of such code from Elder Rattleshell(of which Rattleshell is parent entity):
GML:
sprite_index = Enemy_RattleElder_Damaged
vspeed = -9
hspeed = 0
enemy_health -= 20
However, the code does not work for some reason. So here I am, asking for a solution, or a better code. For that purpose, I will share the latest build, so that you could try it out yourselves. This will help with further research.


Of note, it was even tried to refer to a specific enemy, and it still did not work. Reminder, Elder Rattleshell is currently the only enemy featured standing on crates.
GML:
if place_meeting(x,y-1,Elder_Rattleshell)
{
with other{event_perform(ev_other,ev_user1)}
}
 

FrostyCat

Redemption Seeker
Your basic GML needs work. I'll leave it to you to decide why I'm asking you to read these two articles.

How NOT to use && and || (#2)
WRONG:
Code:
place_meeting(x, y+1, obj_a || obj_b)
RIGHT:
Code:
place_meeting(x, y+1, obj_a) || place_meeting(x, y+1, obj_b)
Code:
place_meeting(x, y+1, obj_parent)
Note: obj_parent is the parent of obj_a and obj_b.
How to Avoid Misusing && and ||
If you don't want to use && and || where they don't belong and not know what hit you, follow these 2 guidelines:
  • DO NOT translate English sentences word-for-word into GML symbols.
  • If either side of an && or || operator is not meant to be treated as a Boolean value (true/false), the code is wrong.
What's the Difference: Objects and Instances (Finding the right instance)
Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ but don't end in _list all return instance IDs. Save that instance ID into a variable, then use that as the subject to work with. DO remember to check that it is not noone before acting on it. DO NOT refer to this instance as other.
GML:
var inst_enemy = instance_place(x, y, obj_enemy);
if (inst_enemy != noone) {
    inst_enemy.hp -= 10;
}
In GM8 legacy syntax:
GML:
var inst_shell;
inst_shell = instance_place(x, y-1, Elder_Rattleshell);
if (inst_shell != noone) {
    with (inst_shell) event_user(1);
}
 

Build Man

Member
I have been reading through the message, but I still have some questions.

GML:
var inst_shell;
inst_shell = instance_place(x, y-1, Elder_Rattleshell);
if (inst_shell != noone) {
    with (inst_shell) event_user(1);
}
Where does this go?
 

Build Man

Member
Where do you want it to go? At what point in your game do you want the thing to happen?
As soon as the object they're currently walking on is destroyed. Assuming you even know what thing we are talking here about. If not, you can always refer to thread name.
 
Never handled instances before.
Yes you have. Everything in your game is an instance. If you've played you're game, you've been playing with instances. Objects only really exist in the editor as a "blueprint" for instances that get created in-game. If I'm reading the thread right, you should be putting inst_shell code inside your destroy event. Do you understand what is happening?

The codeblock:
Code:
var inst_shell;
inst_shell = instance_place(x, y-1, Elder_Rattleshell);
if (inst_shell != noone) {
   with (inst_shell) event_user(1);
}
Is checking to see if there is an enemy above (Elder_Rattleshell in this case) and if there is, it's making that instance run User Event 1. Assuming User Event 1 contains the code for the knockback and damage, then where would you want that code to run? Well, since you are checking above for enemies, you'd assume that you want that code to run in the blocks, as they are the thing that the enemies need to be on top of for them to get knocked back. And when do you want this code to run? When a block is destroyed. So put the code in the Destroy Event for the block objects.

As a side note, you don't need to check for if (inst_shell != noone) as the with (inst_shell) implicitly checks to see if inst_shell exists and won't run (or crash) if it doesn't.
 

Build Man

Member
I must have worded it wrong. I meant "var inst_whatever" thing. Anyways, I have tried the solution. Inserted all of the code proposed. For some reason it worked.

Maybe because inst_shell secretly refers to a crate, or there is some other reason. Does not matter. If it works, that is all that matters.

UPDATE: Maybe it works too well, because Rattleshells and Fungi are faltered by the destruction of solid surface underneath them even if they are standing just by a single pixel on a destructible surface. I do not want that. I have experience in a coding, but not much. After all, my primary function is to build levels.

By the way, I was creating a separate room for testing purpose, and started testing destruction interaction code there.
 
Last edited:

Build Man

Member
Figured I would deploy an up to date prototype, with new test zone designed to test enemy reaction to solid floor destruction. Also, I have updated the original post with latest prototype and an up to datew information.


The current problem is that Rattleshells and Fungi getting faltered by the tile destruction even if they just stand on it by a few pixels. I want a code that limits this behaviour based on the proximity of an enemy to the tile it is standing on. I did not have figured it on my own yet.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Please don't share files and expect users to look through them and solve your issues. Nobody has the time for that. especially not when it's a legacy GM8 project... SO to help yourself get help, please place the relevant code in the posts and CLEARLY explain EXACTLY what is happening right now and EXACTLY what you want to happen. We are not here to fix your projects or give you code. We are here to help you learn so you can make your own code from what you have learned. ;)

Oh, and please do NOT edit the main topic after getting replied. It's ONE TOPIC PER PROBLEM, otherwise it becomes impossible for future users to come to your topic and learn anything, as it'll make no sense. I've reverted your edits and I'm going to close this topic so you may make a new one about this new issue. ;)
 
Status
Not open for further replies.
Top