How do I refer to different objects based on the level

0 favourites
  • 8 posts
From the Asset Store
Adjusting the game screen for different resolutions (Letterbox scale)
  • Hi,

    I'd like to update some of my game's code so that it could be more efficient and easy to deal with.

    Here is the result I want :

    In Healer Quest, each level is the same layout and same event sheet, but sees you fighting different enemies. I would like all the 4 player's characters to automatically attack enemies at random based on the current level (selecting a valid target).

    There are enemies on the first line and enemies on the second line, and of course they should attack enemies on the front line first (refer to my signature for a visual clue, even though there is only first line enemies in the picture).

    Here is how I did it :

    For each playable character, I made a huge list of all levels (more than 100) and the code looks like this :

    (...)

    Level = 32 (you are fighting rats (1st line enemies) and bats (second line enemies))

    Rat is visible

    Pick random rat instance and deal damage to it

    Else Bat is visible

    Pick random Bat instance and deal damage to it

    Level = 33 (you are fighting thieves (1st line enemies) and knifeThrowers (second line enemies))

    thief is visible

    Pick random thief instance and deal damage to it

    Else knifeThrower is visible

    Pick random knifeThrower instance and deal damage to it

    (...)

    What I would like to change :

    Right now, if I want to change something in the way damage is dealt to monsters, I need to update more than 800 events! And I will need to update this code many times in the following months... So I'm looking for a solution to avoid this.

    I hoped I would be able to do this with functions, but if I'm correct, it appears that functions doesn't allow to define a certain object to which the function should be applied.

    Is there any simpler way to deal with that part of the code?

    Thanks in advance for any ideas!

  • What I would like to change :

    Right now, if I want to change something in the way damage is dealt to monsters, I need to update more than 800 events! And I will need to update this code many times in the following months... So I'm looking for a solution to avoid this.

    I hoped I would be able to do this with functions, but if I'm correct, it appears that functions doesn't allow to define a certain object to which the function should be applied.

    Is there any simpler way to deal with that part of the code?

    Thanks in advance for any ideas!

    Functions work on whatever you tell them to work with. So you can define exactly what objects you want and don't want. It depend on what you call it with. Unless I misunderstand what you mean?

    Initially by looking at the code you have posted, it seems like you have a lot of unnecessary code due to the way you have made it. Meaning you have X amount of enemies, which I assume all share the same properties, like health and so on. But from the screenshot you are referring to each of them as individuals, like Wolf, Rat and so on. But if you had a family and some other structure of events using functions, you could have used the family instead, except for spawning them. Meaning for instant in event 799 and 795 you basically do the same thing, dealing archer damage to an enemy. But since all enemies have health you could simply put this in a function if you had family. like so:

    We pass two objects to the function.
    0 Param = Enemy
    1 Param = Player Character
    
    Function "Deal damage to Enemy"
    Pick Fam_Enemy.UID = Function.Param(0)
    Pick Fam_Player_Character.UID = Function.Param(1)
    //Now you have both the enemy and the player character picked.
    Fam_Enemy.Health = Fam_Enemy.Health - Player_Character.Damage
    
    //Now it doesn't matter if its a Wolf, Bat or Rat. So you can get away with a lot of events using functions and families.
     [/code:2o38iop6]
    
    Now this is just an example as im not sure how it works exactly in your game. But using families and functions are crucial in my opinion, because as you say yourself, you now have 800 events and having to go through them and correct them will take a long time. And to turn it all into using families and functions will also take a while, as you have to remake a lot of code. But if you plan on making lots of changes the next couple of month, my best guess is that you most likely would benefit the most from organising your project so it uses families and functions correctly. So the base for further developing your game is in order. 
    
    Anyway since you are only showing a small part of your program, this is just my initial view from looking at the code in your screenshot, so might of course not be true.
  • nimos100

    Hi and thanks a lot for your help.

    I really believe your solution should work in my game, and I'm ready to pass a few hours or days optimizing this part of the code, I'm sure it will be worth it + mastering it is time well spent.

    If I understand correctly, the code you are describing couldn't work without using families, right?

    Thanks again! I'll try your solution today!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'm already facing a problem actually. I need to select one of the enemies at random, so after selecting the enemy family just as you stated, how could I do that?

    In my code I made

    pick enemy where enemy.number=randomValue (targetArcher in my code)

    so that I select only one instance at random

    but as now it could be any sort of enemy (as I'm just selecting a family's UID), I don't see how to do that...

    The idea would be to pick fam_enemies where fam_enemies.UID = Param(0) and then check the "number" instance variable of each instances of the selected object type to select the correct instance. Is it possible to do this, given it could be any enemy type?

    I think that if there is a solution to this, it should work perfectly!

    Thanks again!

  • I'm already facing a problem actually. I need to select one of the enemies at random, so after selecting the enemy family just as you stated, how could I do that?

    In my code I made

    pick enemy where enemy.number=randomValue (targetArcher in my code)

    so that I select only one instance at random

    but as now it could be any sort of enemy (as I'm just selecting a family's UID), I don't see how to do that...

    The idea would be to pick fam_enemies where fam_enemies.UID = Param(0) and then check the "number" instance variable of each instances of the selected object type to select the correct instance. Is it possible to do this, given it could be any enemy type?

    I think that if there is a solution to this, it should work perfectly!

    Thanks again!

    If you just need to select a random enemy on the your combat screen you can use "Pick random instance" from the system tab, and then you of course just choose the family as being the object.

    [quote:1levrniq]

    The idea would be to pick fam_enemies where fam_enemies.UID = Param(0) and then check the "number" instance variable of each instances of the selected object type to select the correct instance. Is it possible to do this, given it could be any enemy type?

    To select a more specific type of enemy in a family you can sort of do the same thing as above using random. But since you are now looking for a specific type, you first have to narrow it down.

    The way you can do that, is to add a variable to you enemy family called "Type" for instant and then in each enemy you write there type, for instant "Wolf", "Dire Wolf", "Bat" etc.

    Then when you need to pick a random enemy type, you just do like this:

    Pick Fam_Enemies.Type = "Wolf"

    Pick random Instance Fam_Enemies.Type

    If that is what you mean?

  • nimos100

    Thanks for the reply!

    [quote:32bvka2k]If you just need to select a random enemy on the your combat screen you can use "Pick random instance" from the system tab, and then you of course just choose the family as being the object.

    Yes it could work, but there is one subtility : there are (up to) 3 enemies on the front line, and up to 3 enemies in the back line. So I'm selecting a number between 0 an 2 and the ally attacks the line of enemies corresponding to this number. If the enemy on the first row of this line is already dead, the ally attacks the back line enemy of this same row instead. Now if I'm selecting an instance at random, the allies won't attack enemies on the back line before every enemies on the front line are dead, and that's a problem.

    I probably misexplained myself, as I never need to select an enemy type at random. This will be based on the current level. However what I need to do is to select one given instance of an enemy type, based upon a predefined instance variable which is equal to 0, 1 or 2 (the "number" variable, which defines the row number, so that the ally could attack the back line enemy sharing this number with the front line enemy, if the front line enemy is dead).

    So it can't be an instance at random, I'm afraid.

  • nimos100

    I probably misexplained myself, as I never need to select an enemy type at random. This will be based on the current level. However what I need to do is to select one given instance of an enemy type, based upon a predefined instance variable which is equal to 0, 1 or 2 (the "number" variable, which defines the row number, so that the ally could attack the back line enemy sharing this number with the front line enemy, if the front line enemy is dead).

    So it can't be an instance at random, I'm afraid.

    Ok but the concept should be the same, instead of "Type" you just make it "Row" or whatever. When you spawn the enemies you just assign them a row number.

    And you do the same:

    Pick Fam_Enemy.Row_Number = 1 (For instant)

    And then you can add random enemy in that row or the one selected by the player etc.

  • nimos100

    It worked out! Thanks a lot, it really helps + I learned a lot!

    Instance variable in families is definitely something I will use in the future.

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