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
 
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.
 
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.
 
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
 
in Corelone script, make sure you change the TriggerAddAction line to what dudeim suggested!
 
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
 
[ljass]function HeroRandomRoll takes nothing returns nothing[/ljass]

[ljass]call TriggerAddAction( gg_trg_HeroRandomRoll, function udg_RandomHeroRoll ) //Don't you mean just HeroRandomRoll?[/ljass]
;)
 
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 :)
 
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]
 
Yes, if he wanted to do it in GUI and JASS...
 
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
 
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 The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/
  • The Helper The Helper:
    Happy Thursday!

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top