Jump to content

check moon phase


Recommended Posts

So I have an item that I would like to have a different effect based on the phase of the moon.

I am trying to set a local variable to the current moon phase-

    local phase = GetClock():GetMoonPhase()

gives this error:

.../scripts/prefabs/mooncake_simple.lua:38: attempt to index a nil value

Link to comment
Share on other sites

GetClock() may return nil if the world doesn't have the clock component yet.

Perhaps this may fix your issue?  You may need to handle the "N/A" case in your future code to follow for when there's no clock yet.

local clock = GetClock()
local phase = clock and clock:GetMoonPhase() or "N/A"

 

Link to comment
Share on other sites

1 hour ago, CarlZalph said:

GetClock() may return nil if the world doesn't have the clock component yet.

Perhaps this may fix your issue?  You may need to handle the "N/A" case in your future code to follow for when there's no clock yet.


local clock = GetClock()
local phase = clock and clock:GetMoonPhase() or "N/A"

 

First, thank you, I've been struggling with this for several hours.

Now it doesn't crash on game launch, but still crashes out at world load...Well - its an improvement...

Here is what I have ...

Spoiler

 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

local function SetPhase()
    local W101_CLOCK = GetClock()
    local W101_PHASE = W101_CLOCK and GetClock():GetMoonPhase() or "_simple"
    print ("phase is "..tostring(W101_PHASE)) -- This prints to the log file correctly
end

.. <snip> extra code removed

local function OnMoonPhaseChange(inst)

    SetPhase()
        
    if W101_PHASE == "full" then -- this part of the function fails with error
        inst.Light:SetDisableOnSceneRemoval(false)
    else
        inst.Light:SetDisableOnSceneRemoval(true)
    end

    local lightstate = lightstate[W101_PHASE]
    print ("Set light to intensity "..tostring(lightstate.brightness))
    inst.Light:SetIntensity(lightstate.brightness)
    inst.Light:Enable(lightstate.enabled)
    inst.Light:SetRadius(lightstate.radius)
    
    UpdateImages(inst)

end

 

 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

I addded a line to print the W101_PHASE variable
print ("phase is "..(W101_PHASE)) -- this is in prefab.lua to confirm W101_PHASE is behaving properly

[00:01:06]: ../mods/waiter-381565292/scripts/prefabs/mooncake_simple.lua(41,1) phase is new
[00:01:06]: ...waiter-381565292/scripts/prefabs/mooncake_simple.lua:77: variable 'W101_PHASE' is not declared

It literally prints the variable, and then states the variable is not declared.

Link to comment
Share on other sites

Ah, well you're trying to use a variable that is defined in the local scope of a function.

 

local function SetPhase()
    local W101_CLOCK = GetClock()
    local W101_PHASE = W101_CLOCK and GetClock():GetMoonPhase() or "_simple"
    print ("phase is "..tostring(W101_PHASE)) -- This prints to the log file correctly
    return W101_PHASE
end 
    local W101_PHASE = SetPhase()
        
    if W101_PHASE == "full" then -- this part of the function fails with error

 

The issue is that once the function exits, any local variable poofs into the memory void for the garbage collector to remove, unable to be accessed.  This is called scope, and something you'll need to be familiar with if you plan on chunking things up into functions.

Link to comment
Share on other sites

Yep it's a scope issue.  Carl explains it well.  Make SetPhase() return the variable and pass it over.  

local function SetPhase()
    local W101_CLOCK = GetClock()
    local W101_PHASE = W101_CLOCK and GetClock():GetMoonPhase() or "_simple"
    print ("phase is "..tostring(W101_PHASE)) -- This prints to the log file correctly

return W101_PHASE --return the variable
end

.. <snip> extra code removed

local function OnMoonPhaseChange(inst)

local W101_PHASE = SetPhase() --stores the variable where OnMoonPhaseChange() can access it
        
    if W101_PHASE == "full" then -- this part of the function fails with error
        inst.Light:SetDisableOnSceneRemoval(false)
    else
        inst.Light:SetDisableOnSceneRemoval(true)
    end

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...