[JASS] Help With Events

Status
Not open for further replies.
Level 17
Joined
Feb 11, 2011
Messages
1,860
Hi guys,

I am trying to learn more JASS, so I tried to make event scripts. Here is a simple test one:

JASS:
function Test takes nothing returns nothing
    call RemoveUnit(GetTriggerUnit())
endfunction

function Trig_Test takes nothing returns nothing
    local trigger t
    set t = CreateTrigger()
    call TriggerRegisterUnitEvent(t, GetTriggerUnit(), EVENT_UNIT_SPELL_EFFECT)
    call TriggerAddAction(t, function Test)
    call DestroyTrigger(t)
    set t = null
endfunction
However, nothing happens! What am I doing wrong?
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
Okay, thanks. Now I have this:

JASS:
function Test takes nothing returns nothing
    call RemoveUnit(GetTriggerUnit())
endfunction

function InitTrig_Test takes nothing returns nothing
    local trigger t
    set t = CreateTrigger()
    call TriggerRegisterUnitEvent(t, GetTriggerUnit(), EVENT_UNIT_SPELL_EFFECT)
    call TriggerAddAction(t, function Test)
endfunction

And still nothing happens. Suggestions?
 
Level 4
Joined
Mar 27, 2008
Messages
112
You cannot register the event to triggerunit as there is no triggerunit yet. Instead use TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
The above line is freehand so it might be a little off but it's something like this.
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
JASS:
function Test takes nothing returns nothing
    call RemoveUnit(GetBuyingUnit())
endfunction

function InitTrig_Test takes nothing returns nothing
    local trigger t
    set t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SELL_ITEM)
    call TriggerAddAction(t, function Test)
endfunction

Still nothing. Any more ideas? :/
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
If you are going to use jass download NewGen and learn to use scopes.
This can also help:
http://www.wc3c.net/vexorian/jasshelpermanual.html

I already know JASS, I am just trying to learn how to make events.

selling unit refferes to the shop and replace GetSellingUnit() to GetTriggerUnit()...

if you wanna reffer it to the hero the replace the EVENT_PLAYER_UNIT_SELL_ITEM to EVENT_PLAYER_UNIT_PAWN_ITEM

Won't EVENT_PLAYER_UNIT_PAWN_ITEM refer to when a unit sells an item TO a shop?

This can be done in 1 line.

More efficient to run as well.

I know this, I just copied that part from a tutorial, to make sure I wasn't doing anything wrong.

EDIT: Still nothing happens. What am I doing wrong? :/

JASS:
function act_Test takes nothing returns nothing
    call RemoveUnit(GetTriggerUnit())
    call RemoveUnit(GetBuyingUnit())
endfunction

function InitTrig_Test takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SELL_ITEM)
    call TriggerAddAction(t, function act_Test)
endfunction
Also, GetTriggerUnit() refers to the shop (just tested it).
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
buy an item...

I'm not stupid...

JASS:
function act_Test takes nothing returns nothing
    call RemoveUnit(GetTriggerUnit())
    call RemoveUnit(GetBuyingUnit())
endfunction

function InitTrig_Test takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PAWN_ITEM)
    call TriggerAddAction(t, function act_Test)
endfunction

Still nothing (EVEN WHEN I BUY AN ITEM)...
 
This code removes the Hero which pawns or sells the item...
JASS:
function act_Test takes nothing returns nothing
    call RemoveUnit(GetTriggerUnit())
endfunction
function InitTrig_Test takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PAWN_ITEM)
    call TriggerAddAction(t, function act_Test)
endfunction

This code removes the shop if the hero BUYS an item...
JASS:
function act_Test2 takes nothing returns nothing
    call RemoveUnit(GetTriggerUnit())
endfunction

function InitTrig_Test2 takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SELL_ITEM)
    call TriggerAddAction(t, function act_Test2)
endfunction

lastly, your code will NOT WORK if your trigger name is NOT Test and Test2...

InitTrig_Test >>> trigger name must be Test
InitTrig_Test2 >>> trigger name must be Test2
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
@cohadar:
Sorry for being so noob - please have patience.

Would someone else mind testing this map and tell me if it works?

Thanks.
Just what i said, the init function doesn't get executed; and why would it.
In vanilla JASS, for an Initialization function (such as adding events to triggers) to get executed, you have to create a so called trigger in GUI, convert it to custom text (select trigger > edit > convert to custom text) and then add code there while init function has to be named: InitTrig_TriggerNameHere.
 
Status
Not open for further replies.
Top