• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] Creating an ammo system, its not working...

Status
Not open for further replies.
Level 4
Joined
Jul 9, 2008
Messages
88
Hello, I am currently creating an ammunition system that the ammo shows up as charges on a permanent item, and if you run out of the charges you can't attack anymore.
I am new to JASS and have a few questions about my script...
1. How do i remove an integer leak? I try to set it to null and JASSCRAFT says i cant convert an integer to null.
2. I have tested it, and even when the unit has ammo, it orders him stop and displays the message. what am I doing wrong?
3. I had heard somewhere that you can remove the attack ability from a unit (and give it back later), is this true? It would make things a hell of a lot easier.
4. JC says that 2 lines of my script is wrong. (its in bold).
Code:
//|----------------------|
//| AMMUNITION SYSTEM|
//|----------------------|
//note that I000 is the default item for ammunition until further versions. 
//clip item is 'I001'
//the following function "ammoatizes" the unit.
function CreateAmmoUnit takes unit u, integer i returns nothing
    local item it=CreateItem('I000', GetUnitX(u), GetUnitY(u))
    call UnitAddItem(u, it)
    call SetItemCharges(it, i)
endfunction
function UseAmmo takes unit u, integer i returns nothing //This sets the charges to be lower
    local item it=UnitItemInSlot(u, 1)
    local integer int
    set int=GetItemCharges(it)-i
    call SetItemCharges(it, int)
endfunction
function AddAmmo takes unit u, integer i returns nothing //this adds ammo
    local item it=UnitItemInSlot(u, 1)
    local integer int
    set int=GetItemCharges(it)+i
    call SetItemCharges(it, int)
endfunction
function GetAmmo takes unit u returns integer//this returns the charges in slot 1,
//the default slot for ammo(undroppable)
    local integer i=GetItemCharges(UnitItemInSlot(u,1))
    return i
endfunction
function AmmoErrorMessage takes integer i, player p returns nothing //this is for messages
    local string message
    if i==0 then
        set message="You don't have enough ammunition to attack."
        call DisplayTimedTextToForce(GetForceOfPlayer(p), 5.00, message)
    elseif i==1 then
        set message="The maximum ammunition you can have is 100."
        call DisplayTimedTextToForce(GetForceOfPlayer(p), 5.00, message)
    elseif i==2 then
        set message="You don't have any ammunition."
        call DisplayTimedTextToForce(GetForceOfPlayer(p), 5.00, message)    
    elseif i==3 then
        set message="The maximum number of clips you can have is 100."
        call DisplayTimedTextToForce(GetForceOfPlayer(p), 5.00, message)
    elseif i==4 then
        set message="Your Hero is trying to attack, but doesn't have any ammunition."
        call DisplayTimedTextToForce(GetForceOfPlayer(p), 5.00, message)
    endif
endfunction
//
//now time for triggers
//
function Trig_Attacking_Actions takes nothing returns nothing
    local integer i
    if GetIssuedOrderIdBJ() == String2OrderIdBJ("attack") then //this is if its actually ordered to attack  
[COLOR="Red"][B]        if not GetItemType(UnitItemInSlot(GetOrderedUnit(), 1))=='I000' then[/B][/COLOR]
            call IssueImmediateOrderBJ( GetOrderedUnit(), "stop" )
            call AmmoErrorMessage(0, GetTriggerPlayer()) 
        elseif GetAmmo(GetOrderedUnit())<1 then //checks if it has no ammo
            call IssueImmediateOrderBJ( GetOrderedUnit(), "stop" )
            call AmmoErrorMessage(0, GetTriggerPlayer())  
        else 
            call UseAmmo(GetOrderedUnit(), 1) //if nothing's wrong, it sets the charges
        endif
    elseif GetIssuedOrderIdBJ() == String2OrderIdBJ("smart") then
    //this does the exact same thing as the previous one, only 
    //this is for it is a "smart" attack,
    //A.K.A. not ordered to attack, it did it on its own.   
       [COLOR="red"] [B]if not GetItemType(UnitItemInSlot(GetOrderedUnit(), 1))=='I000' then[/B][/COLOR]
            call IssueImmediateOrderBJ( GetOrderedUnit(), "stop" )
            call AmmoErrorMessage(0, GetTriggerPlayer()) 
        elseif GetAmmo(GetOrderedUnit())<1 then
            call IssueImmediateOrderBJ( GetOrderedUnit(), "stop" )
            call AmmoErrorMessage(0, GetTriggerPlayer())  
        else 
            call UseAmmo(GetOrderedUnit(), 1)
        endif
    endif           
endfunction

//===========================================================================
function InitTrig_Attacking takes nothing returns nothing
    set gg_trg_Attacking = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Attacking, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER )
    call TriggerAddAction( gg_trg_Attacking, function Trig_Attacking_Actions )
endfunction
//yea yea, I know I have a lot of simialar functions, but I like to write scripts this way,
//instead of writing it out everytime, I like to keep it simple
//thanks for the help I hope you give me!
 
Level 4
Joined
Jul 9, 2008
Messages
88
why thanks, +rep.
EDIT: so just do like "UnitRemoveAbility(Unit, 'Aatk')
EDIT2: It sais on the lines in red that "Comparing two variables of different primitive types except "real" and "integer" is not allowed."
 

bee

bee

Level 2
Joined
Aug 2, 2009
Messages
6
JASS:
function CreateAmmoUnit takes unit u, integer i returns nothing
    local item it=CreateItem('I000', GetUnitX(u), GetUnitY(u))
    call UnitAddItem(u, it)
    call SetItemCharges(it, i)
endfunction
need change to:
JASS:
function CreateAmmoUnit takes unit u, integer i returns nothing
    local item it=CreateItem('I000', GetUnitX(u), GetUnitY(u))
    call UnitAddItem(u, it)
    call SetItemCharges(it, i)
    set it=null
endfunction
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,547
why thanks, +rep.
EDIT: so just do like "UnitRemoveAbility(Unit, 'Aatk')
EDIT2: It sais on the lines in red that "Comparing two variables of different primitive types except "real" and "integer" is not allowed."

1: yes

2: Because you're comparing a specific item (handle) with an item type (integer)

Use GetItemTypeId()
 
Status
Not open for further replies.
Top