Grid based combat
#1
Posted 24 February 2012 - 05:19 PM
The way I have it all set up is that when a player moves to another square, every enemy moves as well.
For combat it should work like when a player moves against an enemy, it attacks it. Also, each enemy needs a separate health variable.
#2
Posted 24 February 2012 - 06:44 PM
That shouldn't be a problem, I don't get why that's there? Do you not know how to use regular variables instead of the built-in health?Also, each enemy needs a separate health variable.
@Every enemy moving, I'd have a variable can_move that gets set to false when the player moves once- then the enemies check if that variable is false, then make their move. Once every enemy has moved, can_move is set to true again.
@Player attacking by moving against an enemy, check the place where the player would move if it was free to see if it is occupied by an enemy; if it is, then the player attacks.
#3
Posted 25 February 2012 - 07:43 PM
Also, I have my code set up like this but it's not working:
First step code:
if global.Move=-1
{
var m;
if keyboard_check_pressed(vk_right)
{
if !place_meeting(x+32,y,obj_Wall) && !place_meeting(x+32,y,obj_Enemy)
{
m=0;
motion_add(0,32);
global.Move=-1;
with (obj_Enemy) {canmove=true; move=m;}
}
else{
if place_meeting(x+32,y,obj_Enemy)
attack=1
}
}And then so on for other directions.Second step code:
if attack = 1
{
obj_Enemy.hp -=1
}
#4
Posted 25 February 2012 - 07:59 PM
#5
Posted 25 February 2012 - 08:05 PM
In what way is it not working? Also, I'd use parent objects. It would make it much simpler.
Well for one, it shows an error that the variable hp doesn't exist even though I called it in the create event.
It also destroys both enemies instead of just the one.
How would I set up a parent?
#6
Posted 25 February 2012 - 08:25 PM
I'd change the code to this (which may or may not work):
var ee;
if global.Move=-1
{
var m;
if keyboard_check_pressed(vk_right)
{
if !place_meeting(x+32,y,obj_Wall) && !place_meeting(x+32,y,obj_Enemy)
{
m=0;
motion_add(0,32);
global.Move=-1;
with (obj_Enemy) {canmove=true; move=m;}
}
else{
ee=place_meeting(x+32,y,obj_Enemy)
if ee>0
{attack=1}
}
}if attack=1 && ee>0
{ee.hp-=1};
#7
Posted 25 February 2012 - 08:33 PM
In the object properties (the menu that opens when you double-click the object in the resources menu) there should be something like "Parent" on the left-hand side- you simple click there and select an object to act as parent. I'd create a base object obj_enemy_basic to act as parent for every type of enemy, and select that as the parent.
I'd change the code to this (which may or may not work):var ee; if global.Move=-1 { var m; if keyboard_check_pressed(vk_right) { if !place_meeting(x+32,y,obj_Wall) && !place_meeting(x+32,y,obj_Enemy) { m=0; motion_add(0,32); global.Move=-1; with (obj_Enemy) {canmove=true; move=m;} } else{ ee=place_meeting(x+32,y,obj_Enemy) if ee>0 {attack=1} } }if attack=1 && ee>0 {ee.hp-=1};
Thanks for the code, but it says that ee is an unknown variable.
#8
Posted 25 February 2012 - 08:45 PM
#9
Posted 25 February 2012 - 09:08 PM
8.1 but I think I figured out the problem. I put ee = 0 and it ran, but nothing happens when I attack. Also it is saying that hp is an unknown variable for ee.hpThat's strange. I'd have no idea why your GM isn't liking variables; what version of GM are you using, by the way?
#10
Posted 25 February 2012 - 09:41 PM
you should set ee to noone by default, since a object can have the index 0 (noone = -4), and instance_place will return noone if it doesn't find anything
if attack=1 && ee!=noone
{ee.hp-=1};
#11
Posted 25 February 2012 - 09:54 PM
place_meeting() only returns true or false .. you might want to use instance_place() which returns the id
you should set ee to noone by default, since a object can have the index 0 (noone = -4), and instance_place will return noone if it doesn't find anythingif attack=1 && ee!=noone {ee.hp-=1};
That's all fine and dandy, but it still says unknown variable for ee.hp
It triggers the attack, but I cannot lower the enemies health.
#12
Posted 26 February 2012 - 05:00 AM
#13
Posted 26 February 2012 - 05:26 AM
You're absolutely the variable hp is set in the object's creation event?
Yes I am absolutely sure.
#14
Posted 26 February 2012 - 06:17 AM
create:
ee=noone;
if attack=1 && ee!=noone
{ee.hp-=1};if that already is so, then I'm not sure, if you change it and it now doesn't give a error, then remove "var ee;" aswell and now it should be able to deal dmg to the foe
#15
Posted 26 February 2012 - 06:39 AM
well, the main code you have been given does not change the local variable ee at all because of "var ee;" on the first line, and therefor should the code with ee.hp-=1 never been allowed to perform since the local variable ee is never changed, only the variable attack .. therefor I have to suspect that ee's default value is not what it should be
create:ee=noone;if attack=1 && ee!=noone {ee.hp-=1};
if that already is so, then I'm not sure, if you change it and it now doesn't give a error, then remove "var ee;" aswell and now it should be able to deal dmg to the foe
I think I fixed it. The problem seemed to be that during the code that subtracts health, I didn't make attack equal 0. Therefore it created a loop and subtracted 1 until hp became invalid.
Thank you so much for your help guys. If I need any further assistance I will let you know!
Edited by seanpk21, 26 February 2012 - 06:42 AM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











