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

GML [Solved] Charged Attack With Sword

I

Intelligents

Guest
Hello, I'm new to programming with Game Maker's GML. How would I go about creating a charged attack that can be released at any time until fully charged, where it will automatically attack, and deal damage to an enemy based on how long it is charged? I don't have much experience with this and don't have a sample of code to work off of yet.

If I am overstretching and trying to do something I'm not ready for, please let me know. I'd have to work on simpler game mechanics.
 
T

Tyler Logsdon

Guest
Nah. This is very basic(not to say you don't have skill, I have problems with very basic gml things too and I've been coding for 6 years)

What you could do(this is off the top of my head there's probably a way better way to do it) is in the step event, check if charge button is oressed, and if it is, add +1(you will probably want something a lot lower) and in the keyboard release event, do damage equal to the charge and then set it back to 0.
 
S

Silver_Mantis

Guest
Hello, I'm new to programming with Game Maker's GML. How would I go about creating a charged attack that can be released at any time until fully charged, where it will automatically attack, and deal damage to an enemy based on how long it is charged? I don't have much experience with this and don't have a sample of code to work off of yet.

If I am overstretching and trying to do something I'm not ready for, please let me know. I'd have to work on simpler game mechanics.
The real question is, do you have an attacking system set up at all?
Anything is possible if you put your mind to it, but I have no clue what you have built already haha.

There is a really good tutorial I found that helps out a lot. You might find some use from it, because it is the very basics of melee combat.
 

Zerb Games

Member
Nah. This is very basic(not to say you don't have skill, I have problems with very basic gml things too and I've been coding for 6 years)

What you could do(this is off the top of my head there's probably a way better way to do it) is in the step event, check if charge button is oressed, and if it is, add +1(you will probably want something a lot lower) and in the keyboard release event, do damage equal to the charge and then set it back to 0.
Are you sure 6 years? Nvm, you profile pic explains everything.
 
Z

zendraw

Guest
what tyler suggested is the way to go
if (key_charge)
{
charge++
if (charge>=max_charge) {execute attack; charge=0};
} else
{
if (charge) {charge=0};//simply reset charge or attack with less charge if youve accumulated any
}
 

Yal

šŸ§ *penguin noises*
GMC Elder
what tyler suggested is the way to go
if (key_charge)
{
charge++
if (charge>=max_charge) {execute attack; charge=0};
} else
{
if (charge) {charge=0};//simply reset charge or attack with less charge if youve accumulated any
}
You missed the part where the charge attack power is transferred, though...

Here's a tip: you can use the lerp() function to linearly interpolate from a minimum to a maximum attack value.
Code:
n = instance_create(x,y,obj_chargedattack);
n.atk = lerp(1,10,charge/charge_max);
Also, you probably want to set the attack's direction to match the player's in most cases, but I can't tell how to do that without knowing how the attacks - and the player direction system, now when I think about it - work in your game.
 
Z

zendraw

Guest
well i thought that wuld be obvious? i was focused on the charge part.
 
I

Intelligents

Guest
Wow this is a lot of good info here Thanks everyone I'll try this out but I have a couple of questions.
what tyler suggested is the way to go
if (key_charge)
{
charge++
if (charge>=max_charge) {execute attack; charge=0};
} else
{
if (charge) {charge=0};//simply reset charge or attack with less charge if youve accumulated any
}
How would I make the variable increase accordingly with the steps?

And I think the tutorial will also cover how to move the hit box from on top of the player to a certain distance of pixels away from the character depending on the direction.

Basically the game is viewed from the side and you stand still but can attack either left or right, while waves of enemies come at you at different speeds. You have a shield too to knock enemies back but I can apply the same charge attack technique to do that.

So this is what I'm trying to do, tell me if I'm on the right track:
In the step event of the sword object (for now I'm using the sword as the collision box instead of an invisible object, so enemies currently lose hp if they are simply touching it, but I'll fix that later):
if (keyboard_check_pressed(vk_right)=true)
{global.swordcharge+=1;}
In the create event for the sword object I defined the variable just as
global.swordcharge = 0
and in the enemy's create I have
enemyhp = 100
a draw event that draws the hp bar above the enemy with an amount of health "enemyhp"
and in a collision event with sword I have
enemyhp -= global.swordcharge
the problem I'm having is that enemies take 1 damage constantly if the button is being pressed, instead of taking equivalent to how long I was pressing the button.

Am I using the expressions correctly?
 
Last edited by a moderator:
Top