1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The raddest synthwave tracks were chosen - Check out our Music Contest #12 - Results and congratulate the winners!
    Dismiss Notice
  4. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  5. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  6. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  7. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  8. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  9. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    Dismiss Notice
  10. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] Having Trouble with a gamble trigger (JASS)

Discussion in 'Triggers & Scripts' started by dudds4, Nov 8, 2009.

  1. dudds4

    dudds4

    Joined:
    Apr 25, 2009
    Messages:
    18
    Resources:
    0
    Resources:
    0
    im making a trigger that lets a player gamble.
    the idea would be that when a player says gamble, one of his units is removed from the game, and he has a wins one of 5 types of units, 4 being heroes, and 1 being a footman. to gamble requires 500 gold, and u must have more than 1 unit.

    the main problem is that the programs (JassCraft, and the actual Warcraft 3 editor) don't seem to read any variables. it keeps saying that variables are undefined. its not entirely done( i havent added in the part where it costs 500) because i like to take things one at a time...
    any suggestions would be apreciated

    Here is the actual thing:

    Code (vJASS):

    function gamble_Cond takes nothing returns boolean
        if GetPlayerUnitCount(GetTriggerPlayer(), false) > 1 then
            if GetPlayerState(GetTriggerPlayer(), PLAYER_STATE_RESOURCE_GOLD) > 499 then
            return true
            else
            call BJDebugMsg("sorry, not enough money kid")
            return false
            endif
        else
        call BJDebugMsg("sorry kid, you need more units")
        return false
        endif
    endfunction
    function gamble_actions takes nothing returns nothing
        local unitpool randomUnit = CreateUnitPool()
        call UnitPoolAddUnitType(randomUnit, 'H001', 1.00)
        call UnitPoolAddUnitType(randomUnit, 'H000', 1.00)
        call UnitPoolAddUnitType(randomUnit, 'H002', 1.00)
        call UnitPoolAddUnitType(randomUnit, 'H003', 1.00)
        call UnitPoolAddUnitType(randomUnit, 'hfoo', 4.00)
    local player gambler = GetTriggerPlayer()
    local unit removed = GroupPickRandomUnit(GetUnitsOfPlayerAll(u))
    local location spwn = GetUnitLoc(removed)
    call RemoveUnit(removed)
    call PlaceRandomUnit(randomUnit, gambler, GetLocationX(spwn),GetLocationY(spwn), 0.00)
    set gambler = null
    set removed = null
    call RemoveLocation(spwn)
    call DestroyUnitPool(randomUnit)
    endfunction
    //=====================================================================
    function InitTrig_Gamble takes nothing returns nothing
    local trigger gamble_trig = CreateTrigger()
    call TriggerRegisterPlayerChatEvent(gamble_trig, Player(0), "-gamble", true)
    call TriggerRegisterPlayerChatEvent(gamble_trig, Player(1), "-gamble", true)
    call TriggerRegisterPlayerChatEvent(gamble_trig, Player(2), "-gamble", true)
    call TriggerRegisterPlayerChatEvent(gamble_trig, Player(3), "-gamble", true)
    call TriggerAddCondition(gamble_trig, Condition(function gamble_Cond))
    call TriggerAddAction(gamble_trig, function gamble_actions)
    set gamble_trig = null
    endfunction
     
     
    Last edited: Nov 10, 2009
  2. Cheezeman

    Cheezeman

    Joined:
    Aug 19, 2008
    Messages:
    437
    Resources:
    2
    Spells:
    1
    Tutorials:
    1
    Resources:
    2
    please, write with the [JASS] [/JASS] tags when writing scripts, like so:

    Code (vJASS):

    function gamble_Cond takes nothing returns boolean
        if GetPlayerUnitCount(GetTriggerPlayer(), false) > 1 then
            if GetPlayerState(GetTriggerPlayer(), PLAYER_STATE_RESOURCE_GOLD) > 499 then
                return true
            else
                call BJDebugMsg("sorry, not enough money kid")
                return false
            endif
        else
            call BJDebugMsg("sorry kid, you need more units")
            return false
        endif
    endfunction

    function gamble_actions takes nothing returns nothing
        local unitpool randomUnit = CreateUnitPool()
        call UnitPoolAddUnitType(randomUnit, 'H001', 1.00)
        call UnitPoolAddUnitType(randomUnit, 'H000', 1.00)
        call UnitPoolAddUnitType(randomUnit, 'H002', 1.00)
        call UnitPoolAddUnitType(randomUnit, 'H003', 1.00)
        call UnitPoolAddUnitType(randomUnit, 'hfoo', 4.00)
        //Locals can only be declared at the top of a function.
        //Dunno if it'll solve your problem, but it gives a syntax error.
        local player gambler = GetTriggerPlayer()
        local unit removed = GroupPickRandomUnit(GetUnitsOfPlayerAll(u))
        local location spwn = GetUnitLoc(removed)
        call RemoveUnit(removed)
        call PlaceRandomUnit(randomUnit, gambler, GetLocationX(spwn),GetLocationY(spwn), 0.00)
        set gambler = null
        set removed = null
        call RemoveLocation(spwn)
        call DestroyUnitPool(randomUnit)
    endfunction
    //=====================================================================
    function InitTrig_Gamble takes nothing returns nothing
        local trigger gamble_trig = CreateTrigger()
        call TriggerRegisterPlayerChatEvent(gamble_trig, Player(0), "-gamble", true)
        call TriggerRegisterPlayerChatEvent(gamble_trig, Player(1), "-gamble", true)
        call TriggerRegisterPlayerChatEvent(gamble_trig, Player(2), "-gamble", true)
        call TriggerRegisterPlayerChatEvent(gamble_trig, Player(3), "-gamble", true)
        call TriggerAddCondition(gamble_trig, Condition(function gamble_Cond))
        call TriggerAddAction(gamble_trig, function gamble_actions)
        set gamble_trig = null
    endfunction
     
     
  3. Reaper2008

    Reaper2008

    Joined:
    Jul 27, 2008
    Messages:
    1,117
    Resources:
    1
    Spells:
    1
    Resources:
    1
    Make your Gamble_Cond function nicer looking, like:
    Code (vJASS):
    function gamble_Cond takes nothing returns boolean
        if  GetPlayerUnitCount(GetTriggerPlayer(), false) <= 1 then
            call BJDebugMsg("sorry kid, you need more units")
            return false
        endif
       
        if GetPlayerState(GetTriggerPlayer(), PLAYER_STATE_RESOURCE_GOLD) <= 499 then
              call BJDebugMsg("sorry, not enough money kid")
              return false
        endif

        return true
    endfunction


    Also that locals thingy is correct. Ps: Why not use an array for the random units?
     
  4. dudds4

    dudds4

    Joined:
    Apr 25, 2009
    Messages:
    18
    Resources:
    0
    Resources:
    0
    I'm new to hive, and i really don't know how to use the JASS tag thing...
    i used a unit pool kuz it works better for selecting random units especially since i want footman to be the most commonly picked.
    If the locals are all correct than why won't the program let me do anything? I just don't get it

    and the condition u gave me... won't that just return true no matter what, since the return true is outside of the if?
     
  5. Anachron

    Anachron

    Joined:
    Sep 9, 2007
    Messages:
    6,167
    Resources:
    66
    Icons:
    49
    Packs:
    2
    Tools:
    1
    Maps:
    3
    Spells:
    9
    Tutorials:
    1
    JASS:
    1
    Resources:
    66
    Welcome.

    When you post JASS Scripts, there is an [01010101] icon right above the post box. Select your JASS script and click it in order to make JASS Tags around.

    Can you elaborate please?

    Nope, the return skippes the code. And the returns inside the if will instantly skip the rest of the code.
     
  6. Zack1996

    Zack1996

    Joined:
    Dec 18, 2007
    Messages:
    1,088
    Resources:
    3
    Models:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Code (vJASS):
    GroupPickRandomUnit(GetUnitsOfPlayerAll(u))

    First, this is inefficient.

    Code (vJASS):
        local group g = CreateGroup()
        call GroupEnumUnitsOfPlayer(g, whichPlayer, filter)
        call DestroyBoolExpr(filter)

    This would be more efficient, but use a global BoolExpr. Also, you used the wrong variable. Use gambler instead of u. You did not even declare it.

    Other than that, it should be fine.

    PS: Did you update Jasscraft to work with the 1.24 code?
     
  7. dudds4

    dudds4

    Joined:
    Apr 25, 2009
    Messages:
    18
    Resources:
    0
    Resources:
    0
    oh... locals can only be declared at the top of a function... no1 ever told me that...
    Could u explain how GroupPickRandomUnit is inefficient... and i'm somewhat new to JASS, the tutorial i used to learn how to use JASS didnt explain what a BoolExpR is... So if you could explain that?

    thanks for telling me about the u thing... i just realized that when i edited i forgot to change that "u" to "gambler"
     
  8. toofless

    toofless

    Joined:
    Nov 9, 2008
    Messages:
    496
    Resources:
    0
    Resources:
    0
    A BoolExpr (short for Boolean Expression) is used to enforce conditions when you are making groups.

    For example, this Filter checks whether the unit trying to enter the group belongs to Player 12...

    Code (vJASS):
    function plyrFltr takes nothing returns boolean
        local unit u = GetFilterUnit()
        local boolean a = GetOwningPlayer(u) == Player(11)
        set u = null
        return a
    endfunction


    Then I use the filter when I make the group...

    Code (vJASS):
    GroupEnumUnitsInRange(Group, X, Y, Range, tmpFltr)


    and what I get is a group within Range of X and Y and only Player 12.

    Code (vJASS):

    function GetUnitsOfPlayerAll takes player whichPlayer returns group
        return GetUnitsOfPlayerMatching(whichPlayer, null)
    endfunction

    function GetUnitsOfPlayerMatching takes player whichPlayer, boolexpr filter returns group
        local group g = CreateGroup()
        call GroupEnumUnitsOfPlayer(g, whichPlayer, filter)
        call DestroyBoolExpr(filter)
        return g
    endfunction
     

    and that is why GetUnitsOfPlayerAll() is inefficient. Because it calls another function to do what you could essential write in the first place.

    So, to simply put all units of one player into a group you just
    Code (vJASS):
     local group g = CreateGroup()
        call GroupEnumUnitsOfPlayer(g, whichPlayer, filter)
        call DestroyBoolExpr(filter)
        return g

    Where it says filter, if you don't want to use a filter then type null instead and remove DestroyBoolExpr.
     
  9. dudds4

    dudds4

    Joined:
    Apr 25, 2009
    Messages:
    18
    Resources:
    0
    Resources:
    0
    doing
    call GetUnitsOfPlayerAll(Player(0))

    is a lot easier than doing
    local group g = CreateGroup()
    call GroupEnumUnitsOfPlayer(g, whichPlayer, filter)
    call DestroyBoolExpr(filter)

    and would BoolExpr be like a function that returns boolean, or could it be something totally different?
    for example would IsUnitEnemy be a BoolExpr
     
  10. toofless

    toofless

    Joined:
    Nov 9, 2008
    Messages:
    496
    Resources:
    0
    Resources:
    0
    For you it may be slightly quicker to type but for the program it makes it slightly longer to run because it is doing unecessary things.
    Anyway, you only need:

    local group g = CreateGroup()
    call GroupEnumUnitsOfPlayer(g, Player(0), null)

    For some reason I thought a BoolExpr was a FilterFunc. I think your right BoolExpr is a function that returns boolean so a filter is a type of BoolExpr? I'm confused now.
     
  11. dudds4

    dudds4

    Joined:
    Apr 25, 2009
    Messages:
    18
    Resources:
    0
    Resources:
    0
    If i confused u, i think u shud just go back to thinking what you thought before... kuz i have no idea about BoolExpr, im just asking.

    What do u mean by a filterfunc?
    I'm gonna google that and get back to you on it...
     
  12. dudds4

    dudds4

    Joined:
    Apr 25, 2009
    Messages:
    18
    Resources:
    0
    Resources:
    0
    Ok, So Conditions and Filters are two types of Boolean Expressions
    the only problem is i dont no the difference between them...
     
  13. Zack1996

    Zack1996

    Joined:
    Dec 18, 2007
    Messages:
    1,088
    Resources:
    3
    Models:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Filters are used for groups. When you use a function for a Filter, you gotta do it like this:
    Filter(function xxx).

    This way, the game will use the function to check each unit one by one. If the unit meets certain conditions, it will be added into the group. I think GetFilterUnit() is used to get the unit currently being checked. You can use this to clean out those units you don't want in the group, but I'm not completely certain as I have not used it before.

    Conditions are somewhat the same, except that they are used for triggers to check if certain requirements are fulfiled.

    Basically, a Boolean Expression is some that is used to return a boolean. That's the best I can explain it.
     
  14. Cheezeman

    Cheezeman

    Joined:
    Aug 19, 2008
    Messages:
    437
    Resources:
    2
    Spells:
    1
    Tutorials:
    1
    Resources:
    2
    Optimization is overrated...
     
  15. dudds4

    dudds4

    Joined:
    Apr 25, 2009
    Messages:
    18
    Resources:
    0
    Resources:
    0
    thanks, that actually helped a lot. Now i dont have to use all those If structures in all my other triggers