• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

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

Status
Not open for further replies.
Level 2
Joined
Apr 25, 2009
Messages
20
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:

JASS:
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:
Level 10
Joined
Aug 19, 2008
Messages
491
please, write with the [code=jass] [/code] tags when writing scripts, like so:

JASS:
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
 
Level 12
Joined
Jul 27, 2008
Messages
1,181
Make your Gamble_Cond function nicer looking, like:
JASS:
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?
 
Level 2
Joined
Apr 25, 2009
Messages
20
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?
 
I'm new to hive, and i really don't know how to use the JASS tag thing...
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.

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
Can you elaborate please?

and the condition u gave me... won't that just return true no matter what, since the return true is outside of the if?
Nope, the return skippes the code. And the returns inside the if will instantly skip the rest of the code.
 
Level 15
Joined
Dec 18, 2007
Messages
1,098
JASS:
GroupPickRandomUnit(GetUnitsOfPlayerAll(u))
First, this is inefficient.

JASS:
    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?
 
Level 2
Joined
Apr 25, 2009
Messages
20
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"
 
Level 8
Joined
Nov 9, 2008
Messages
502
... and i'm somewhat new to JASS, the tutorial i used to learn how to use JASS didnt explain what a BoolExpR is...

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...

JASS:
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...

JASS:
GroupEnumUnitsInRange(Group, X, Y, Range, tmpFltr)

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

...Could u explain how GroupPickRandomUnit is inefficient...

JASS:
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
JASS:
 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.
 
Level 2
Joined
Apr 25, 2009
Messages
20
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
 
Level 8
Joined
Nov 9, 2008
Messages
502
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.
 
Level 2
Joined
Apr 25, 2009
Messages
20
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...
 
Level 2
Joined
Apr 25, 2009
Messages
20
Ok, So Conditions and Filters are two types of Boolean Expressions
the only problem is i dont no the difference between them...
 
Level 15
Joined
Dec 18, 2007
Messages
1,098
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.
 
Level 2
Joined
Apr 25, 2009
Messages
20
thanks, that actually helped a lot. Now i dont have to use all those If structures in all my other triggers
 
Status
Not open for further replies.
Top