How do I Grab the sum of instance varibles from two instance

  • Lets say you have a player object, and you use the UID to separate them within scripts. I am trying to create a co-op system where they have their own score but have x score to each which multiplies each stage. I want to grab the sum of each instances score. How would I do that.

    Basically

    Instance one score = 25

    instance two score = 50

    so I want to do a test of

    if player.UID(1).score + player.UID(2).score > goal

    Goal being a global variable, set prior. But I don't know how too do that, as it doesn't accept that in expression fields.

    The reason for this being there is a co-op & vs mode I am trying to make, & trying to create shared scripts for different game modes.

  • You can just make a variable called "Combined_ score".

    Set Combined_score = 0
    For each Player
           Combined_score = Combined_score + Player.score
    
    If Combined_score > Goal
    
    [/code:fgt82psg]
  • global var scored

    function scorecounter

    event

    pick instance by unique ID = function.param(0)

    action

    set scored to instance.score

    event

    pick instance by unique ID = function.param(1)

    action

    add instance.score to scored

    use

    function(UID1,UID2)

    and

    event

    scored > 50

    action

    go nutts

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • The idea is there, just wrong implantation. With that for a test value in that case you would want to use a local var not global as it pertains only to that script. Trying to optimize my scripts as much as I possibly can, working on a massive game.

    A expression refference method would be more beneficial & what I'm trying to ask for as it would mean any independent but mathmatically connected value between two instances of the same object type would need a new value created just for the connection, just to be constantly called, never mind if I want to constantly call the function.

    If it is impossible with c2 then, yeah I will have to do it basically the same.

  • hmm

    if a score takes place, have it update a separate var, in a unique instance or global.

    For instance a sprite named player1 with a instance var called score. Or a global var dedicated to the player 1's score.

    Same goes for player 2 and others

    Then, use

    event

    system, compare 2 values

    (player1.score + player2.score+player3.score)

    50

    action

    do something

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