SC2Mapster Forums

Development > Galaxy Scripting

arrays in an event doesn't work

  • 6 posts
    #1 Aug 03, 2012 at 13:16 UTC - 0 likes

    hello, I am using triggers for the most part but I have a custom script snippet in my code for the "TriggerAddEventUnitProperty" functionality.

    My script looks like this

    UnitCreate(1, "Stalker2", 0, 1, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_test[0] = UnitLastCreated();
    
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_test[0]"), c_unitPropLife);
    

    The "gt_hb3" trigger has only has one event: to say "hello world" in chat.

            UI - Display "hello world" for (All players) to Chat area
    

    The problem is that the trigger will activate when ANY UNIT changes health. Now the reason I come up with the assumption that arrays don't work is because when you use a regular variable in the add event function:

    UnitCreate(1, "Stalker2", 0, 1, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_test = UnitLastCreated();
    
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_test"), c_unitPropLife);
    

    Now the code works... I don't understand how to make arrays work in add even function.

    What I'm trying to do I'm trying to create a custom boss bar. I have a function that when called will create a boss bar user interface component on the screen. The function will also add an event (Unit property changes) to a trigger that updates the custom boss bar. The function creates a boss bar for any unit that contains health, shield, energy, experience, level, name, and portrait information which is why I don't just use the pre-made boss bar. This is more like a unit status bar.

    I would really like to use an array, but if all fails I guess I'll have to make many variables.

    Thank you for your help.

    BTW, the reason I need arrays is to create a unit bar for each player like so:

    lv_temp_1 = 1;
    while (lv_temp_1 <= 4) {
    UnitCreate(1, "Stalker2", 0, lv_temp_1, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_hero[lv_temp_1] = UnitLastCreated();
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_hero[lv_temp_1]"), c_unitPropLife);
    lv_temp_1 = lv_temp_1 + 1;
    }
    

    never-mind I fixed it immediately, I simply put the array value into a temporary variable and used that.

    here is the fix for the code in the very beginning:

    UnitCreate(1, "Stalker2", 0, 1, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_test[0] = UnitLastCreated();
    gv_tester = gv_test[0];
    
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_tester"), c_unitPropLife);
    

    I hope this helps someone else.

    EDIT: THE SOLUTION I POSTED DOES NOT WORK, READ MY NEXT POST! I STILL NEED HELP.

    Last edited Aug 03, 2012 by bradosia
    #2 Aug 03, 2012 at 13:38 UTC - 0 likes

    Out of curiosity: Why do you use UnitRefFromVariable over UnitRefFromUnit? The latter should not have any problems whatsoever with an array variable.

    The only reason to pick UnitRefFromVariable I could think of would be, that it updates the unit reference to the new unit, if you change the variable dynamically (does it? just guessing here), but that would cause your current solution to malfunction, wouldn't it?

    Last edited Aug 03, 2012 by Kueken531

    http://img577.imageshack.us/img577/4226/blaj.jpg

    #3 Aug 03, 2012 at 14:32 UTC - 0 likes

    @Kueken531: Go

    I'm actually fairly new to SC2 editor so I'm not sure what "UnitRefFromUnit" function does.

    Actually I have new problems in my trigger on the lines of what you just stated. When I change the variable reference to a new unit, the trigger will fire upon the new unit's life changing and the old unit's unit bar will not update. I have no idea how to go about doing this now.

    How would I go about using "UnitRefFromUnit" instead?

    At the moment it seems that I will have to create a new variable for every player's hero, like so:

    UnitCreate(1, "Stalker2", 0, 1, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_player_1_hero = UnitLastCreated();
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_player_1_hero"), c_unitPropLife);
    UnitCreate(1, "Stalker2", 0, 2, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_player_2_hero = UnitLastCreated();
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_player_2_hero"), c_unitPropLife);
    UnitCreate(1, "Stalker2", 0, 3, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_player_3_hero = UnitLastCreated();
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_player_3_hero"), c_unitPropLife);
    UnitCreate(1, "Stalker2", 0, 4, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_player_4_hero = UnitLastCreated();
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_player_4_hero"), c_unitPropLife);
    

    if I use:

    lv_temp_1 = 1;
    while (lv_temp_1 <= 4) {
    UnitCreate(1, "Stalker2", 0, lv_temp_1, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_hero[lv_temp_1] = UnitLastCreated();
    gv_hero_temp = gv_hero[lv_temp_1];
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_hero_temp"), c_unitPropLife);
    lv_temp_1 = lv_temp_1 + 1;
    }
    

    then the trigger "gt_hb3" will only activate when the health of the last stalker created changes. I actually have not solved this at all yet.

    EDIT: Fixed it again!

    lv_temp_1 = 1;
    while (lv_temp_1 <= 4) {
    UnitCreate(1, "Stalker2", 0, lv_temp_1, RegionRandomPoint(RegionFromId(1)), libNtve_gf_RandomAngle());
    gv_hero[lv_temp_1] = UnitLastCreated();
    TriggerAddEventUnitProperty(gt_hb3, UnitRefFromVariable("gv_hero[" + IntToString(lv_temp_1) + "]"), c_unitPropLife);
    lv_temp_1 = lv_temp_1 + 1;
    }
    

    It seems one can pass an array value. I just forgot to convert the integer into a string so the wrong key for the array kept getting passed. Yahoo!!

    Last edited Aug 03, 2012 by bradosia
    #4 Aug 03, 2012 at 15:00 UTC - 1 like

    UnitRefFromUnit would be used about the same way, just that you don't use a string matching the variable name, but the variable itself (so basically, same thing but no ""):

    UnitRefFromVariable("MyUnitVariable")  -> UnitRefFromUnit(MyUnitVariable)
    

    Also, your further problem confirmed, what I was suspecting earlier, the unit reference for UnitRefFromVariable gets updated, if you change the value of the variable. This is probably useful for stuff like repicking, you wouldn't need to add a new event/trigger for the replaced unit.

    Last edited Aug 03, 2012 by Kueken531
    #5 Aug 03, 2012 at 22:51 UTC - 0 likes

    Thank you very much Kueken!

    Problem completely solved now. I was able to remove the string reference and now the game works perfectly.

    #6 Dec 06, 2012 at 22:00 UTC - 0 likes

    Damn...

    If you use triggers instead of pure galaxy you can't fix this...

    the array is empty on startup
    initTriggers() is called while the array is still empty
    the event is now registered to null aka any unit
    initialising the array in a map init trigger comes way to late

    edit: I was wrong, you can add the event later after the init in a custom script section with the array and it works

    Last edited Dec 06, 2012 by b0ne123

    A programmer started to cuss
    Because getting to sleep was a fuss
    As he lay in his bed
    Looping 'round in his head
    Was: while (!asleep()) sheep++;

    twitter.com/SC2IncomeWars
    facebook.com/SC2IncomeWars

  • 6 posts

You must login to post a comment. Don't have an account? Register to get one!