0 Favourites

How do I set states with variables? Not sure what's wrong

  • I worked on this all day and I absolutely cannot figure out what is wrong. Probably something really simple, but I'm just not seeing it.

    I have an enemy that checks to see if it's touching one of two imagepoints on a player object =

    Enemybbox (inverted) X = Player_Target.ImagePointX(1) | Player_Target.ImagePointX(2)

    If it does not (meaning the above script is true), a instance variable on Enemybbox called "EnemyAction" is set to a text value of "CHASE". This causes the enemy to move towards either of the Player's two imagepoints. That all works fine.

    However, when the enemy reaches one of the player's imagepoints, I want to switch the instance variable from "CHASE" to "FIGHT", stop moving, and start following a new set of commands listed under an Enemybbox EnemyAction="FIGHT" action. The enemy does stop (because it reaches the imagepoint), but I can't get the variable to change. Even when it's stopped and when I have actions that tell the variable to switch to "FIGHT" once it's reached the imagepoint, in the debugger, it still shows the EnemyAction value as "CHASE".

    Inverting the above script does not work (example below), and I've also tried this with global variables and it didn't work either.

    Enemybbox X = Player_Target.ImagePointX(1) | Player_Target.ImagePointX(2) Set EnemyAction to "FIGHT"

    I really want to use variables because I need to have different states for the enemy (chase, fight, hit, dead, evade, etc). Any help would be greatly appreciated!

  • Make sure in your 'default value' for any Text variable, you aren't using quotes... Something along those lines anyway. Generally some places you need quotes, others you don't. Check them all.

    Edit: I changed from using Text to Number in one place for this reason. I recommend using Number, and have constant variables to denote different 'states' or whatever, like MONSTER_ATTACKING = 1, MONSTER_DEFENDING = 2, etc.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Thanks, but they all seem to be correct. No quotes in the default EnemyAction variable and all of the ones that are called upon have the quotes (or I get the error message saying it's an unknown expression).

    (oh you edited your post, thanks). I did try that once, but I'll do it again and post again shortly.

  • No worries.. even after I corrected mine I still seemed to be having issues. Switched to numeric variables and been fine since. I think they are less error prone because you can't misspell a constant variable's name

    Edit: Yes if you don't use quotes where you need them, it will tell you. But if you use them where you don't have to, it won't. But if yours are fine, then I'm not sure. You could post a capx

  • I changed the variables to numeric and I have the same results. I think the problem occurs when the enemy reaches one of the two Player's imagepoints (Player_Target.ImagePointX(1) | Player_Target.ImagePointX(2).)

    Could you tell me how you might add an action to determine when the enemy has reached an imagepoint and switch the instance variable from 0 to 1 (or whatever)? Whatever I'm doing, it's not right. Very sorry I can't post a capx now, I'm making this for a friend and I'm not supposed to share it yet. If we can't figure it out though, I'll try to make a dummy one and at least post screenshots of the code.

  • I think your

    "Enemybbox (inverted) X = Player_Target.ImagePointX(1) | Player_Target.ImagePointX(2)"

    condition is written the wrong way

    What you want to do is

    Enemybbox (inverted) X = Player_Target.ImagePointX(1)

    Enemybbox (inverted) X = Player_Target.ImagePointX(2)

    And if you want to invert the whole condition (to check if they are actually touching) you should do

    Enemybbox X = Player_Target.ImagePointX(1)


    Enemybbox X = Player_Target.ImagePointX(2)

  • Also the chances that that values match exactly are slim. The usual technique is to use collision or overlap checks, possibly with hidden objects, positioned to the image-points.

  • Also the chances that that values match exactly are slim. The usual technique is to use collision or overlap checks, possibly with hidden objects, positioned to the image-points.

    That's normally true, but we don't know exactly how he's handling his movement, so maybe it's just that precise.

    In any case, blackhornet is right, you shouldn't trouble yourself with manual collision detecting, since the engine does that quite well by itself

  • Thank you for your help so far, I will try what you suggested and post again in a little while.

    My sprites are attached to invisible primitive shapes that act as bounding boxes, and in this particular case, the enemy sprite moves towards the player until it's bounding box overlaps one of the two imagepoints on either side of the player. It actually works very well, there's just a tiny animation issue that I'll try to fix later. Got to fix this problem first.

  • Your commands do work (thank you), but the problem still isn't fixed. I'm sure I'm just missing something simple here, so here's an example of the code. Sorry to bother you guys with this!

  • I believe it's a precision thing. The chance for the enemy's sprite X position to be actually EQUAL to the image point X just when you're checking is pretty slim. You should work with approximate ranges, like so:

    enemy_bbox X < Player_Target.ImagePointX(1) - 2

    enemy_bbox X > Player_Target.ImagePoint(1) + 2

    This checks if the enemy is within a 2 pixel distance from the point. The thing is, you cannot have both AND and OR conditions within the same event, so you will have to use another subevent to check for image point #2, and then either repeat the same actions in both subevents, use a 'isCloseEnough' variable and set it to 1 when the enemy is inside either of the ranges, then do the actions in a subevent with the condition isCloseEnough = 1, or (which I think is the better solution) create a function which takes two X values and a range, returns 1 if they are within the range, or 0 if they are not, and then use System Compare two values like so

    System Function.Call("IsWithinRange", enemy_bbox.X, Player_Target.ImagePointX(1), 2) = 1


    System Function.Call("IsWithinRange", enemy_bbox.X, Player_Target.ImagePointX(2), 2) = 1

  • Thank you for looking at the code. The strange thing is that if I don't try to make states using variables and just put actions to be run when the enemy is in position, everything works (the enemy does attack when it gets in range). As soon as I set up the EnemyAction variables though, the actions that take place when the enemy gets into range do not work. I really don't think it has to do with precision only because it works otherwise, that's why I thought something was wrong with my code.

    I do something similar to call on player attacks using variables to trigger animations to play, and they all work too. Just wish I could figure out what's wrong with this one event...

    I guess I'll try adding an actual sprite for the enemy to try and overlap instead of imagepoints and see if that works. Thank you for the help so far!

  • Allowing the enemy to attack when it's stopped moving (so when it reaches the imagepoints) does work, but it's not what I need. I really do think I need states and variables because the enemy will do different things depending on what the player does, and it could get complicated.

    ...guh, my game is dead in the water if I can't find a solution that allows this (and I won't be able to justify buying the Personal version either). If anyone has any ideas that could help, even if it means completely redoing movement, I'm all ears.

  • I've only scanned the previous posts so sorry if this isn't what you are after, or has already been suggested but I've made a small example.


    Again might not be what you are after but it's late and I'm not feeling 100%.

  • I appreciate it, thank you! Will take a look at it in a little bit and let you know. Hope you feel better soon!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)