My code is broken while trying to fix it?

Discussion about Mods for Prison Architect

Moderator: NBJeff

GamingBud
level2
level2
Posts: 194
Joined: Mon Dec 29, 2014 8:33 pm

My code is broken while trying to fix it?

Postby GamingBud » Mon Feb 02, 2015 10:25 pm

So a commentor said that my mod slowed down very large prisons dramatically, so i decided to add a timing mechanism to the code so that only every two seconds, it would search for prisoners to do what not, instead of searching every split second.

Code: Select all

function Create()
    Object.SetProperty( "Stock", 32.0); 
    Object.SetProperty("StockType", "WaterBottle" );
    Object.SetProperty("NeedsLoading", false);
   Object.SetProperty("Count", 0.0);
   Object.SetProperty("CurrentDrink", "WaterBottle");
   Object.SetProperty("IsLoaded", true);
end

function Update( timePassed )

   if Object.GetProperty("IsLoaded") == nil then
      Create();
   end
   
   local currentStock = tonumber(Object.GetProperty("Stock"));
   local count = tonumber(Object.GetProperty("Count"));
   local nearbyPrisoner = this.GetNearbyObjects("Prisoner",10);
   local pCounter = 0
   
   count = count + timePassed;
   Object.SetProperty("Count", count);
   
   if count > 10.0 then
      if currentStock > 0.0 then
      nearbyPrisoner = this.GetNearbyObjects("Prisoner",10)
         if pCounter > 0 then
             Object.GetProperty("CurrentDrink");
             local currentDrink = Object.Spawn( "WaterBottle", Object.GetProperty("Pos.x"), Object.GetProperty("Pos.y"));
             Object.SetProperty("CurrentDrink", currentDrink);
             Object.SetProperty( "Stock", currentStock - 1 );   
             end
          end
      end
      
      Object.SetProperty("Count", 0.0);
   
   if currentStock < 1.0 then
      Object.SetProperty( "NeedsLoading", true );
      end
   
   this.Tooltip = tostring(Object.GetProperty("Stock")) .. " Water Bottles Left."
   
    if Object.GetProperty("NeedsLoading") then
      Object.GetProperty("CurrentDrink");
      Object.SetProperty("CurrentDrink", "WaterBottle");
      Object.CreateJob("LoadCooler");
        Object.SetProperty("Tooltip", "job_custom_loadcooler");
    end

   if count > 2.0 then
       nearbyPrisoner = this.GetNearbyObjects("Prisoner",10)
      if count > 4.0 then
         nearbyPrisoner = this.GetNearbyObjects("Prisoner",10)
           if count > 6.0 then
               nearbyPrisoner = this.GetNearbyObjects("Prisoner",10)
              if count > 8.0 then
                 nearbyPrisoner = this.GetNearbyObjects("Prisoner",10)
               end
            end
         end
      end
   
   for table, distance in pairs(nearbyPrisoner) do
      pCounter = pCounter + 1
      end
end


It doesn't say there is a problem, but the water cooler no longer spouts out water bottle, AND slows down the game now dramatically when there are only 16 prisoners -.- What have i done??
Puman
level1
level1
Posts: 48
Joined: Fri Aug 29, 2014 1:21 pm

Re: My code is broken while trying to fix it?

Postby Puman » Mon Feb 02, 2015 11:43 pm

GamingBud wrote:It doesn't say there is a problem, but the water cooler no longer spouts out water bottle, AND slows down the game now dramatically when there are only 16 prisoners -.- What have i done??

You are doing GetNearbyObjects (an expensive call which you should do rarely) 1 to 6 times every Update.

Here is a snippet for how to properly not call everything every update.

Code: Select all

local timer = 0
local nextTimeout = 0.1

function Update(timePassed)
    timer = timer + timePassed;
    if timer > nextTimeout then
        nextTimeout = IntervalUpdate(timer)
        timer = 0
    end
end

function IntervalUpdate(timePassed)
    -- put your code here   
   
    -- then return the amount of time until next pass here
    return 10
end
GamingBud
level2
level2
Posts: 194
Joined: Mon Dec 29, 2014 8:33 pm

Re: My code is broken while trying to fix it?

Postby GamingBud » Tue Feb 03, 2015 1:32 am

What do you mean by "-- then return the amount of time until next pass here"?

A: The "return 10" line is what this means

B: I should put when i want the timer to reset here?
Puman
level1
level1
Posts: 48
Joined: Fri Aug 29, 2014 1:21 pm

Re: My code is broken while trying to fix it?

Postby Puman » Tue Feb 03, 2015 6:12 pm

GamingBud wrote:What do you mean by "-- then return the amount of time until next pass here"?

In your case returning 2 would give you what you intended to do with your own code, then it will be called every 2.0 'seconds' (timePassed is a flexible concept in PA that is dependant on map size making it difficult to tell exactly how much time 2.0 will be in the game)
GamingBud
level2
level2
Posts: 194
Joined: Mon Dec 29, 2014 8:33 pm

Re: My code is broken while trying to fix it?

Postby GamingBud » Tue Feb 03, 2015 7:05 pm

Ok, thanks. But now, about the "WaterBottle" still not spawning?

Here is the code now with what you gave me.

Code: Select all

function Create()
    Object.SetProperty( "Stock", 32.0); 
    Object.SetProperty("StockType", "WaterBottle" );
    Object.SetProperty("NeedsLoading", false);
   Object.SetProperty("Count", 0.0);
   Object.SetProperty("CurrentDrink", "WaterBottle");
   Object.SetProperty("IsLoaded", true);
end

function Update( timePassed )

   if Object.GetProperty("IsLoaded") == nil then
      Create();
   end
   
   local currentStock = tonumber(Object.GetProperty("Stock"));
   local count = tonumber(Object.GetProperty("Count"));
        -- This is just so that it gets a value when loading pCounter. This only sets it once (i think) which is what i want.
   local nearbyPrisoner = this.GetNearbyObjects("Prisoner",10);
   local pCounter = 0
   
   count = count + timePassed;
   Object.SetProperty("Count", count);
   
        -- This spawns a water bottle if there is more than one prisoner nearby (pCounter)
   if count > 10.0 then
      if currentStock > 0.0 then
         if pCounter > 0 then
             Object.GetProperty("CurrentDrink");
             local currentDrink = Object.Spawn( "WaterBottle", Object.GetProperty("Pos.x"), Object.GetProperty("Pos.y"));
             Object.SetProperty("CurrentDrink", currentDrink);
             Object.SetProperty( "Stock", currentStock - 1 );
            Object.SetProperty("Count", 0.0);
          end
      end
    end
   
   if currentStock < 1.0 then
      Object.SetProperty( "NeedsLoading", true );
      end
   
        -- This just says how many bottles are left till a cook needs to restock it.
   this.Tooltip = tostring(Object.GetProperty("Stock")) .. " Water Bottles Left."
   
    -- This says "Loading Water Cooler".
    if Object.GetProperty("NeedsLoading") then
      Object.GetProperty("CurrentDrink");
      Object.SetProperty("CurrentDrink", "WaterBottle");
      Object.CreateJob("LoadCooler");
        Object.SetProperty("Tooltip", "job_custom_loadcooler");
    end
       
        -- This counts prisoners.
   for table, distance in pairs(nearbyPrisoner) do
      pCounter = pCounter + 1
      end

local timer = 0
local nextTimeout = 2

    timer = timer + timePassed;
    if timer > nextTimeout then
        nextTimeout = IntervalUpdate(timer)
        timer = 0
    end
end

function IntervalUpdate(timePassed)
    nearbyPrisoner = this.GetNearbyObjects("Prisoner", 10)
   return 2.0
end
GamingBud
level2
level2
Posts: 194
Joined: Mon Dec 29, 2014 8:33 pm

Re: My code is broken while trying to fix it?

Postby GamingBud » Wed Feb 04, 2015 4:02 am

Lot of activity elsewhere, but i need to update this because people are waiting on me (I updated without testing, fool on me) ^

Return to “Modding”

Who is online

Users browsing this forum: No registered users and 3 guests