• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Why does this function not work?

Status
Not open for further replies.
Level 6
Joined
Jul 25, 2005
Messages
221
Hiya, I've stumbled upon a mystery of some sorts.

This function should work, should it not?
If I switch the > and < the trigger works perfectly, doing it the other way doesn't... explanations appreciated... :p

JASS:
function CheckMainBools takes nothing returns boolean
    if ( GetTimeOfDay() > 6.00) then
        return true
    endif
    if ( GetTimeOfDay() < 7.00) then
        return true
    endif
    return false
endfunction
function ActionFunc takes nothing returns nothing
    if ( CheckMainBools()) then
        call DisplayTextToForce(GetPlayersAll(), "Past 6 AM, Before 7 AM")
    endif
endfunction
//===========================================================================
function InitTrig_TestTrigger takes nothing returns nothing
    set gg_trg_TestTrigger = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_TestTrigger, 0.10)
    call TriggerAddAction( gg_trg_TestTrigger, function ActionFunc )
endfunction

(Please note that I just until recently learned JASS, so I'm still fairly new at it :cute:)

EDIT: Eh, remove post please, I realised how to do it. :)
 
Level 17
Joined
Apr 13, 2008
Messages
1,608
JASS:
function CheckMainBools takes nothing returns boolean
    if ( GetTimeOfDay() > 6.00) then
        return true
    endif
    if ( GetTimeOfDay() < 7.00) then
        return true
    endif
    return false
endfunction

Nuke this function. You do not need this and it's incorrect anyways.
It will always return true.

To fix the function it should look like this:

JASS:
function CheckMainBools takes nothing returns boolean
    if GetTimeOfDay() > 6.00 and GetTimeOfDay() < 7.00 then
        return true
    endif
    return false
endfunction

BUT.
Always creating a different function for every if statement is just some retardiness that you get when you convert from GUI to JASS.
You should not create a function for every if statement. Not unless you would use that function in many if statements.

The optimal code should look like this:
JASS:
function ActionFunc takes nothing returns nothing
    if GetTimeOfDay() > 6.00 and GetTimeOfDay() < 7.00 then
        call DisplayTextToForce(GetPlayersAll(), "Past 6 AM, Before 7 AM")
    endif
endfunction
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,255
The code is horriadly unefficent.

Rather use the bellow for your sake.

JASS:
function ActionFunc takes nothing returns nothing
    local real tod = GetTimeOfDay()
    if tod > 6 or tod < 7 then
        call DisplayTextToPlayer(GetLocalPlayer(),0,0,"Past 6 AM, Before 7 AM")    
    endif
endfunction

function InitTrig_TestTrigger takes nothing returns nothing
    set gg_trg_TestTrigger = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_TestTrigger, 0.1)
    call TriggerAddAction( gg_trg_TestTrigger, function ActionFunc )
endfunction
 
Level 6
Joined
Jul 25, 2005
Messages
221
emperor_d3st, I see that your way worked most efficiently, also Dr Super Good's, but when I tried doing that earlier it just gave me an error saying I missed alot of ")" when i clearly used the right amount for the right places...

Either way, thanks for optimizing the code, a newb needs to know these things :)

Err... emperor? could I install some sort of button that, every time it is pressed it will give you a notice saying " ShadowMan Needs Help, Log On to THW NOW!"? :grin:

+rep to you both

(need to spread rep before reping you again emperor >.<)
 
Level 17
Joined
Apr 13, 2008
Messages
1,608
Just turn on the bat signal:
Bat-signalBatman_1989.jpg


DSG's version didn't work because of the OR statement.
Using a GetLocalPlayer() instead of the GetPlayersAll() should be more efficient but you should never use the GetLocalPlayer before reading up on it and know every little stuff about it because it causes desyncs.
 
Level 6
Joined
Jul 25, 2005
Messages
221
Why doesn't "or" or "and" statements work? every time I use them the syntax reads it as one sentence >.<

Do you have a good link for the GetLocalPlayer() so I can learn more about it? :p


If I have to use that picture every time I need your help, the posts will be flooded with that signal...
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,255
Just turn on the bat signal:
Bat-signalBatman_1989.jpg


DSG's version didn't work because of the OR statement.
Using a GetLocalPlayer() instead of the GetPlayersAll() should be more efficient but you should never use the GetLocalPlayer before reading up on it and know every little stuff about it because it causes desyncs.

Ok um, "or" is a perfectly valid opperation in JASS just like "and", so why does it make my script not work?

Also the GetLocalPlayer method is what he used before, but that time blizzard was doing it via a BJ (display text to force is a BJ) well here it is direct and more efficent.

Also I know what all of those things I used do, if I did not I would not use them.

P.S. emperor_d3st, your optimization fails to function as a real can not be both bigger than and less than a constant at the same time thus will always return false and so the message will never appear. I am sure you mixed up the opperations there and meant "or" and not "and".
 
Level 17
Joined
Apr 13, 2008
Messages
1,608
Dr Super Good, I am certain you are well aware of all the ups and downs of GetLocalPlayer and I'm well aware that "or" is a perfectly valid operation in JASS, but

local real tod = GetTimeOfDay()
if tod > 6 or tod < 7 then
This means if the time of day is bigger than 6 or smaller than seven do the actions.
That means: if time of day is ANY, then do actions.

This is why "or" is not good here, this statement needs an "and".
That means, if the time of day is bigger than 6 AND less than 7 so anything between 6 and 7.

I know that Blizzard uses the GetLocalPlayer in that BJ, but if you mention it and don't redirect the Op to a tutorial then he won't be aware of the problems coming with GetLocalPlayer() and he will only start to search after he finished his map and players report desyncs all the time - unless he accidentally stumbles upon the //comments on GetLocalPlayer() left by Blizzard in some of their BJs.

Why doesn't "or" or "and" statements work? every time I use them the syntax reads it as one sentence >.<
I don't understand the problem here :p
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,255
Must have miss read his dam greater signs, afterall I copyed them taking into account that the source was correct.

JASS:
function ActionFunc takes nothing returns nothing
    local real tod = GetTimeOfDay()
    if tod < 6 or tod > 7 then
        call DisplayTextToPlayer(GetLocalPlayer(),0,0,"Past 6 AM, Before 7 AM")    
    endif
endfunction

function InitTrig_TestTrigger takes nothing returns nothing
    set gg_trg_TestTrigger = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_TestTrigger, 0.1)
    call TriggerAddAction( gg_trg_TestTrigger, function ActionFunc )
endfunction

The fixed version, and it is still better than "and" as unlike "and", if the first comparision is true it skips the second and thus is less demanding than "and"'s having to compair both times and checking if both are true.

Like I said, I took into account the source he had up there was correct which it was not and so I did not fix it but now I have.

Also why would he use GetLocalPlayer()? If he does not know what it does I dought he would think of using it.
 
Level 5
Joined
Jan 15, 2007
Messages
199
Must have miss read his dam greater signs, afterall I copyed them taking into account that the source was correct.

JASS:
function ActionFunc takes nothing returns nothing
    local real tod = GetTimeOfDay()
    if tod < 6 or tod > 7 then
        call DisplayTextToPlayer(GetLocalPlayer(),0,0,"Past 6 AM, Before 7 AM")    
    endif
endfunction

function InitTrig_TestTrigger takes nothing returns nothing
    set gg_trg_TestTrigger = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_TestTrigger, 0.1)
    call TriggerAddAction( gg_trg_TestTrigger, function ActionFunc )
endfunction

The fixed version, and it is still better than "and" as unlike "and", if the first comparision is true it skips the second and thus is less demanding than "and"'s having to compair both times and checking if both are true.

Like I said, I took into account the source he had up there was correct which it was not and so I did not fix it but now I have.

Also why would he use GetLocalPlayer()? If he does not know what it does I dought he would think of using it.

DSG,
The function you made would be "Before 6 AM or After 7 AM"
JASS:
    if tod < 6 or tod > 7 then
That checks if tod(Time of Day) is less than 6 or greater than 7, it should be less than 7 AND greater than 6

ShadowMan wants it to be "Past 6 AM, Before 7 AM"

The and that emperor used is the proper boolean operator in this case..
 
Level 6
Joined
Jul 25, 2005
Messages
221
I really hope you guys aren't mad OR are now "un-friends" just because of this post.

If you want to blame it on someone blame it on meh :cute:
 
Level 17
Joined
Apr 13, 2008
Messages
1,608
I'm sure there was no offense taken from either sides.
But did you manage to solve this problem?

JASS:
    if GetTimeOfDay() > 6.00 and GetTimeOfDay() < 7.00 then
        call DisplayTextToForce(GetPlayersAll(), "Past 6 AM, Before 7 AM")
    endif

This is the statement that you wanted.
*swap the GetPlayersAll() if you want :)
 
Level 6
Joined
Jul 25, 2005
Messages
221
(Posting code at the bottom of the post)
Yes, I used your example, but I also tried to add an "or" to the function with "and"s...´

Please note that I used the call DisplayTextToForce(GetPlayersAll()/GetLocalPlayer(), "Past 6 AM, Before 7 AM")
just as a sort of test message to see what happened with my trigger. :p

As you can see, I used two if statements instead of one whole... this was and still is my general problem, I don't know how to put boolA and BoolB or BoolC and BoolD, where if boolA and BoolB is true then do actions, or if BoolC and BoolD is true then do actions, into just one if. (This is what I meant with the sentence thingey...)

:fp: Fyi this is used for a power system that doubles the power generated when it is around midday

JASS:
function ActionFunc takes nothing returns nothing
local integer i
local player p
local integer n
    //Here it is :D =========================================================
    // How can I make the two if statements into one?
    if GetTimeOfDay() > 6.00 and GetTimeOfDay() < 11.00 and udg_real_PowerAdd != 1.00 then
    set udg_real_PowerAdd=1.00
    endif
    if GetTimeOfDay() > 13.00 and GetTimeOfDay() < 18.00 and udg_real_PowerAdd != 1.00 then
    set udg_real_PowerAdd=1.00
    endif
    if GetTimeOfDay() > 11.00 and GetTimeOfDay() < 13.00 and udg_real_PowerAdd != 2.00 then
    set udg_real_PowerAdd=2.00
    endif
    //=======================================================================
    set i = 1
    loop
        exitwhen i > 100
        if ( udg_bool_CheckSolarPlantInt[i])== true then
            if (udg_int_SolarPlantEnergy[i]) >= R2I(udg_real_TimeBetweenPower) then
                set udg_int_SolarPlantEnergy[i] = 0
                if ( udg_real_Power < udg_real_MaxPower ) then
                    set udg_real_Power = udg_real_Power + udg_real_PowerAdd 
                    //set p= GetOwningPlayer(udg_unit_SolarPlant[i])
                    //set n= GetConvertedPlayerId( p )
                    //call MultiboardSetItemValueBJ( udg_multiboard_Power[n], 2, 1, I2S(R2I(udg_real_Power)) )
                    //this does not work for some reason...
                    call MultiboardSetItemValueBJ( GetLastCreatedMultiboard(), 2, 1, I2S(R2I(udg_real_Power)) )
                    else
                    set udg_real_Power = udg_real_MaxPower
                    call MultiboardSetItemValueBJ( GetLastCreatedMultiboard(), 2, 1, I2S(R2I(udg_real_Power)) )
                endif
            else
                set udg_int_SolarPlantEnergy[i] = ( udg_int_SolarPlantEnergy[i] + 1 )
            endif
        endif 
        set i = i+1
    endloop
endfunction
//===========================================================================
function InitTrig_TestTrigger takes nothing returns nothing
    set gg_trg_TestTrigger = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_TestTrigger, 0.10)
    call TriggerAddAction( gg_trg_TestTrigger, function ActionFunc )
endfunction
 
Last edited:
Level 17
Joined
Apr 13, 2008
Messages
1,608
Are you sure you want to run this trigger every 0.10 second?

You can glue events to the time of day with this:

JASS:
TriggerRegisterGameStateEvent(trig, GAME_STATE_TIME_OF_DAY, opcode, limitval)


Like this:

JASS:
TriggerRegisterGameStateEvent(SomeTrigger, GAME_STATE_TIME_OF_DAY, EQUAL, 20.00)

I say this solution is much more yummy than running a wall of code every 0.10 sec.
 
Level 6
Joined
Jul 25, 2005
Messages
221
Its not that much of a code, since I use the ifs, they only run once if the udg_real_PowerAdd isn't 1 or 2, and the loop is used for a different thing; it counts up to a certain amount of time and then gives you power accordingly. But you have a good point, I can move the udg_real_PowerAdd time of day checks to a different trigger using that event and save alot of memory, avoiding the ifs checking for time of day all the time :D.
 
Level 5
Joined
Jan 15, 2007
Messages
199
DSG, it says inside his post "Past 6 am, Before 7 am".

This refers to 6:00:00:001 - 6:59:59:999

I am sure that is >6 and <7.. can anyone else confirm this??
 

Rui

Rui

Level 41
Joined
Jan 7, 2005
Messages
7,550
That is what his function is telling you. However, it returns true if the TimeOfDay is inferior to 6AM AND it returns true if it is superior to 7AM. Interval of times is (24:00 - 7:00 + 6:00 - 0:00).
Considering ">6" doesn't check back midnight, 11 PM, etc, the condition should work fine, although it is true that it is not very well coded.
 
Status
Not open for further replies.
Top