| 10-23-2006, 03:17 AM | #1 |
Yes, I need help again with a JASS trigger. Anyways, what I want to do is once a hero picks up an item, it will turn on another trigger. Every 10 seconds that the hero has the item there will be a 50% chance to make the item into a better item. If he already has the better item, it will be a 50% chance to make it into a worse item. I made it by 3 triggers: 1st trigger "Item Start": Trigger: 2nd trigger "Item End": Trigger: 3rd trigger "Item": JASS:function Trig_Item_Func003C takes nothing returns boolean local integer a = GetRandomInt(1, 100) if ( not ( a >= 50 ) ) then return false endif if ( not ( GetItemTypeId(UnitItemInSlotBJ(gg_unit_H000_0004, 1)) == 'I000' ) ) then return false endif return true endfunction function Trig_Item_Func004C takes nothing returns boolean local integer a = GetRandomInt(1, 100) if ( not ( a >= 50 ) ) then return false endif if ( not ( GetItemTypeId(UnitItemInSlotBJ(gg_unit_H000_0004, 1)) == 'I001' ) ) then return false endif return true endfunction function Trig_Item_Actions takes nothing returns nothing if ( Trig_Item_Func003C() ) then call RemoveItem( GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I000') ) call UnitAddItemByIdSwapped( 'I001', gg_unit_H000_0004 ) else call DoNothing( ) endif if ( Trig_Item_Func004C() ) then call RemoveItem( GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I001') ) call UnitAddItemByIdSwapped( 'I000', gg_unit_H000_0004 ) else call DoNothing( ) endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger( ) call DisableTrigger( gg_trg_Item ) call TriggerRegisterTimerEventPeriodic( gg_trg_Item, 10.00 ) call TriggerAddAction( gg_trg_Item, function Trig_Item_Actions ) endfunction Now, there's no errors, but for some reason it won't change into the item I want! I create a local variable random int from 1-100, check if it's 50+, and if it is it's suppose to change the item. But when I test it in-game, it doesn't work. Thanks in advance to anyone who helps (Or, if everything's correct and I'm just unlucky and can't get the better item, tell me, thanks )P.S. Is setting an integer null = destroying it (like DestroyForce etc.)? |
| 10-23-2006, 10:21 AM | #2 |
Well, since its converted from GUI, it has nasty looks, so lets remove them first. JASS:function Trig_Item_Actions takes nothing returns nothing if (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I000') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I000')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I001', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif if (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 1)) == 'I000') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I001')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I000', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger() call TriggerRegisterTimerEventPeriodic(gg_trg_Item, 10.00) call TriggerAddAction(gg_trg_Item, function Trig_Item_Actions) call DisableTrigger(gg_trg_Item) endfunction EDIT: Removed useless functions EDIT2: Removed some BJ's, and swapped DisableTrigger to the bottom of the InitTrig, i think that disallows it to add conditions and actions, i doubt it but its worth a try, i dont see anything wrong with this code, test it and tell us what happens. |
| 10-23-2006, 09:45 PM | #3 |
Thanks. When I tried it out, I realised 2 things: a) It ALWAYS converted to Crown of Kings at the 1st 10 seconds. b) It NEVER turned back to Crown of Princes. Or again, maybe I'm too unlucky/impatient. EDIT: JASS:function Trig_Item_Actions takes nothing returns nothing if (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I000') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I000')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I001', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif if (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 1)) == 'I000') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I001')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I000', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger() call TriggerRegisterTimerEventPeriodic(gg_trg_Item, 10.00) call TriggerAddAction(gg_trg_Item, function Trig_Item_Actions) call DisableTrigger(gg_trg_Item) endfunction Hm, is it suppose to be the same (I'm not sure because I suck at JASS), or is it a typo you made? |
| 10-23-2006, 10:06 PM | #4 |
Damnit, your write, typo. Change it too: JASS:function Trig_Item_Actions takes nothing returns nothing if (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I000') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I000')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I001', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif if (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 1)) == 'I001') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I001')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I000', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger() call TriggerRegisterTimerEventPeriodic(gg_trg_Item, 10.00) call TriggerAddAction(gg_trg_Item, function Trig_Item_Actions) call DisableTrigger(gg_trg_Item) endfunction |
| 10-23-2006, 10:13 PM | #5 |
Thanks :D |
| 10-24-2006, 04:45 AM | #6 |
You do realise that the way you have this, it could potentially upgrade the item and immediately take it back away since you are using if endif if endif. Basically making it more like a 25% chance to upgrade it instead of a 50% chance since you have a 50% chance of losing it again . I don't know if you wanted it like this also, but you are only checking the first item slot. Edit: I noticed though that you are checking item slot 0, then item slot 1, so it wouldn't happen like I thought, but still doesn't make a lot of sense as to why you're doing it like that. I would think you should be checking every item slot. Try this code: JASS:function Trig_Item_Actions takes nothing returns nothing local integer i=0 if (GetRandomInt(1, 100)>=50 then loop exitwhen i>5 if GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004,i))=='I000' then call RemoveItem(UnitItemInSlot(gg_unit_H000_0004, i)) call UnitAddItemById(gg_unit_H000_0004,'I001') exitwhen true elseif GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004,i))=='I001' then call RemoveItem(UnitItemInSlot(gg_unit_H000_0004, i)) call UnitAddItemById(gg_unit_H000_0004,'I000') exitwhen true endif set i=i+1 endloop endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger() call TriggerRegisterTimerEventPeriodic(gg_trg_Item, 10.00) call TriggerAddAction(gg_trg_Item, function Trig_Item_Actions) call DisableTrigger(gg_trg_Item) endfunction |
| 10-24-2006, 08:10 PM | #7 |
Where does it say I'm only checking item slot 0? And yes, I wanted only item slot 1. Where in your code does it make an action of checking all item slots? And thanks for another way of doing it. EDIT: @The_Awakening: Hm, it doesn't work. Note that I'm using the normal JASS built in with World Editor, so I can't use any other crap. EDIT EDIT: These are the erorrs: JASS:function Trig_Item_Actions takes nothing returns nothing local integer i=0 if (GetRandomInt(1, 100)>=50 then loop exitwhen i>5 if GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004,i))=='I000' then call RemoveItem(UnitItemInSlot(gg_unit_H000_0004, i)) call UnitAddItemById(gg_unit_H000_0004,'I001') exitwhen true elseif GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004,i))=='I001' then call RemoveItem(UnitItemInSlot(gg_unit_H000_0004, i)) call UnitAddItemById(gg_unit_H000_0004,'I000') exitwhen true endif set i=i+1 endloop endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger() call TriggerRegisterTimerEventPeriodic(gg_trg_Item, 10.00) call TriggerAddAction(gg_trg_Item, function Trig_Item_Actions) call DisableTrigger(gg_trg_Item) endfunction EDIT EDIT EDIT: Oh nevermind about the item slot thing, this is it right? JASS:if (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I000') then |
| 10-24-2006, 09:06 PM | #8 |
Ah, good spotting Awakening, i dident notice it could potentially remove it again. Change it too: JASS:function Trig_Item_Actions takes nothing returns nothing if (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I000') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I000')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I001', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) elseif (GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I001') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I001')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I000', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger() call TriggerRegisterTimerEventPeriodic(gg_trg_Item, 10.00) call TriggerAddAction(gg_trg_Item, function Trig_Item_Actions) call DisableTrigger(gg_trg_Item) endfunction |
| 10-24-2006, 10:12 PM | #9 |
Thanks for the help guys. But it STILL doesn't work properly. It turned to Crown of Kings at the 1st 10 seconds, and again, it won't turn back to Crown of Princes. EDIT: No one answered my question. Is setting an integer = null like using DestroyForce? |
| 10-24-2006, 11:30 PM | #10 | |
Quote:
You cant set a integer to null. Ok, here i go again. I'll explain the basis of leakage. A handle is something that isent a string, real, integer, boolean or code. A handle leaks, if its not a handle, dont worry about it. A string surposidly leaks once, although i wouldent call this a leak, more of a optimization for reusage. Anyway, you only have to destroy a handle, and you only have to set a handle to null. So setting a integer to null will probally give you a error, if not it wont do much anyway. Ok. Ok, you have 3 things when you create a handle, the pointer, the actual handle, and the variable. The chain is: Code:
Variable --> Pointer --> Handle Now, when you Destroy a variable, you destroy the end part, which is at the end of the chain. When you null a variable, you destroy the pointer. Without the pointer, you cant find the Handle. Example: JASS:... local unit u = CreateUnit(...) set u = null call UnitAddTimedLife(u, ...) ... That will add timed life to nothing Because variable u is pointing to nothing, so the handle is now lost. The handle is still in the game, but nothing is pointing to it, so it cant be retrieved. Now, this is where leaks come in. If you end a function, without destroying handles your finished with, theyre is no possible way to find a pointer, so that handle is perminatly in your game, thats bad news. This is a appropiate way to deal with handles: JASS:... local group g = CreateGroup() call DoSomethingWithAGroup(g) call DestroyGroup(g) set g = null We create the varible, and we set its pointer to CreateGroup, that will then return a fresh group, we then use group g, and once weve finished, we destroy it. So no harm is done, but be carful, pointers also use memory, so they also leak, so you need to set them to null once you arnt going to use it again. This applies to all handles, in theyre own respective Destroy/Remove function, just look them up on Jasscraft of something. Hope that helped. |
| 10-25-2006, 05:16 AM | #11 |
Eh, sorry about that. I just missed one ) change this line in my code: JASS:if (GetRandomInt(1,100)>=50 then JASS:if (GetRandomInt(1,100))>=50 then The code should work perfect. If you don't want to check all item slots, then just remove JASS:loop exitwhen i>5 JASS:set i=i+1 endloop |
| 10-25-2006, 10:45 AM | #12 | |
Quote:
And the 2 exitwhen true's. I'v never seen that used before, exitwhen true in a if statement, thats a good idea, meaning if the if is right it exits the loop, i'll bear that in mind! |
| 10-27-2006, 04:03 AM | #13 |
Alright, thanks, will try it tomorrow. Going to sleep now. EDIT: Great, it's STILL not working. I don't understand what's wrong. Here's my code and the errors: JASS:function Trig_Item_Actions takes nothing returns nothing if (GetRandomInt(1, 100))>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I000') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I000')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I001', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) elseif (GetRandomInt(1,100))>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I001') then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I001')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I000', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger() call TriggerRegisterTimerEventPeriodic(gg_trg_Item, 10.00) call TriggerAddAction(gg_trg_Item, function Trig_Item_Actions) call DisableTrigger(gg_trg_Item) endfunction ERROR 1: Expected 'then' ERROR 2: Expected 'endif' ERROR 3: Expected 'endif' I don't get it... |
| 10-28-2006, 02:28 AM | #14 |
You have too many of these " ) ". Remove the last one before "then", in both the red highlighted code. |
| 10-28-2006, 04:25 AM | #15 |
should be like this :) JASS:function Trig_Item_Actions takes nothing returns nothing if GetRandomInt(1, 100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I000' then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I000')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I001', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) elseif GetRandomInt(1,100)>=50 and GetItemTypeId(UnitItemInSlot(gg_unit_H000_0004, 0)) == 'I001' then call RemoveItem(GetItemOfTypeFromUnitBJ(gg_unit_H000_0004, 'I001')) call UnitAddItem(gg_unit_H000_0004, CreateItem('I000', GetUnitX(gg_unit_H000_0004), GetUnitY(gg_unit_H000_0004))) endif endfunction //=========================================================================== function InitTrig_Item takes nothing returns nothing set gg_trg_Item = CreateTrigger() call TriggerRegisterTimerEventPeriodic(gg_trg_Item, 10.00) call TriggerAddAction(gg_trg_Item, function Trig_Item_Actions) call DisableTrigger(gg_trg_Item) endfunction |
