Advertisement
Guest User

syntax error issue

a guest
Jan 31st, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.09 KB | None | 0 0
  1.         -- food_values is a dictionary with food prefab names as keys and stat-dictionaries as values.
  2.     -- The stat-dictionaries have stat-names as keys and the effect on each stat as values.
  3.     -- If you want a stat not to be affected, you can just omit it from the stat-dictionary.
  4.     local food_stat_dict = {
  5.         monstermeat = { health = 0, sanity = 0, hunger = 12 },
  6.         cookedmonstermeat = { health = 0, sanity = 0, hunger = 18.75 },
  7.         monsterlasagna = { health = 0, sanity = 0, hunger = 37.5 },
  8.         monstertartare = { health = 3, sanity = 10, hunger = 37.5 },
  9.         jellyfish_dead = { health = 0, sanity = 0, hunger = 10 },
  10.         jellyfish_cooked = { health = 5, sanity = 0, hunger = 18.75 },
  11.         jellyjerky = { health = 0, sanity = 0, hunger = 18.75 },
  12.         durian = { health = -1, sanity = 0, hunger = 20 },
  13.         durian_cooked = { health = 0, sanity = 0, hunger = 20 },
  14.         monstermeat_dried = { health = 0, sanity = 0, hunger = 18.75 },
  15.         meat = { health = 0, sanity = 0, hunger = 10 },
  16.         smallmeat = { health = 0, sanity = 0, hunger = 6.05 },
  17.         plantmeat = { health = 0, sanity = 0, hunger = 8.5 },
  18.         plantmeat_cooked = { health = 0, sanity = 0, hunger = 18.75 },
  19.     }
  20.  
  21.     -- This is the ONLY function you should be making changes to.
  22.     local calculateFoodValues(food)
  23.         -- We want the caller of this function to be told whether our code made changes to the food.
  24.         -- Therefore, we also send back a bool, called changesweremade, which we set to true if we change anything.
  25.         -- In this case it's very simple. If we find the food in our food_stats, we will make changes to it.
  26.         --v THIS LINE IS CAUSING TROUBLE v
  27.         local changesweremade = false  
  28.        
  29.         -- Local variables to hold our food values.
  30.         local healthval, hungerval, sanityval = 0, 0, 0
  31.        
  32.         ---------- ONLY EDIT BELOW THIS LINE ----------
  33.        
  34.         -- HERE you make your changes to the values, if you want to affect this particular food.
  35.        
  36.         -- For this example, we will make use of our food_stat_dict to determine whether to change the food,
  37.         -- and what to change its values to.
  38.        
  39.         -- We look up the food values in our dictionary, using the prefab-variable (prefab identifier)
  40.         -- of the item we are eating.
  41.         local food_stats = food_stat_dict[food.monstermeat],
  42.         local food_stats = food_stat_dict[food.cookedmonstermeat],
  43.         local food_stats = food_stat_dict[food.monsterlasagna],
  44.         local food_stats = food_stat_dict[food.monstertartare],
  45.         local food_stats = food_stat_dict[food.jellyfish_dead],
  46.         local food_stats = food_stat_dict[food.jellyfish_cooked],
  47.         local food_stats = food_stat_dict[food.jellyjerky],
  48.         local food_stats = food_stat_dict[food.durian],
  49.         local food_stats = food_stat_dict[food.durian_cooked],
  50.         local food_stats = food_stat_dict[food.monstermeat_dried],
  51.         local food_stats = food_stat_dict[food.meat],
  52.         local food_stats = food_stat_dict[food.smallmeat],
  53.         local food_stats = food_stat_dict[food.plantmeat],
  54.         local food_stats = food_stat_dict[food.plantmeat_cooked],
  55.        
  56.        
  57.         -- If we found an entry in our food_stat_dict dictionary for the food...
  58.         if food_stats ~= nil then
  59.             -- We indicate that we made changes to the food.
  60.             changesweremade = true
  61.            
  62.             -- Then set our values to whatever is set for them in the dictionary,
  63.             -- and default to 0 if no value is given for the stat.
  64.             healthval = food_stats["health"] or 0
  65.             hungerval = food_stats["hunger"] or 0
  66.             sanityval = food_stats["sanity"] or 0
  67.         end
  68.        
  69.         ---------- ONLY EDIT ABOVE THIS LINE ----------
  70.        
  71.         -- Return the results.
  72.         return changesweremade, healthval, hungerval, sanityval
  73.     end
  74.  
  75.     local old_Eat = inst.components.eater.Eat
  76.     inst.components.eater.Eat =  function(self, food)
  77.         -- Make a local variable holding the edible component of the food (optimization).
  78.         local edible_comp = food.components.edible
  79.  
  80.         -- Make a local variable saying whether we made changes to the food.
  81.         local changesweremade = false
  82.        
  83.         -- If the food has an edible component...
  84.         if edible_comp then
  85.             -- Local variables to hold the new food values.
  86.             local healthval, hungerval, sanityval
  87.            
  88.             -- Calculate the food values, and let us know if changes were made to them.
  89.             changesweremade, healthval, hungerval, sanityval = calculateFoodValues(food)
  90.            
  91.             if changesweremade then
  92.                 -- We first save the original food values, since we want to reset them after changing them temporarily for our character.
  93.                 edible_comp.originalhealthvalue = edible_comp.healthvalue
  94.                 edible_comp.originalhungervalue = edible_comp.hungervalue
  95.                 edible_comp.originalsanityvalue = edible_comp.sanityvalue
  96.                
  97.                 -- We change the food to have our new stat values, and default to 0 if the stat was omitted from the dictionary entry.
  98.                 edible_comp.healthvalue = healthval
  99.                 edible_comp.hungervalue = hungerval
  100.                 edible_comp.sanityvalue = sanityval
  101.             end
  102.         end
  103.        
  104.         -- Call the original Eat function, while the food has our new values, and save the result in a variable.
  105.         local returnvalue = old_Eat(self, food)
  106.        
  107.         -- If we made changes to the food, and the food is still valid (meaning it has not been destroyed
  108.         -- because it was the last in the stack), and the edible component is still accessible...
  109.         if food:IsValid() and changesweremade then
  110.             -- We reset the food values after eating it.
  111.             edible_comp.healthvalue = edible_comp.originalhealthvalue
  112.             edible_comp.hungervalue = edible_comp.originalhungervalue
  113.             edible_comp.sanityvalue = edible_comp.originalsanityvalue
  114.            
  115.             -- Remove the temporary values from the food to save memory.
  116.             edible_comp.originalhealthvalue = nil
  117.             edible_comp.originalhungervalue = nil
  118.             edible_comp.originalsanityvalue = nil
  119.         end
  120.        
  121.         -- Then we return the value returned by the original Eat function.
  122.         return returnvalue
  123.     end
  124.  
  125.     -- This last piece of code will make the food values display correctly when using the popular ShowMe mod.
  126.     inst.FoodValuesChanger = function(player, food)
  127.         local changesweremade, healthval, hungerval, sanityval = calculateFoodValues(food)
  128.         if changesweremade then
  129.             return healthval, hungerval, sanityval
  130.         end
  131.         local e = food.components.edible
  132.         return e.healthvalue, e.hungervalue, e.sanityvalue
  133.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement