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

Legacy GM Changing the order of code execution[SOLVED]

A

ammu678

Guest
So i have 2 objects, one is the hero and other is enemy so i already designed the hero's attacks and enemy's attacks, so now the problem i have is that say the user presses a button to start the attack then at that time

candmg=1//a local variable in hero object

then in that object collison is checked with the enemy if its true then

global.dmg=1;

then in enemy object again a collison with hero is checked and provide global.dmg=1
then he enter damage state and cant move nor attack.(as corresponding variables are set to 0)

its somewhat similiar when enemy attacks the hero, a collison is checked and an attack is chosen at random
candmg=1;(local to enemy)
then a collison is checked with the hero object in step event of the enemy object

if (candmg=1 && place_meeting(obj_enemy.x,obj_enemy.y,obj_ichigo))
{
obj_ichigo.herodmg=1;
}
then accordingly hero enter dmg state.. so the problem is when the enemy detects collison with hero's attack sprite its starts its own attack choosing at random to solve this i created a variable as a conditon

canattack=1

for enemy to start the attack and also set in damaged state of the enemy canattack=0. but now it seems choosing attacks code is executed earlier than checking if enemy has to go in damaged state or not..so canattack isn't set to 0 early enough since when i start the game enemy also attacks when i attack him simultaneously . i tried putting the code(to check enemy is in damaged state or not) early in the step event that didnt work as of now i have the code in a separate drag and drop collison check.


Edit:turned out my enemy's damage animation time was very less so before my hero's attack was finished canattack was set to 1(since after my enemy damage state was over canattack was set to 1)
 
Last edited by a moderator:
A

Aura

Guest
Not sure what you're doing, but you're over-complicating it. Use states and determine the behaviour of instances on that basis. Makes the code more organized and easy to change.
 
W

Wraithious

Guest
it looks like both objects are setting global.dmg to 1, maybe set it to 1 so hero can attack, and 2 so enemy can attack, so you could have something like

hero button event:

if (global.dmg=1 && place_meeting(obj_ichigo.x,obj_ichigo.y,obj_enemy) )
{
obj_enemy.enemydmg=1;
alarm[0]=10;//set to whatever you want
}

enemy step event:

if (global.dmg=2 && place_meeting(x,y,obj_ichigo))
{
obj_ichigo.herodmg=1;
alarm[0]=10;//set to whatever you want
}

alarm event[0] (make a local variable called change or whatever you want, and set it to 1):

if (global.dmg=1 && change=1 )
{
global.dmg=2;
obj_ichigo.canattack=1;
obj_ichigo.candmg=1;
obj_enemy.canattack=0;
obj_enemy.candmg=0;
change=0;
alarm[1]=1;
}
if (global.dmg=2 && change=1)//edited 1/1/16
{
global.dmg=1;//edited 7/1/16
obj_ichigo.canattack=0;
obj_ichigo.candmg=0;
obj_enemy.canattack=1;
obj_enemy.candmg=1;
change=0;
alarm[1]=1;
}

alarm 1 event:

change=1;
 
Last edited by a moderator:
A

ammu678

Guest
Not sure what you're doing, but you're over-complicating it. Use states and determine the behaviour of instances on that basis. Makes the code more organized and easy to change.
by states you mean attack state and damage state and stuff? i mean its not function or tool right?
I am kind of new to programming AI(this is my first time) and i am not really following any tutorial, is that required?.

anyway, Thanks for the help
 
Last edited by a moderator:
A

ammu678

Guest
it looks like both objects are setting global.dmg to 1, maybe set it to 1 so hero can attack, and 2 so enemy can attack, so you could have something like
.
.
.
:
;
actually i am not using global.dmg as a condition for whether hero can attack or not its like this variable is for all enemies to check whether they were attacked by hero or not but in each enemy 2 conditions will be checked
1)if (global.dmg=1)
2)if collison is there with hero
this means enemy is attacked then it cant attack nor move

also both objects are not setting global.dmg=1 it is set 1 only in hero object(When collison of enemy with hero's ATTACK sprite only)

also the code in alarm 1 is the entire code there for both hero and enemy? i mean i dont understand it if it is as will 2nd part be ever executed since change =0 and the condition would be checked before alarm 1 can make change=1;

also, would it help if i make a video?
Thanks for the help
 
Last edited by a moderator:
W

Wraithious

Guest
That could actually be the problem, or part of the problem, both of your objects are relying on global.dmg being set to 1. a global variable can be used by any object, local variables are object specific and are harder to use to affect other objects, I mean it can be done but it is much easier to rely on what a global variable's value is. so if you set it to 1 for one object, 2 for another etc. you have more control over what object can execute it's own code

quote:
also the code in alarm 1 is the entire code there for both hero and enemy? i mean i dont understand it if it is as will 2nd part be ever executed since change =0 and the condition would be checked before alarm 1 can make change=1;

change would = 0 so it will only execute that part of the code instead of it changing global.dmg to 1 then in the next part changing it to 2, if change is 1 and global.dmg is 1 it will setit to 2 and ignore the rest of the code, then in the next alarm it triggers it sets change back to 1 so that the next time the first alarm is triggered it will see that change=1 and global.dmg is 2, and it will set global.dmg back to 1 and change to 0, then set the second alarm that brings change back to 1, this is a type of switch

oh, i overlooked 1 thing, if you use the step event for the enemy you should use a switch there too to prevent the alarm from not being set untill place meeting is no longer true (if objects are meeting the alarm will allways be set to 10) like this:

enemy step event:

if (global.dmg=2 && place_meeting(x,y,obj_ichigo))
{
obj_ichigo.herodmg=1;
if (change=1)
{alarm[0]=10;//set to whatever you want
change=0;
}
alarm[1]=1;
}

notice alarm[1]=1 is outside the if change=1 switch block? that will keep it from setting the alarm[0] again untill the objects arn't meeting anymore, and when you press the button for your hero object that will still do what you want it to do, hope that helps
 
Last edited by a moderator:
A

ammu678

Guest
That could actually be the problem, or part of the problem, both of your objects are relying on global.dmg being set to 1. a global variable can be used by any object, local variables are object specific and are harder to use to affect other objects, I mean it can be done but it is much easier to rely on what a global variable's value is. so if you set it to 1 for one object, 2 for another etc. you have more control over what object can execute it's own code
all right so i understand your code now but in your previous post in the alarm 1 you made both the if conditions same,thats a typo right?

so what your suggesting is that i use global variables to determine whether hero or enemy can attack or not. But i cant use one variable to determine whether both enemy and hero can attack as that would mean they can attack only alternatively

also when you put alarm[1] =1 outside that block, say a step event repeats after 1 step so next time change will be 1 the code will execute the same way, so i still dont understand what difference it will make. And also when you say that alarm 0 mustn't be set the 2nd time untill they are in collison are you assuming that their collison will stop in 1 step(The sprites are all animations and i am using modified masks like only collison with sword matters, but its difficult to say how long collison is)

the attacks works fine, i mean its probably not the matter of which code executes first like i had orginally asked since putting that code(canattack=0) earlier didnt make a difference, like you said it might be mistake in assigning variables properly but global.dmg (atleast the way i used it) seems fine
 
W

Wraithious

Guest
ahh, sorry for the confusion, i had thought you had wanted them to attack in turn and not get damaged if they were attacking.

Yep, i just realized that was a typo sorry i will edit it now

With the last thing i said,
notice alarm[1]=1 is outside the if change=1 switch block? that will keep it from setting the alarm[0] again untill the objects arn't meeting anymore, and when you press the button for your hero object that will still do what you want it to do
I was trying to show that if you wanted the user to switch turns when pressing the button during colision you could do that and it would overide the enemy's attack if the objects stayed meeting, but as you've said its a weapon mask your using so you don't need to worry about that.
 
Top