How do I use a variable as an Object in expressions ?

0 favourites
  • 3 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hello, first post here, so I want to congratulate Scirra for their amazing work, and the construct 2 community to gave me a lot of answer with having to post here. But this time I can't find a solution by myself.

    I'm making a game that is a kind of tower defense/shoot'em up/beat'em up where you choose (1-4) heroes from a selection of 10, and can switch from one to another in game.

    What I want to do is to use a variable as an object in expressions, for exemple, in my game I have a variables that contain the name of the Hero the player choose for the A slot, another with the chosen Hero for the B slot (etc for C and D). So one variable per slot : ChoixHeroA / ChoixHeroB / ChoixHeroC / ChoixHeroD

    It would make me earn a lot of event, especially on the smooth HP bar system that use an "every tick" event to use them in expression to remplace the Hero name by the name contained in the variable. But I can't do it so I must make every scenario possible, that is a long thing to do (not a problem , i can deal with it), but also CPU consummating .

    Here what I want to do ( would make 4 events only : one per slot) :

    lerp(self.width, (ChoixHeroA.POWER/ChoixHeroA.POWER_MAX)*BarrePOWER_A.LargeurInitiale, 5*dt)

    Here what i have to do ( make 40 events : 4 slots x 10 hero) :

    lerp(self.width, (HardcoreGirl.POWER/HardcoreGirl.POWER_MAX)*BarrePOWER_A.LargeurInitiale, 5*dt)

    Sorry if it's a noob question (I'm more into graphics stuff than development), and thanks you for your responses.

    If it's can help here's a link to the very alpha of a level, the expression talks about the red bar to the left : http://alphatest.freeteknodefense.com/

    (arrow for moving, "E" to switch between heroes - or gamepad and "B" to switch)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • [quote:1l7ubrr1]What I want to do is to use a variable as an object in expressions, for exemple, in my game I have a variables that contain the name of the Hero the player choose for the A slot, another with the chosen Hero for the B slot (etc for C and D). So one variable per slot : ChoixHeroA / ChoixHeroB / ChoixHeroC / ChoixHeroD...

    Here what I want to do ( would make 4 events only : one per slot) :

    lerp(self.width, (ChoixHeroA.POWER/ChoixHeroA.POWER_MAX)*BarrePOWER_A.LargeurInitiale, 5*dt)

    Here what i have to do ( make 40 events : 4 slots x 10 hero) :

    lerp(self.width, (HardcoreGirl.POWER/HardcoreGirl.POWER_MAX)*BarrePOWER_A.LargeurInitiale, 5*dt)

    Nice graphic and idea for a tower defence

    I think your problem is in your object design, so you should be able to fix it by thinking about your heroes a bit differently. Meaning that your heroes share a lot of common attributes and functionality, Like health, they can move around and so on.

    Since these things are shared, what you can do is look at all your heroes as being the same object type, except with different values, such as name, damage, movement speed and so on.

    So you can solve it several ways. Either you can make one hero and make a unique identifier such as a name variable and use that whenever you have to work with a hero, like this:

    //Require: 
    //Hero.Name
    
    Function Power   (Just a function)
    Pick Hero.Name = Function.param(0) <--- (Function.param(0) would contain the name of the hero you work with) 
    lerp(self.width, (Hero.POWER/Hero.POWER_MAX)*BarrePOWER_A.LargeurInitiale, 5*dt) <-- (This wouldn't work in this example. But what is important to notice is that you can replace the "HardcoreGirl" with "Hero" and thereby make it work for all of them. rather than having to duplicate code, which I guess you want to avoid. But the principal is the same."
    
    [/code:1l7ubrr1]
    
    In this case assuming the lerp worked, it would happen to the hero with the name you call the function with.
    
    Another way is to use a family and add all your heroes to the family. Which would allow you to do as show above as well. Just make sure that all shared variables are added to the family object and not the individual heroes.
    
    Again a good idea to use a unique variable such as name even if they are in a family.
    
    Both methods also allow you to use "for each FAM_heroes" if that's what you called the family or "For each Hero" if you just use one  sprite for all heroes.
    
    However using Families are in my opinion the way to go as you can better work with each individual hero.
    
    If none of what I wrote here makes sense. I suggest you read about "Functions" and "Families" in the manual. And if I misunderstood you, then ignore what I wrote
  • Thanks a lot four your response

    I will take the second option as I'm already using functions and family, but that's true that I don't use the param things (neither the pick) , I have to read about it.

    Thanks again, I'll tell if I manage or fail to do it.

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