Jump to content

[SOLVED] Issues with GetAgeInDays() function causing crashes


Recommended Posts

I'm currently having a problem with my code within the modmain of a custom character I was creating and I was able to isolate it down to this line here:

if  v.components.age:GetAgeInDays() % daysRequired = 0 then

 

What I'm trying to accomplish is a simple RNG item giver to a specific character, but only when their time alive in the world fully divides (nothing in the decimal places) into the required day count (determined by the config data of 'gem_generation'). However, when it comes to running the code below, I always receive the same results from the game. For the line I isolated above, it always crashes saying that "then is expected near =" for said line. I'd appreciate any assistance in finding out the issue as well as how to solve it.

 

The full code:

if GetModConfigData('gem_generation') > 0 then
	local function GiveGems()
		for i,v in ipairs(AllPlayers) do
			local daysRequired = tonumber(GetModConfigData('gem_generation'))
			local playerAge = tonumber(v.components.age:GetAgeInDays())

			-- If the player is example, has more than 0 health (isn't dead) and the age of the player meets the generation requirements 
			if (v:HasTag("example")) and (v.components.health.currenthealth > 0) and (playerAge > 1) and not v:HasTag("playerghost") then
				if  v.components.age:GetAgeInDays() % daysRequired = 0 then 
					--Gives the player a random gem
					local RNG = math.random(1,36)
					if RNG < 7 then
						v.components.inventory:GiveItem("redgem")
					elseif RNG >= 7 and RNG < 13 then
						v.components.inventory:GiveItem("orangegem")
					elseif RNG >= 13 and RNG < 19 then
						v.components.inventory:GiveItem("yellowgem")
					elseif RNG >= 19 and RNG < 25 then
						v.components.inventory:GiveItem("greengem")
					elseif RNG >= 25 and RNG < 31 then
						v.components.inventory:GiveItem("bluegem")
					else
						v.components.inventory:GiveItem("purplegem")
					end
				end
			end
		end
	end
	-- Runs GiveGems
	local function OnDayComplete(inst,day)
		GiveGems()
	end
	-- Runs GiveGems at the start of a new day
	AddWorldPostInit(function(w)
		w:WatchWorldState("cycles", OnDayComplete)
	end)
end

 

Edited by LordBritish
Problem solved. On to the next one!
Link to comment
Share on other sites

1 hour ago, Serpens said:

In an " if x equals y" statement, you need "==" instead of "="

I wish it were that easy, but everytime I try that it still crashes, however without a crash log. Maybe there's something else wrong with the code I'm not seeing.

Link to comment
Share on other sites

23 minutes ago, LordBritish said:

I wish it were that easy, but everytime I try that it still crashes, however without a crash log. Maybe there's something else wrong with the code I'm not seeing.

even if there is no direct error screen, there will still be an error report in your logfile.

here you see where to find logfile :

The logfiles resets everytime you restart the whole game.

Edited by Serpens
Link to comment
Share on other sites

A little late for doing this, but I figured on adding in the final code in the event someone would like to use it for a future mod project. Namely due to the code I posted lacking some vital functions that will let it work as normal on a server.

 

And sorry if this is a little cluttered, I salvaged the code from a separate mod that gave a specific item to everyone on the server, simply modifying it to suit my character's needs.

-- This complicated bit is needed to add our code into the world's prefab
local world_init_fns = {}
local function AddWorldPostInit(fn)
	table.insert(world_init_fns,fn)
end
local was_forest
local function world_init(inst)
	if was_forest then
		return
	end
	was_forest = true
	for i=1,#world_init_fns do
		world_init_fns[i](inst)
	end
end
AddPrefabPostInit("world",world_init)

-- Random item pool for reference
local random_items = {
	items =
	{
		"axe",
		-- Insert whatever item prefabs you wish here, like above
	},
}

--Checks to see if item generation is disabled.
--(item_generation is a mod configuration option that chooses how many days a player must be alive for, set to 0 disables the perk)
if GetModConfigData('item_generation') > 0 then
	local function GiveItems()
		local AllPlayers = GLOBAL.AllPlayers

		-- Influences every individual player on the server with an easy to use ipairs function
		for i,v in ipairs(AllPlayers) do
			-- A lot of these small if statements are here to prevent potential crashes. Call me paranoid, but this is for DST.
			if not v.components.age then
				v:AddComponent("age")    
			end
		
			-- Sets our day intervals and player age. Player age is +1 since age starts at 0 while cycles don't.
			local daysRequired = GetModConfigData('item_generation')
			local playerAge = v.components.age:GetAgeInDays() + 1

			-- If this is for a specific character, keep this if. If it is for all players as a server mod, remove this if 
			if v:HasTag("exampleTag") then
				-- Ghosts don't get this benefit.
				if not v:HasTag("playerghost") then
					-- If the player's age divides completely into the day interval set before, give that man an item.
					if  playerAge % daysRequired == 0 and playerAge >= 1 then 
						--Gives the player a random item
						if not v.components.inventory then
							v:AddComponent("inventory")    
						end

						-- Picks 1 random item from the table set above. Can be changed to grab more than 1 without duplicates
						local starting_items = GLOBAL.PickSome( 1, random_items.items )
						for i, r in pairs(starting_items) do
							-- Takes every individual item and gives it to the player.
							local item = GLOBAL.SpawnPrefab(r)
							if item ~= nil then
								v.components.inventory:GiveItem( item )
								--v.components.talker:Say("Got item!")
							end
						end
					end
				end
			end
		end
	end
	-- Runs GiveItems
	local function OnDayComplete(inst,day)
		GiveItems()
	end
	-- Runs GiveItems at the start of a new day
	AddWorldPostInit(function(w)
		w:WatchWorldState("cycles", OnDayComplete)
	end)
end

Hopefully it will be void of crashes and bugs in-game...

 

...or not and it erases your inventory visually...

Edited by LordBritish
Stuff happens
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...