Random integer function with exceptions

Baltazhar

Active Member
Reaction score
55
As many people before me have probably realised, JASS is needed for some things. Being purely GUI myself until now (except for leak removal), I am struggling a bit with my first JASS function.

Usage: Each player gets a hero, and can repick once. When a hero is randomed, selected or repicked they must not be able to be picked/randomed again until next round.

Theory: The whole idea is to call the function and have it generate a random number for a global variable (with exceptions). So you would make a Custom Script in the GUI with "Call udg_HeroRandomRoll" and it would create a random integer for the HeroRandomRoll with the exceptions of not being one of the previous selected.

After reading what seems to be tons of tutorials I think I get the drift of JASS, but I end up with loads of errors.

The globals have been defined in the Trigger Editor's Variable box and are the following:
JASS:
integer array AvoidSameHeroes //default value 0
integer HeroRandomRoll //default value 0
integer TotalNumberOfHeroes //default value 42 (current number of different heroes)


The entire code (with errors) looks like this:
JASS:

function RandomHeroRoll takes nothing returns nothing
  local integer iRand=0
  local boolean AddNew=True //Expected a name
  local integer a=0 //Expected expression
  local integer b=0 //Expected expression

    loop
        exitwhen a>20 //Expected name
        set iRand=GetRandomInt(1, udg_TotalNumberOfHeroes)
        set b=0 //Expected a variable name
        set AddNew=True //Expected a variable name
            loop
                exitwhen b == a //Expected a name
                if (udg_AvoidSameHeroes<b> == iRand) then //Expected a name
                    set AddNew=False //Expected a variable name
                    set b=a //Expected a variable name
                endif //Expected &#039;endloop&#039;
                set b=b+1 //Expected a variable name
            endloop
        if (AddNew == True) then //Expected a name
            set udg_AvoidSameHeroes[a]=iRand //Expected a name
            set a=a+1 //Expected a variable name
            set udg_HeroRandomRoll=iRand
        endif //Expected &#039;endloop&#039;
    endloop //Expected &#039;endloop&#039;
endfunction</b>


I am unsure if I need an addition to the trigger posted above that states this:
JASS:
//===========================================================================
function InitTrig_HeroRandomRoll takes nothing returns nothing
    set gg_trg_HeroRandomRoll = CreateTrigger(  )
    call TriggerAddAction( gg_trg_HeroRandomRoll, function Trig_HeroRandomRoll_Actions )
endfunction


The trigger doesn't need any events or conditions, just actions with randoming the integer when run.

Hope someone with more insight than me can help here.

Thanks in advance,
- Balt
 

dudeim

New Member
Reaction score
22
Change this line
JASS:
call TriggerAddAction( gg_trg_HeroRandomRoll, function Trig_HeroRandomRoll_Actions )


to

JASS:
call TriggerAddAction( gg_trg_HeroRandomRoll, function RandomHeroRoll )


because you probably changed the name of the function after you converted it you also have to change it in the init function as now you were calling another function that may not exist.
 

Corleone

New Member
Reaction score
44
Try this code

JASS:
function RandomHeroRoll takes nothing returns nothing
  local integer iRand=0
  local boolean AddNew=true
  local integer a=0
  local integer b=0

    loop
        exitwhen a&gt;20 
        set iRand=GetRandomInt(1, udg_TotalNumberOfHeroes)
        set b=0 
        set AddNew=true 
            loop
                exitwhen b == a 
                if (udg_AvoidSameHeroes<b> == iRand) then 
                    set AddNew=false 
                    set b=a 
                endif 
                set b=b+1 
            endloop
        if (AddNew == true) then 
            set udg_AvoidSameHeroes[a]=iRand 
            set a=a+1 
            set udg_HeroRandomRoll=iRand
        endif 
    endloop 
endfunction

//===========================================================================
function InitTrig_HeroRandomRoll takes nothing returns nothing
    set gg_trg_HeroRandomRoll = CreateTrigger(  )
    call TriggerAddAction( gg_trg_HeroRandomRoll, function Trig_RandomRoll_Actions )
endfunction</b>


The problem was that you wrote "True" and "False" instead of "true" and "false". JASS is case sensitive. Once you make a typo like that you'll automatically get a billion errors, even though it's only one small mistake.

You must also make sure that you have defined all of the global variables in the variable editor; The program cannot refer to variables that don't exist, after all.

Hope that helps.
 

Baltazhar

Active Member
Reaction score
55
You are both absolute stars! Was able to enable the trigger after implementing both suggestions.

I am unable to test if it work as intended, as I'm currently at work Remote Desktop'ing to my home computer. I can use the editor, but can't do anything that requires 3D graphics :)

Rep given to both of you - Thanks for the assistance <3
 

jig7c

Stop reading me...-statement
Reaction score
123
in Corelone script, make sure you change the TriggerAddAction line to what dudeim suggested!
 

Baltazhar

Active Member
Reaction score
55
It appears I have yet another problem - Not sure what happened, but the trigger disabled itself with an error.

Here's the complete trigger along with the //error code.
JASS:

function HeroRandomRoll takes nothing returns nothing
  local integer iRand=0
  local boolean AddNew=true
  local integer a=0
  local integer b=0

    loop
        exitwhen a&gt;20
        set iRand=GetRandomInt(1, udg_TotalNumberOfHeroes)
        set b=0
        set AddNew=true
            loop
                exitwhen b == a
                if (udg_AvoidSameHeroes<b> == iRand) then
                    set AddNew=false
                    set b=a
                endif
                set b=b+1
            endloop
        if (AddNew == true) then
            set udg_AvoidSameHeroes[a]=iRand
            set a=a+1
            set udg_HeroRandomRoll=iRand
        endif
    endloop
endfunction

//===========================================================================
function InitTrig_HeroRandomRoll takes nothing returns nothing
    set gg_trg_HeroRandomRoll = CreateTrigger(  )
    call TriggerAddAction( gg_trg_HeroRandomRoll, function udg_RandomHeroRoll ) //Expected a function name
endfunction
</b>


Not entirely sure why it's upset this time, I could swear I changed nothing since the previous update.
Anyway, it won't allow me to enable the trigger, so I am hoping for your expertise once more :)

Further more I have a question that I haven't been able to find the answer to anywhere, or perhaps I'm doing that wrong too.
I am still more comfortable in GUI, so I thought I'd make a GUI trigger to test my JASS function, but I get the "Expected a function name" error there as well on the line with the Custom Script.

Trigger:
  • Test JASS
    • Events
      • Player - Player 1 (Red) types a chat message containing jass as An exact match
    • Conditions
    • Actions
      • Game - Display to (All players) for 1.00 seconds the text: ...
      • For each (Integer A) from 1 to 20, do (Actions)
        • Loop - Actions
          • Custom script: call HeroRandomRoll ()
          • Game - Display to (All players) for 120.00 seconds the text: (Number was: + (String(HeroRandomRoll)))


I've tried different variations of the "call HeroRandomRoll ()" line, but all fails with the same message.

Hope you guys can help, and thanks in advance.
- Baltazhar
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
[ljass]function HeroRandomRoll takes nothing returns nothing[/ljass]

[ljass]call TriggerAddAction( gg_trg_HeroRandomRoll, function udg_RandomHeroRoll ) //Don't you mean just HeroRandomRoll?[/ljass]
;)
 

Baltazhar

Active Member
Reaction score
55
Ah yeah, that was a mistake in the copy/paste.

The code does state this (as you suggested):
JASS:

function InitTrig_HeroRandomRoll takes nothing returns nothing
    set gg_trg_HeroRandomRoll = CreateTrigger(  )
    call TriggerAddAction( gg_trg_HeroRandomRoll, function RandomHeroRoll )
endfunction


Sorry about the mixup, have been playing around to test if I could get anything to work :)
 

tooltiperror

Super Moderator
Reaction score
231
This won't really help you with the problem, but,
JASS:
function InitTrig_HeroRandomRoll takes nothing returns nothing
    set gg_trg_HeroRandomRoll = CreateTrigger(  )
    call TriggerAddAction( gg_trg_HeroRandomRoll, function RandomHeroRoll )
endfunction


Can be inlined to --->

JASS:
function InitTrig_HeroRandomRoll takes nothing returns nothing
    call TriggerAddAction(CreateTrigger(), function RandomHeroRoll )
endfunction


As long as you're not using conditions, that is.

You'll find a lot of cool things to inline as you go on in JASS, like instead of creating an effect, it's something like [ljass]call CreateEffectAtLoc(location, effect)[/ljass] and then you destroy it, you can do like [ljass]call DestroyEffect(CreateEffectAtLoc(location,effect))[/ljass]
 

tooltiperror

Super Moderator
Reaction score
231
Yes, if he wanted to do it in GUI and JASS...
 

Komaqtion

You can change this now in User CP.
Reaction score
469
No ?!

You don't use "function" there...
Just "call" ;)

But anyways, you should just copy this code into the map header, if you don't have Newgen...
JASS:
function HeroRandomRoll takes nothing returns nothing
  local integer iRand=0
  local boolean AddNew=true
  local integer a=0
  local integer b=0

    loop
        exitwhen a&gt;20
        set iRand=GetRandomInt(1, udg_TotalNumberOfHeroes)
        set b=0
        set AddNew=true
            loop
                exitwhen b == a
                if (udg_AvoidSameHeroes<b> == iRand) then
                    set AddNew=false
                    set b=a
                endif
                set b=b+1
            endloop
        if (AddNew == true) then
            set udg_AvoidSameHeroes[a]=iRand
            set a=a+1
            set udg_HeroRandomRoll=iRand
        endif
    endloop
endfunction</b>


The "Map Header" is the highest item in the Trigger list, where the map name is, and the "Map" icon is :D
 

Baltazhar

Active Member
Reaction score
55
Hey guys, here's an update.

I put it in the "Map Header" and removed the last "InitTrig-lines". Both the map and the "call HeroRandomRoll ()" custom script is accepted by the editor when saved. Read up on that header, and loading that function before any triggers is definately a good idea - Thanks Komaqtion! <3

The JASS test trigger I made doesn't display anything at all, but I'm sure there's a reasonable explanation for that some where. Going to take a look, I probably missed something :)

One small step for me, a huge leap for.. no one really, but it's a work in progress :p

+Rep given for helpful inputs, thanks again guys.

- Baltazhar
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top