• 🏆 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!

[JASS] Help me with pickup trigger

Status
Not open for further replies.
Level 6
Joined
Oct 4, 2008
Messages
263
Yeah, i've been learning JASS for about a day now, and i want to create a trigger for my map. and i must say, i pretty lost.
Í want my trigger to, whenever a unit picks up an item, that item is removed, and then a dummy unit spawns inside a region called 'inventory [player ID number]', and then that dummy unit recieves a copy of the item that was being picked up.
could anybody help me out here?
here's the function ive worked out so far:
JASS:
function pickup takes unit picking, item picked returns nothing
local player pickplayer = GetOwningPlayer (picking)
//does this return "Player(id) or just "id"??
call CreateUnitAtLocByName (Player(6), dummy, GetRectCenter(gg_rct_inventory_XX)
//as you can see, i need to find out a way to find out whose inventory to spawn the unit in.

//i also need to find out how to create the function which triggers this one.
 
Level 13
Joined
Nov 22, 2006
Messages
1,260
I think it would be something like this:

JASS:
function Actions takes nothing returns nothing
    local unit u = GetManipulatingUnit() // GUI: Hero manipulating item (picking unit)
    local item i = GetManipulatedItem() // GUI: Manipulated item
    local integer id = GetItemTypeId(i) // id of the item
    local player p = GetOwningPlayer(u) // you know what this is :P
    
    call RemoveItem(i) // removes the item from the game (meaning from the unit also)
    set u = CreateUnitByName(p, "dummy", GetRectCenterX(gg_rct_inventory_XX), GetRectCenterX(gg_rct_inventory_XX), 0) // i didn't quite get why you use that ByName function, i don't know how that works exactly, but i guess you do :D
    call UnitAddItemById(u, id) // adds that item to the dummy unit
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
    call TriggerAddAction(t, function Actions)
endfunction

If that was what you wanted. I'm not sure if that works, I haven't worked with items much.
 
Level 6
Joined
Oct 4, 2008
Messages
263
quite simply, i used byname because i dont know what the ID of my dummy unit is :D.
thanks for help. ill check if it works (+rep to you if so).
EDIT: im recieving an error message, that the 'create unit' line soesnt work. it says 'expecting a name'.
 
Last edited:
You forgot to null your handles.

JASS:
function Actions takes nothing returns nothing
    local unit u = GetManipulatingUnit() // GUI: Hero manipulating item (picking unit)
    local item i = GetManipulatedItem() // GUI: Manipulated item
    local integer id = GetItemTypeId(i) // id of the item
    local player p = GetOwningPlayer(u) // you know what this is :P

    call RemoveItem(i) // removes the item from the game (meaning from the unit also)
    set u = CreateUnitByName(p, "dummy", GetRectCenterX(gg_rct_inventory_XX), GetRectCenterX(gg_rct_inventory_XX), 0) // i didn't quite get why you use that ByName function, i don't know how that works exactly, but i guess you do :D
    call UnitAddItemById(u, id) // adds that item to the dummy unit
    set u = null
    set i = null
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
    call TriggerAddAction(t, function Actions)
endfunction
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Can't seem to find any errors. The error you have is probably related to another trigger, right before this one.

Anyway, I have 2 thoughts:
1) In its current form your map will crash, because you'll be stuck in an infinite loop. Turn off the trigger before giving the item to the dummy, then turn it back on.
2) Why are you creating a new item? Just drop the manipulated item, and give the item to the dummy.
 
Level 13
Joined
Nov 22, 2006
Messages
1,260
Apparently gg_rct_inventory_XX doesn't exist in your map, because that's the only error I'm getting in JNGP (advanced syntax checker).

EDIT:

2) Why are you creating a new item? Just drop the manipulated item, and give the item to the dummy.

Because I honestly don't care. If there was a negative value in caring, I would certainly have reached it.

EDIT2: Hmm, that was a bit harsh. I just don't care :D
 
Last edited:
Level 6
Joined
Oct 4, 2008
Messages
263
ok, this is what i ended up with:
Code:
function Actions takes nothing returns nothing
    local unit u = GetManipulatingUnit() // GUI: Hero manipulating item (picking unit)
    local item i = GetManipulatedItem() // GUI: Manipulated item
    local integer id = GetItemTypeId(i) // id of the item
    local player p = GetOwningPlayer(u) // you know what this is :P
    call RemoveItem(i) // removes the item from the game (meaning from the unit also)
    set u = CreateUnitByName(p, "dummy", GetRectCenterX(gg_rct_inventory_XX), GetRectCenterX(gg_rct_inventory_XX), 0)
    call DisableTrigger (GetTriggeringTrigger())
    call UnitAddItemById(u, id) // adds that item to the dummy unit
    set u = null
    set i = null
    call EnableTrigger (GetTriggeringTrigger())
    endfunction

//===========================================================================
function InitTrig_Actions takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
    call TriggerAddAction(t, function Actions)
endfunction

its really wierd, because everything seems to work, it doesnt display any errors, but then when i test the map, my hero buys the item, it disappears, just as planned, but no units spawn anywhere.
i think i might have found out a better way to do what i was trying to achieve. so there might just be no need for this. +rep for your efforts, though.
 
Level 6
Joined
Oct 4, 2008
Messages
263
ok, im gonna try a new approach to this system im trying to make.
whats supposed to happen now is, you pick up an item, then you gain X ability and Y stat increase(or whatever), and then the item is destroyed.
im still tampering with the mechanics, but the basic script is this:
JASS:
function Actions takes nothing returns nothing
    local unit u = GetManipulatingUnit() // GUI: Hero manipulating item (picking unit)
    local item i = GetManipulatedItem() // GUI: Manipulated item
    if i == 'i000' then
        call UnitAddAbility(u, A001)
        call SetUnitManaBJ(u,500)
    elseif i == 'i002' then
        call UnitAddAbility(u, A002)
        call UnitAddAbility(u, AOcr)
    elseif i == 'i001' then
        call UnitAddAbility(u,A003)
        call SetUnitLifeBJ(u, 1000)
    endif
    call RemoveItem(i) // removes the item from the game (meaning from the unit also)
 endfunction   

//===========================================================================
function InitTrig_Actions takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
    call TriggerAddAction(t, function Actions)
endfunction
i000/1/2 being item Id's for the example items.
however, the script shows a whole load of error messages that i honestly dont know what means.
screenloz.jpg
 
Level 6
Joined
Oct 4, 2008
Messages
263
that helped, although it still gives me error messages, saying that:
line 20(if i == 'I000' then):invalid type for specified operator
lines 23,26 and 29:(all the lines with elseif and endif): expected a code statement.
 
Level 11
Joined
Feb 22, 2006
Messages
752
Items aren't integers. What you want is to get the item's type id. And you're still missing the ' ' around your ability ids. So change your script to:

JASS:
function Actions takes nothing returns nothing
    local unit u = GetManipulatingUnit() // GUI: Hero manipulating item (picking unit)
    local item i = GetManipulatedItem() // GUI: Manipulated item
    local integer id = GetItemTypeId(i)
    if id == 'i000' then
        call UnitAddAbility(u, 'A001')
        call SetUnitManaBJ(u,500)
    elseif id == 'i002' then
        call UnitAddAbility(u, 'A002')
        call UnitAddAbility(u, 'AOcr')
    elseif id == 'i001' then
        call UnitAddAbility(u, 'A003')
        call SetUnitLifeBJ(u, 1000)
    endif
    call RemoveItem(i) // removes the item from the game (meaning from the unit also)
endfunction
//===========================================================================
function InitTrig_Actions takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
    call TriggerAddAction(t, function Actions)
endfunction
 
Level 6
Joined
Oct 4, 2008
Messages
263
JASS:
function Actions takes nothing returns nothing
    local unit u = GetManipulatingUnit() // GUI: Hero manipulating item (picking unit)
    local item i = GetManipulatedItem() // GUI: Manipulated item
    local integer id = GetItemTypeId(i)
    local integer pid = GetPlayerId(GetOwningPlayer(u))
    if id == 'I000' then
        if udg_PCHC[pid] == false then
           call UnitAddAbility(u, 'A001')
           call SetUnitManaBJ(u,500)
           udg_PCHC[pid] = true
        else
           call DisplayTextToPlayer(GetOwningPlayer(u), 0, 0, 'You already have a class')
        endif
    elseif id == 'I002' then
        if udg_PCHC[pid] == false then
           call UnitAddAbility(u, 'A002')
           call UnitAddAbility(u, 'AOcr')
           udg_PCHC[pid] = true
        else
           call DisplayTextToPlayer(GetOwningPlayer(u), 0, 0, 'You already have a class')
        endif
    elseif id == 'I001' then
        if udg_PCHC[pid] == false then
           call UnitAddAbility(u,'A003')
           call SetUnitLifeBJ(u, 1000)
           udg_PCHC[pid] = true
        else
           call DisplayTextToPlayer(GetOwningPlayer(u), 0, 0, 'You already have a class')
        endif
    endif
    call RemoveItem(i) // removes the item from the game (meaning from the unit also)
 endfunction   

//===========================================================================
function InitTrig_Actions takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
    call TriggerAddAction(t, function Actions)
endfunction
I added some functions that check if you already have a benefit granted from picking up an item. but there is some error messages that i dont know hat means...
at lines 11, 19, and 27: expected 'set'
at lines 12, 20 and 28: expected expression.
wth does that mean? and how do i fix it?
 
Level 11
Joined
Feb 22, 2006
Messages
752
To set a variable, you need to begin the line with the keyword 'set', so for example:

JASS:
    set udg_PCHC[pid] = true

O, and you should add the line:

JASS:
    set i = null

As the very last line in function Actions, so you don't leak a handle pointer.

EDIT: Enclose strings with double quotes, not single quotes:

JASS:
"You already have a class"

Single quotes are only used for converting ascii characters into their integer values. And in other languages, unicode characters as well.
 
Level 6
Joined
Oct 4, 2008
Messages
263
that worked, thanks a lot again! (ill probably be back in a few moments to bother you again, lol)
 
Status
Not open for further replies.
Top