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

Whats Wrong with this Code?

Status
Not open for further replies.
Level 4
Joined
Mar 20, 2014
Messages
67
It gives me an error, is it because I can't add the parameter to the function when I'm adding it as an action? If I can't then what do I do?
- Zach
JASS:
function PlayerExceedsMax takes integer p returns nothing
    call SetPlayerStateBJ( Player(p), PLAYER_STATE_RESOURCE_GOLD, udg_MaxGold )
    call SetPlayerStateBJ( Player(p), PLAYER_STATE_RESOURCE_LUMBER, udg_MaxLumber )
endfunction
 
//===========================================================================
function InitTrig_Player_Exceeds_Max takes nothing returns nothing
    local integer i = 0
    local integer stop = GetPlayers()
    local integer p
    set gg_trg_Player_Exceeds_Max = CreateTrigger(  )
    loop
        call TriggerRegisterPlayerStateEvent( gg_trg_Player_Exceeds_Max, Player(i), PLAYER_STATE_RESOURCE_GOLD, GREATER_THAN_OR_EQUAL, udg_MaxGold[i])
        call TriggerRegisterPlayerStateEvent( gg_trg_Player_Exceeds_Max, Player(i), PLAYER_STATE_RESOURCE_LUMBER, GREATER_THAN_OR_EQUAL, udg_MaxLumber[i] )
        set i = i + 1
        exitwhen i >= stop
    endloop
    set p = GetConvertedPlayerId(GetTriggerPlayer())
    call TriggerAddAction( gg_trg_Player_Exceeds_Max, function PlayerExceedsMax(p) )
endfunction
 
Last edited by a moderator:

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
use jass tags.
JASS:
function PlayerExceedsMax takes integer p returns nothing
    call SetPlayerStateBJ( Player(p), PLAYER_STATE_RESOURCE_GOLD, udg_MaxGold )
    call SetPlayerStateBJ( Player(p), PLAYER_STATE_RESOURCE_LUMBER, udg_MaxLumber )
endfunction
 
//===========================================================================
function InitTrig_Player_Exceeds_Max takes nothing returns nothing
    local integer i = 0
    local integer stop = GetPlayers()
    local integer p
    set gg_trg_Player_Exceeds_Max = CreateTrigger(  )
    loop
        call TriggerRegisterPlayerStateEvent( gg_trg_Player_Exceeds_Max, Player(i), PLAYER_STATE_RESOURCE_GOLD, GREATER_THAN_OR_EQUAL, udg_MaxGold[i])
        call TriggerRegisterPlayerStateEvent( gg_trg_Player_Exceeds_Max, Player(i), PLAYER_STATE_RESOURCE_LUMBER, GREATER_THAN_OR_EQUAL, udg_MaxLumber[i] )
        set i = i + 1
        exitwhen i >= stop
    endloop
    set p = GetConvertedPlayerId(GetTriggerPlayer())
    call TriggerAddAction( gg_trg_Player_Exceeds_Max, function PlayerExceedsMax(p) )
endfunction
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Right. Actions have no explicit parameters. However, they give you access to a number of "event response" functions which serve the same purpose. In this case, you're looking for GetTriggerPlayer().

Furthermore, you should use GetPlayerId and not GetConvertedPlayerId, since Player(GetConvertedPlayerId(p)) will not return p. The difference is that GetPlayerId and Player both work on a 0-15 range, whereas GetConvertedPlayerId is on a 1-16 range (to make it more intuitive for non-programmers using GUI).

Same deal with SetPlayerStateBJ. As a general rule, if it has BJ in the name or highlights red in THW's JASS tags, you probably want to avoid it (with the occasional exception, such as TriggerRegisterAnyUnitEventBJ).
 
In your init function "udg_MaxLumber" are not initiliasized as I can see.

Make your loop run from 0 to 11. Remove this "stop" variable.

AddTriggerAction cant be used if you want to add functions with paramters.

Remove the paramater and just add the function without it for the trigger.

Then in your PlayerExceedsMax you can set playerId on top of trigger:

local integer i = GetPlayerId(GetTriggerPlayer())
As you see don't use GetConvertedPlayerId. It is thought only for GUI users and you won't get the player number of the player you want. (because you also did it at adding event)

SetPlayerStateBJ is useless just use the native function without the BJ.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
JASS:
call TriggerAddAction( gg_trg_Player_Exceeds_Max, function PlayerExceedsMax(p) )
The function takes a function pointer (code) value to a function that takes nothing and returns nothing. The syntax for such does not logically allow for arguments. This is why a syntax error is thrown when parsed.
 
Level 26
Joined
Mar 19, 2008
Messages
3,140
As pointed out, your pointer can not take any arguments, thus you are forced to get "p" via different way. You could use globals, structs and callback functions.

However, I dont think this is want you want, beside fixes and solutions written already, why would you even use set p = GetConvertedPlayerId(GetTriggerPlayer()) within init trigger anyway? Im pretty sure it wont retrieve you any data you would want to receive.

Guess you just wanted to apply an action to your trigger, if so, you dont really need any fancy solutions, just AddAction to trigger providing the code argument takes no parameters.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
No what? No his current (incorrect) function takes an argument (which I agree is wrong)? Or no I am wrong (I to fell for this when I first started using JASS 9 years ago thinking you could give arguments to function pointers so I have a good idea that it is wrong)?

I am telling you that the function pointer (code) used by the action creation native is for functions that take nothing and return nothing. If he tries to reference a function that does not take nothing and return nothing it will have unspecified behaviour and most certainly will not work. As it is a pointer he cannot declare arguments for it as it makes no sense (when would it be evaluated and where would it be stored?).
 
Status
Not open for further replies.
Top