Sign in to follow this  
Lokoluna

attempt to index local 'v' (a string value)

Recommended Posts

Lokoluna    50

Hey, I need some quick assistance, I'm totally new to this whole for k, v in pairs stuff so bare with me

So i'm trying to lower the deployed space, and placement to numbers so I can plant plants close together

local prefabs =
{
	"pinecone",
	"seeds",
	"pinecone_sapling",
	"acorn",
	"acorn_sapling",
	"marblebean",
	"twiggy_nut",
	"twiggy_nut_sapling",
	"butterfly",
	"planted_flower",
	"dug_grass",
	"dug_sapling",
	"grass",
	"sapling",
	
}

for _, v in pairs(prefabs) do
	if type(v.min_spacing) ~= "number" or v.min_spacing > 1 then
		v.min_spacing = 0
	end
end

This is what I have, and it simply tells me "attempt to index local 'v' (a string value)" Which I don't understand, why is it counting it as a string?

I'm not sure if it's supposed to be "for k, v" or what but.. regardless it all has the same outcome..

Edited by Lokoluna

Share this post


Link to post
Share on other sites
Thomas Die    902

I'm not sure how you're getting the spacing but because it's a list it's not the actual the prefabs just the names

so it's only comparing the strings
So instead of using a list it'd be better to covert them as a prefab using 

local prefab = SpawnPrefab(v) 
this get the code from the prefab itself but still using the list

Share this post


Link to post
Share on other sites
Lokoluna    50
4 hours ago, thomas4846 said:

I'm not sure how you're getting the spacing but because it's a list it's not the actual the prefabs just the names

so it's only comparing the strings
So instead of using a list it'd be better to covert them as a prefab using 

local prefab = SpawnPrefab(v) 
this get the code from the prefab itself but still using the list

So this actually takes I gues you can say, calls the real prefab i'm referencing?

So I will have to make "local prefab = SpawnPrefab(v)" about 12 times? Or I'm not entirely sure what to do wit hthis code you're giving me, Can you explain into further detrail?

Share this post


Link to post
Share on other sites
Thomas Die    902
for _, v in pairs(prefabs) do
    local prefab = SpawnPrefab(v)
	if type(prefab.min_spacing) ~= "number" or prefab.min_spacing > 1 then
		prefab.min_spacing = 0
	end
end



just include it in the loop after "do" so that everytime it loops the local prefab will replaced through each iteration in the list

prefab should change each time the loop happens because it's local i think, maybe.. hopefully 
 

Edited by thomas4846

Share this post


Link to post
Share on other sites
Lokoluna    50
3 hours ago, thomas4846 said:

snip

Well it was a good idea, but alas it comes back as a nil value, 

modmain.lua"]:21: attempt to call global 'SpawnPrefab' (a nil value)

 

Share this post


Link to post
Share on other sites
-t-    266

I think you're going about this thing the wrong way. Since you want to edit a prefab to change the deploy spacing you should probably use AddPrefabPostInit. This is by far the easiest way you can do it in my opinion. Such code would look like this:

local deployables = { -- Table with all of the prefabs
	"pinecone", -- pinecone_sapling, acorn_sapling, twiggy_nut_sapling, planted_flower, grass and sapling
	"seeds",	  -- are not deployables so they don't have to be in here
	"acorn",	  -- you only change the prefabs that are deployable
	"marblebean",
	"twiggy_nut",
	"butterfly",
	"dug_grass",
	"dug_sapling",
}

for k, v in pairs(deployables) do -- The 'for' statement, checks through every value in the 'deployables' table
	AddPrefabPostInit(v, function(inst) -- A function responsible for overriding prefabs
		if inst.components.deployable then -- Check if the deployable component isn't 'nil'
			inst.components.deployable:SetDeploySpacing(value) -- Set the value to whatever you want
		end
	end)
end

The only problem I see with this solution is that every prefab in that list will have the same spacing but other than that, this is the fastest, simplest solution, as far as I know.

  • Like 1

Share this post


Link to post
Share on other sites
Lokoluna    50
3 hours ago, -t- said:

I think you're going about this thing the wrong way. Since you want to edit a prefab to change the deploy spacing you should probably use AddPrefabPostInit. This is by far the easiest way you can do it in my opinion. Such code would look like this:


local deployables = { -- Table with all of the prefabs
	"pinecone", -- pinecone_sapling, acorn_sapling, twiggy_nut_sapling, planted_flower, grass and sapling
	"seeds",	  -- are not deployables so they don't have to be in here
	"acorn",	  -- you only change the prefabs that are deployable
	"marblebean",
	"twiggy_nut",
	"butterfly",
	"dug_grass",
	"dug_sapling",
}

for k, v in pairs(deployables) do -- The 'for' statement, checks through every value in the 'deployables' table
	AddPrefabPostInit(v, function(inst) -- A function responsible for overriding prefabs
		if inst.components.deployable then -- Check if the deployable component isn't 'nil'
			inst.components.deployable:SetDeploySpacing(value) -- Set the value to whatever you want
		end
	end)
end

The only problem I see with this solution is that every prefab in that list will have the same spacing but other than that, this is the fastest, simplest solution, as far as I know.

Ooh, this makes much more sense, Thank you for describing everything, and waht they do- So I do not need ie. "pinecone_Sapling" as they aren't deplyoables right? They are just the plant, sorry about that.

For example though I could per say do this in addition to what you gave 

 

for k, v in pairs(deployables) do -- The 'for' statement, checks through every value in the 'deployables' table
	AddPrefabPostInit(v, function(inst) -- A function responsible for overriding prefabs
		if inst.components.deployable then -- Check if the deployable component isn't 'nil'
			inst.components.deployable:SetDeploySpacing(value) -- Set the value to whatever you want
		else 
			inst.components.AddComponent("deployable") then inst.components.deployable:SetDeploySpacing(value) --This, Could I do this? I know it may be wrong as I'm jsut typing based on what information I have in my brain atm.
		end
	end)
end

Thank you again for helping me, both of you! I appreciate it a lot! I didn't know I could use Addprefabpostinit to edit values, I thought it could only add, and or remove something

Edited by Lokoluna

Share this post


Link to post
Share on other sites
-t-    266

The 'if inst.components.deployable then' is just to avoid an error if the value is 'nil'. There isn't anything you have to do with it. You don't have to add an 'else' statement. But if you were to add it, it would look like this:

else
	inst.components:AddComponent("deployable") -- You have to remember, when calling a function, which in this case 'AddComponent' is a function, you have to use a colon, not a dot.
	inst.components.deployable::SetDeploySpacing(value) -- And of course 'then' is only used in an 'if' statement and should be placed after the condition
end

 

  • Like 1

Share this post


Link to post
Share on other sites
Lokoluna    50

Thank  you, i'm sorry maybe i gave off wrong information, I was just simply asking If i wanted to add an else statement like I did, would that be an okay way to do it? I know the if statement is just checking if the such thing is true, or if not

I just wanted to know If I could use an else statement like that, So during an else, I cannot use then? Could I use something else to describe what I want it to do, or will it just do it in the prior if statement returns false?

 

if inst.components.deployable then -- Check if the deployable component isn't 'nil'
			inst.components.deployable:SetDeploySpacing(value) -- Set the value to whatever you want
		else 
			inst.components.AddComponent:("deployable")
			inst.components.deployable:SetDeploySpacing(value)
end

I was simply asking if I could use what I used here, minus the error with the colon and then matters, Also here you used 

inst.components.deployable::SetDeploySpacing

Why is there two colons? Maybe it was a typo, sorry if it was.

Share this post


Link to post
Share on other sites
-t-    266

Yeah, if the 'if' statement turns out false, the code after 'else' is run.

And, yes, that double colon is a typo, my bad.

Edited by -t-
  • Like 1
  • Health 1

Share this post


Link to post
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
Sign in to follow this