HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Basic Item Ownership Trigger

08-20-2007, 05:47 PM#1
crayz
Hello, I am going to show you how to create an item ownership trigger in this tutorial.

Basically what we will do is, if an item is picked up we check the custom value of that item. If it's currently set to 0, then the item is fresh & not owned by anyone.

So, if the custom value of the item is 0, the user is allowed to pick up the item. After he picks the item up, the custom value of the item is set to the player number of the triggering unit.

When the user picks up an item that the custom value is not 0, it will check if the custom value of the item matches the player number of the triggering unit. If it does, it will allow the user to pick the item up. Otherwise, the user can't pick the item up, and a user-friendly message is shown.

We'll start with out event:
Trigger:
Events
Unit - A unit Acquires an item

This just runs the actions when any unit acquires an item.

We will have no conditions, as all the checking will be done by If/Then/Else actions.

Trigger:
Conditions
Collapse Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Then - Actions
Else - Actions

Here is our first If/Then/Else action. Inside the If - Conditions we will put a
Trigger:
(Custom value of (Item being manipulated)) Equal to 0

This checks if the custom value of the item is 0. Now for the actions that execute if it returns true:

Trigger:
Conditions
Collapse Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to (Player number of (Triggering player))
Else - Actions

Trigger:
Item - Set the custom value of (Item being manipulated) to (Player number of (Triggering player))

What this does, is it sets the custom value of the item to the number of the player owning the triggering unit. So that way, this item system will know whether or not the item is currently owned by a player, and if it is, it will know which player owns the item.

Now, if the check for the custom value of the item = 0 returns False, our Else actions will be executed. So lets put our second If/Then/Else inside the Else action:

Trigger:
Conditions
Collapse Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to (Player number of (Triggering player))
Collapse Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
Then - Actions
Else - Actions

Inside the If - Conditions for our second If/Then/Else, we will need this:

Trigger:
(Custom value of (Item being manipulated)) Equal to (Player number of (Triggering player))

If the check of the units custom value = 0 returns false, then of course we need to see what the custom value of the item really is. So, that code will be added. And here is where we are at so far:

Trigger:
Collapse Events
Unit - A unit Acquires an item
Conditions
Collapse Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to (Player number of (Triggering player))
Collapse Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to (Player number of (Triggering player))
Then - Actions
Else - Actions

Now we just have 2 last triggers. Ignore the Then - Actions inside that second If/Then/Else, because that is what executes when the custom value is the same as the player number of the triggering unit. AKA, that's what executes when the item belongs to that player.

So if you have any creative ideas to add there, like a message that says: Item Equipped! : or w/e, then put them there :)

Now, in the last Else - Actions, we need to add these 2 triggers:

Trigger:
Hero - Drop (Item being manipulated) from (Triggering unit)
Game - Display to (Player group((Owner of (Triggering unit)))) the text: This item does not belong to you!

These drop the item from the player & displays the user-friendly message, only if the item does not belong to the user trying to pick the item up.

And there we have it, your own Item Ownership trigger. I hope you find this useful for your project :)

Here is the full trigger:

Trigger:
Collapse Events
Unit - A unit Acquires an item
Conditions
Collapse Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to (Player number of (Triggering player))
Collapse Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to (Player number of (Triggering player))
Then - Actions
Collapse Else - Actions
Hero - Drop (Item being manipulated) from (Triggering unit)
Game - Display to (Player group((Owner of (Triggering unit)))) the text: This item does not belong to you!

Thanks for reading

And of course, if you wish to add a periodic event to remove ownership from ground-items, you could do something like:

Trigger:
Clean Items Copy
Collapse Events
Time - Every 300.00 seconds of game time
Conditions
Collapse Actions
Collapse Item - Pick every item in (Playable map area) and do (Actions)
Collapse Loop - Actions
Item - Set the custom value of (Picked item) to 0

How to Create Unit-Specific Items
By Pyrogasm


We do this in nearly the same way as for player-specific items except that we must also use a handy-dandy JASS function called the H2I return-bug. It is a very useful (and very safe; unlike I2H) way to get the handle-index of a particular handle (a handle is everything but a real, integer, boolean, or string).

The handle-index is basically like a number-slot that can be used to identify the handle (there is more, but that's all that is concerned with this tutorial), which we will be using to check to see if the right unit is picking up the item. In order to use the return-bug, you'll need to add the following to your map's Custom Script header:
Collapse JASS:
function Items_H2I takes handle h returns integer
    return h
    return 0
endfunction

Not so hard, is it? Just C 'n P it into the header. Then, we'll see how this is applied to the previous trigger.

First, we'll have to get the custom value of the item like usual and then compare that to the handle index of the manipulating unit. Here's how we'd do that (this requires a variable of type "integer", which I called UnitIndex):
Trigger:
Collapse Events
Unit - A unit Acquires an item
Conditions
Collapse Actions
Custom script: set udg_UnitIndex = Items_H2I(GetTriggerUnit())
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to (UnitIndex)
Collapse Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to (UnitIndex)
Then - Actions
Collapse Else - Actions
Hero - Drop (Item being manipulated) from (Triggering unit)
Game - Display to (Player group((Owner of (Triggering unit)))) the text: This item does not belong to this unit!
As you can see, the only differences are the usage of the Items_H2I function and the switching-out of a variable for a few function calls to get the Player Id and such.

Quite simply put, that is all there is to it. However, you can also combine both sets of ownership triggers as seen below. In the following example, I have made it so that if a unit picks up an unowned item it will be useable by that owning player only; however, if it is a Boots of Speed, only that specifc unit may use it. This works because player indexes range from 1 to 16 (0-15 in JASS) and handle indexes are > 820000.
Trigger:
Collapse Events
Unit - A unit Acquires an item
Conditions
Collapse Actions
Custom script: set udg_UnitIndex = Items_H2I(GetTriggerUnit())
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Collapse Then - Actions
Collapse If (All conditiosn are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
((Item-Type of (Item being manipulated)) equal to Boots of Speed)
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to (UnitIndex)
Collapse Else - Actions
Item - Set the custom value of (Item being manipulated) to (Player number of (Owner of (Triggering Unit)))
Collapse Else - Actions
Collapse If (All conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) greater than 16
Collapse Then - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to (UnitIndex)
Then - Actions
Collapse Else - Actions
Hero - Drop (Item being manipulated) from (Triggering unit)
Game - Display to (Player group((Owner of (Triggering unit)))) the text: This item does not belong to this unit!
Collapse Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to (Player Number of (Owner Of (Triggering Unit)))
Then - Actions
Collapse Else - Actions
Hero - Drop (Item being manipulated) from (Triggering unit)
Game - Display to (Player group((Owner of (Triggering unit)))) the text: This item does not belong to you!
08-21-2007, 06:15 AM#3
Pyrogasm
There's a way to expand on this, I believe, to make unit-specific items.

If you instead set the custom value to the handle index of the owning unit and then compare that when the item is picked up you can make unit-specific items (like soulbound items in WoW).

I could write a short extension to this tutorial explaining this if you want (since you don't appear to know JASS, which is slightly required for this) and then Pitzermike could just tack that on to your tutorial if it gets approved.
08-21-2007, 04:48 PM#4
PitzerMike
That's a very good idea, Pyro.
08-21-2007, 05:49 PM#5
Pyrogasm
Well then; what's the likely fate of this tutorial? Shall I write the addition?
08-21-2007, 06:04 PM#6
crayz
Yea that would make a great extension, go ahead and write it :)

Once finished I can just edit my post & throw it in.
08-21-2007, 10:33 PM#7
The Elite
With the Clean items trigger you use the periodic event every 300 seconds, that doesnt meen that if i drop an item it wil dissapear after 300 seconds, i could drop an item after playing for 299 seconds and it would dissapear 1 second later. Try to fix that
08-22-2007, 01:57 AM#8
crayz
Quote:
Originally Posted by The_Elite
With the Clean items trigger you use the periodic event every 300 seconds, that doesnt meen that if i drop an item it wil dissapear after 300 seconds, i could drop an item after playing for 299 seconds and it would dissapear 1 second later. Try to fix that

True, that is one of the problems I faced after showing that example. But it's just an example :) You gotta use your own imagination to create these kind of triggers to work with this Item Ownership Trigger.

But I will try to think up an algorithm to get that fixed, since you asked


Edit: I came up with this, but it smells a bit leaky.

Trigger:
dfgdf
Collapse Events
Unit - A unit Loses an item
Collapse Conditions
((Triggering unit) is A Hero) Equal to True
Collapse Actions
Wait 300.00 seconds
Collapse For each (Integer A) from 1 to 6, do (Actions)
Collapse Loop - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Item being manipulated) Not equal to (Item carried by (Triggering unit) in slot (Integer A))
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to 0
Else - Actions

Would anyone like to fill me in on the leak part?

Thanks!
08-22-2007, 07:31 AM#9
Pyrogasm
Not leaky, but inefficient. Oh, and it won't work because (Item being manipulated) won't return an item after a wait.

The best solution would be through timers and JASS (attachables/structs)
08-22-2007, 03:34 PM#10
PitzerMike
Quote:
Originally Posted by Pyrogasm
Not leaky, but inefficient. Oh, and it won't work because (Item being manipulated) won't return an item after a wait.

The best solution would be through timers and JASS (attachables/structs)

It is decidedly so.
08-25-2007, 09:32 AM#11
Pyrogasm
How to Create Unit-Specific Items
We do this in nearly the same way as for player-specific items except that we must also use a handy-dandy JASS function called the H2I return-bug. It is a very useful (and very safe; unlike I2H) way to get the handle-index of a particular handle (a handle is everything but a real, integer, boolean, or string).

The handle-index is basically like a number-slot that can be used to identify the handle (there is more, but that's all that is concerned with this tutorial), which we will be using to check to see if the right unit is picking up the item. In order to use the return-bug, you'll need to add the following to your map's Custom Script header:
Collapse JASS:
function Items_H2I takes handle h returns integer
    return h
    return 0
endfunction

Not so hard, is it? Just C 'n P it into the header. Then, we'll see how this is applied to the previous trigger.

First, we'll have to get the custom value of the item like usual and then compare that to the handle index of the manipulating unit. Here's how we'd do that (this requires a variable of type "integer", which I called UnitIndex):
Trigger:
Collapse Events
Unit - A unit Acquires an item
Conditions
Collapse Actions
Custom script: set udg_UnitIndex = Items_H2I(GetTriggerUnit())
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to (UnitIndex)
Collapse Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to (UnitIndex)
Then - Actions
Collapse Else - Actions
Hero - Drop (Item being manipulated) from (Triggering unit)
Game - Display to (Player group((Owner of (Triggering unit)))) the text: This item does not belong to this unit!
As you can see, the only differences are the usage of the Items_H2I function and the switching-out of a variable for a few function calls to get the Player Id and such.

Quite simply put, that is all there is to it. However, you can also combine both sets of ownership triggers as seen below. In the following example, I have made it so that if a unit picks up an unowned item it will be useable by that owning player only; however, if it is a Boots of Speed, only that specifc unit may use it. This works because player indexes range from 1 to 16 (0-15 in JASS) and handle indexes are > 820000.
Trigger:
Collapse Events
Unit - A unit Acquires an item
Conditions
Collapse Actions
Custom script: set udg_UnitIndex = Items_H2I(GetTriggerUnit())
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to 0
Collapse Then - Actions
Collapse If (All conditiosn are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
((Item-Type of (Item being manipulated)) equal to Boots of Speed)
Collapse Then - Actions
Item - Set the custom value of (Item being manipulated) to (UnitIndex)
Collapse Else - Actions
Item - Set the custom value of (Item being manipulated) to (Player number of (Owner of (Triggering Unit)))
Collapse Else - Actions
Collapse If (All conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) greater than 16
Collapse Then - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to (UnitIndex)
Then - Actions
Collapse Else - Actions
Hero - Drop (Item being manipulated) from (Triggering unit)
Game - Display to (Player group((Owner of (Triggering unit)))) the text: This item does not belong to this unit!
Collapse Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Custom value of (Item being manipulated)) Equal to (Player Number of (Owner Of (Triggering Unit)))
Then - Actions
Collapse Else - Actions
Hero - Drop (Item being manipulated) from (Triggering unit)
Game - Display to (Player group((Owner of (Triggering unit)))) the text: This item does not belong to you!
08-25-2007, 09:02 PM#12
crayz
Nice. I guess the posts have to be merged by a mod, if I just copy & paste it won't keep the correct format of the triggers.

To whichever mod merges these posts, please include credit for Pyrogasm.

& thanks :)
08-25-2007, 09:13 PM#13
Pyrogasm
You can quote me to get the appropriate vB code formatting.
08-25-2007, 09:29 PM#14
crayz
Can't believe I didn't think of that. :X

Added w/ credits.
08-27-2007, 06:32 PM#15
PitzerMike
Well done, you two.
I approve.