Jass actions and Jasscraft (was: Ninja_Sheeps Jass-Question thread.)

Duwenbasden

Ver 6 CREATE energy AS SELECT * FROM u.energy
Reaction score
165
Did you understand what I said? You cannot compare equality between two real numbers. As soon as it is type Real == any number type, it will never work.

Code:
local real Random_Number = GetRandomReal(1.00, 3.00)

Instead of that, use this (you have no choice):
Code:
local integer Random_Number = GetRandomInt(1, 3)
if Random_Number == 3 then
  //...

Real uses more memory too.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Did you understand what I said? You cannot compare equality between two real numbers. As soon as it is type Real == type Real, it will never work.

Code:
local real Random_Number = GetRandomReal(1.00, 3.00)

Instead of that, use this (you have no choice):
Code:
local integer Random_Number = GetRandomInt(1, 300)

Hm... So is the code trying to get it so that it gives like a 3/200 (Something like that) chance of getting those numbers? Because it only modifies to 1.00, 2.00, and 3.00. If you wish for it to do only 3 numbers, then you must have it as an integer but instead from 1 to 3, otherwise you can use what Duwenbasden said.
 

Duwenbasden

Ver 6 CREATE energy AS SELECT * FROM u.energy
Reaction score
165
no, :
Code:
Random_Number=R2I(GetRandomReal(1.00, 3.00))
This line did a random number between 1.00 - 3.00, then converts it to integer with floor (1.99 -> 1, 2.01 -> 2); then converts it back to real (if it is 2, then converted to something between 1.999###... - 2.000...##...)
 

SFilip

Gone but not forgotten
Reaction score
634
> As soon as it is type Real == any number type, it will never work.
You sure?
JASS:
function realtest takes nothing returns nothing
    local real test = 50.2
    if test == 50.2 then
        call BJDebugMsg("1")
    endif
endfunction

Displays 1 on screen after you call it.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
no, :
Code:
Random_Number=R2I(GetRandomReal(1.00, 3.00))
This line did a random number between 1.00 - 3.00, then converts it to integer with floor (1.99 -> 1, 2.01 -> 2); then converts it back to real (if it is 2, then converted to something between 1.999###... - 2.000...##...)

Oh. I missed the R2I (Real to Integer) part. I thought it was simply:

JASS:
Random_Number = GetRandomReal(1.00,3.00)


My mistake. :p
 

Duwenbasden

Ver 6 CREATE energy AS SELECT * FROM u.energy
Reaction score
165
> As soon as it is type Real == any number type, it will never work.
You sure?
JASS:
function realtest takes nothing returns nothing
    local real test = 50.2
    if test == 50.2 then
        call BJDebugMsg("1")
    endif
endfunction

Displays 1 on screen after you call it.

http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
# Equality is problematical! Two computational sequences that are mathematically equal may well produce different floating-point values. Programmers often perform comparisons within some tolerance (often a decimal constant, itself not accurately represented), but that doesn't necessarily make the problem go away.

My solution is to avoid comparing equality between reals as they can be problematic.
 

Ninja_sheep

Heavy is credit to team!
Reaction score
64
It's really annoying -.-
If i remove the 'else' my WE crashes...

But the integer i got now :)

JASS:

function Trig_Assigment_Conditions takes nothing returns boolean
    if ( not ( IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO) == true ) ) then
        return false
    endif
    return true
endfunction


function Trig_Assigment_Actions takes nothing returns nothing
    local unit TriggUnit = GetTriggerUnit()
    local player TriggPlayer = GetOwningPlayer(udg_TriggUnit)
    local integer TriggPlayer_Wood = GetPlayerState(udg_TriggPlayer, PLAYER_STATE_RESOURCE_LUMBER)
    local integer TriggPlayer_Gold = GetPlayerState(udg_TriggPlayer, PLAYER_STATE_RESOURCE_GOLD)
    local real TriggUnit_MaxLife = GetUnitStateSwap(UNIT_STATE_MAX_LIFE, udg_TriggUnit)
    local real TriggUnit_MaxMana = GetUnitStateSwap(UNIT_STATE_MAX_MANA, udg_TriggUnit)
    local integer HeroAgi
    local integer HeroStr
    local integer HeroInt
    local integer Random_Number
    call SetPlayerState( TriggPlayer, PLAYER_STATE_RESOURCE_GOLD, TriggPlayer_Gold +  R2I(TriggUnit_MaxLife))
    call SetPlayerState( TriggPlayer, PLAYER_STATE_RESOURCE_LUMBER, TriggPlayer_Wood +  R2I(TriggUnit_MaxMana))
    call SetUnitState( TriggUnit, UNIT_STATE_LIFE, TriggUnit_MaxLife)
    call SetUnitState( TriggUnit, UNIT_STATE_MANA, TriggUnit_MaxMana)
    
    set Random_Number=GetRandomInt(1, 3)
    
// (>^.^>) (>^.^>) (>^.^>) (>^.^>) (>^.^>) (>^.^<) (<^.^<)(<^.^<) (<^.^<) (<^.^<) (<^.^<)    
//                             Till here the trigger is done.
//                              Come on! It's allmost done!
// (>^.^>) (>^.^>) (>^.^>) (>^.^>) (>^.^>) (>^.^<) (<^.^<)(<^.^<) (<^.^<) (<^.^<) (<^.^<)    



    if Random_Number==1.0 then
        set HeroAgi = GetHeroAgi(TriggUnit, false)
        call SetHeroAgi( TriggUnit, (HeroAgi + 1) )
     else
    endif

    if Random_Number==2.0 then
        set HeroStr = GetHeroStr(TriggUnit, false)
        call SetHeroStr( TriggUnit, (HeroStr + 1) )
     else
    endif

    if Random_Number==3.0 then
        set HeroInt = GetHeroInt(TriggUnit, false)
        call SetHeroStr( TriggUnit, (HeroInt + 1))
    else
    endif
endfunction

//===========================================================================
function InitTrig_Assigment_Kopieren takes nothing returns nothing
    set gg_trg_Assigment_Kopieren = CreateTrigger(  )
    call TriggerAddCondition( gg_trg_Assigment_Kopieren, Condition( function Trig_Assigment_Conditions ) )
    call TriggerAddAction( gg_trg_Assigment_Kopieren, function Trig_Assigment_Actions )
endfunction
 

Duwenbasden

Ver 6 CREATE energy AS SELECT * FROM u.energy
Reaction score
165
still comparing reals:

JASS:
    if Random_Number==1 then      // dropped the decimal -- compiler will think it is real
                                  // if you have a decimal point
        set HeroAgi = GetHeroAgi(TriggUnit, false)
        call SetHeroAgi( TriggUnit, (HeroAgi + 1) )
     elseif  Random_Number==2 then
        set HeroStr = GetHeroStr(TriggUnit, false)
        call SetHeroStr( TriggUnit, (HeroStr + 1) )
     elseif Random_Number==3 then
        set HeroInt = GetHeroInt(TriggUnit, false)
        call SetHeroStr( TriggUnit, (HeroInt + 1))
    else
        //nop
    endif
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
> But the integer i got now

I still see decimals though: 1.0, 2.0 and 3.0...


JASS has an "elseif" too btw:

if <condition> then
...
elseif <other condition> then
...
else
...
endif
 

Ninja_sheep

Heavy is credit to team!
Reaction score
64
oO, strange. I removed the ".0" now. that worked.
but with the elseifs it crashed!

JASS:
function Trig_Assigment_Conditions takes nothing returns boolean
    if ( not ( IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO) == true ) ) then
        return false
    endif
    return true
endfunction


function Trig_Assigment_Actions takes nothing returns nothing
    local unit TriggUnit = GetTriggerUnit()
    local player TriggPlayer = GetOwningPlayer(udg_TriggUnit)
    local integer TriggPlayer_Wood = GetPlayerState(udg_TriggPlayer, PLAYER_STATE_RESOURCE_LUMBER)
    local integer TriggPlayer_Gold = GetPlayerState(udg_TriggPlayer, PLAYER_STATE_RESOURCE_GOLD)
    local real TriggUnit_MaxLife = GetUnitStateSwap(UNIT_STATE_MAX_LIFE, udg_TriggUnit)
    local real TriggUnit_MaxMana = GetUnitStateSwap(UNIT_STATE_MAX_MANA, udg_TriggUnit)
    local integer HeroAgi
    local integer HeroStr
    local integer HeroInt
    local integer Random_Number
    call SetPlayerState( TriggPlayer, PLAYER_STATE_RESOURCE_GOLD, TriggPlayer_Gold +  R2I(TriggUnit_MaxLife))
    call SetPlayerState( TriggPlayer, PLAYER_STATE_RESOURCE_LUMBER, TriggPlayer_Wood +  R2I(TriggUnit_MaxMana))
    call SetUnitState( TriggUnit, UNIT_STATE_LIFE, TriggUnit_MaxLife)
    call SetUnitState( TriggUnit, UNIT_STATE_MANA, TriggUnit_MaxMana)
    
    set Random_Number=GetRandomInt(1, 3)
    
// (&gt;^.^&gt;) (&gt;^.^&gt;) (&gt;^.^&gt;) (&gt;^.^&gt;) (&gt;^.^&gt;) (&gt;^.^&lt;) (&lt;^.^&lt;)(&lt;^.^&lt;) (&lt;^.^&lt;) (&lt;^.^&lt;) (&lt;^.^&lt;)    
//                             Till here the trigger is done.
//                              Come on! It&#039;s allmost done!
// (&gt;^.^&gt;) (&gt;^.^&gt;) (&gt;^.^&gt;) (&gt;^.^&gt;) (&gt;^.^&gt;) (&gt;^.^&lt;) (&lt;^.^&lt;)(&lt;^.^&lt;) (&lt;^.^&lt;) (&lt;^.^&lt;) (&lt;^.^&lt;)    



    if Random_Number==1 then
        set HeroAgi = GetHeroAgi(TriggUnit, false)
        call SetHeroAgi( TriggUnit, (HeroAgi + 1) )
    elseif Random_Number==2 then
        set HeroStr = GetHeroStr(TriggUnit, false)
        call SetHeroStr( TriggUnit, (HeroStr + 1) )
    elseif Random_Number==3 then
        set HeroInt = GetHeroInt(TriggUnit, false)
        call SetHeroStr( TriggUnit, (HeroInt + 1))
    endif
endfunction
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
That is probably because you use the elseifs.

Persay:
A man named Bobo walks down the street and he meets a magician. The magician puts a red ball into one cup, a green into another, then a blue one into another cup as well. If Bobo chooses one cup, the chance it will be blue is 1/3.

So, if he picks blue, he will win 2 dollars. If he picks red, he will win 5 dollars. If he picks green, he will win 4 dollars.

Since he is only picking one, he cannot pick one and have all three balls. :p rofl

Basically, if you pick a random integer between 1 and 3, the chances getting all of them when only picking one is 0%.

So, what I think is that you should do this instead:
JASS:
    if Random_Number==1 then
        set HeroAgi = GetHeroAgi(TriggUnit, false)
        call SetHeroAgi( TriggUnit, (HeroAgi + 1) )
    endif
    if Random_Number==2 then
        set HeroStr = GetHeroStr(TriggUnit, false)
        call SetHeroStr( TriggUnit, (HeroStr + 1) )
    endif
    if Random_Number==3 then
        set HeroInt = GetHeroInt(TriggUnit, false)
        call SetHeroStr( TriggUnit, (HeroInt + 1))
    endif
endfunction


This is because they are each separate effects, so I think it got a syntax from that. ;)
 

Ninja_sheep

Heavy is credit to team!
Reaction score
64
okaaaaay, here's the smae problem i had before :(
If i use your trigger (remove the elses) = crash :(
thx anyway :(
 

Duwenbasden

Ver 6 CREATE energy AS SELECT * FROM u.energy
Reaction score
165
Code:
local player TriggPlayer = GetOwningPlayer(udg_TriggUnit)
local integer TriggPlayer_Wood = GetPlayerState(udg_TriggPlayer, PLAYER_STATE_RESOURCE_LUMBER)

who are udg_TriggUnit and udg_TriggPlayer?

PurgeandFire: That is a god awful waste of clock cycles. The trigger will compare with all three conditions, instead of 1-3 times when using elseifs. It isn't wrong, just really bad design.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Woah, stupid me to make that mistake. I was thinking something weird at the moment, sorry. :p

@Ninja_Sheep: Remove the "udg_"s! They are not required because you are using locals not globals! lol :D XD
 

Duwenbasden

Ver 6 CREATE energy AS SELECT * FROM u.energy
Reaction score
165
Code:
local player TriggPlayer = GetOwningPlayer(udg_TriggUnit)
local integer TriggPlayer_Wood = GetPlayerState(udg_TriggPlayer, PLAYER_STATE_RESOURCE_LUMBER)

who are udg_TriggUnit and udg_TriggPlayer?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      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