Jump to content

[Scripting] Check if position is valid for building


Recommended Posts

What I'm trying to do:

-Make a mod that plants things in a grid formation

 

What I'm missing:

-A function to check if a certain position is valid for building

-A function to get the proper spacing in between different objects (i.e. a bush has a larger 'footprint' compared to a sapling)

 

Uploaded is what I've got in the prefab so far. 

 

I'm new to modding and programming in general so I appreciate the help! Thanks ahead of time.

sapling_cluster.lua

Link to comment
Share on other sites

I thought architectural geometry did this already? You could just install that mod

 

From my understanding, architectural geometry snaps to grid when building. This is great but a problem my friend and I ran into was accidentally clicking on an unbuildable part and having to grab the item from the inventory to plant again. This isn't a big deal when planting one or two things, but in DST, we're playing together and trying to get farms setup to sustain multiple people's crazy rampage of setting traps everywhere for giggles.

 

But what I'm trying to do is make it so you click once and it builds a grid of things.

 

For a point of reference:

THIS is how the architectural geometry works correct?

 

and

 

THIS is what I'm building.

Link to comment
Share on other sites

Well pants, ran into a slight problem due to my implementation. Currently I'm spawning objects into the world after "building" from the recipe. 

 

Example code:

local name = "sapling"local offset = "dug_"..name.components.deployable.min_spacing...local plant = SpawnPrefab("dug_"..name)if plant.CanDeploy(pt)   --dostuffend

It'll give a nil value for the min_spacing bit and an "attempt to call field 'CanDeploy' (a nil value)" for the CanDeploy bit.

 

Since the object the player is holding is not the object that's being planted (it's a buildable thing like a beebox) is there a way to get the min_spacing of an object like a berrybush within the prefab? If so, what's the proper syntax for something like that?

 

I've looked at the geometric placement mod (that's where the min_spacing thing came from) but am a little confused on the nil error.

 

On a related note, is the inst argument just the instance of the object? Like in the example code above, is plant usable as the inst argument?

 

Once again thanks for the help.

 

Link to comment
Share on other sites

On a related note, is the inst argument just the instance of the object? Like in the example code above, is plant usable as the inst argument?
Yes to the first, and no to the second (well, it depends on where you put it). "inst" isn't anything magical, it's just a variable name that is usually used to refer to a constructed prefab.

 

So the error you're running into there is because name is just a String, but you're trying to work with it as if it's an instance created by SpawnPrefab(name). So I think what you should do instead is use the instance of the dug_sapling that is being planted at the center point, then call CanDeploy on that for all the points. If you can deploy, then you can just directly SpawnPrefab("sapling") and move it there. 

Link to comment
Share on other sites

Thanks again rezecib. That clarifies a lot. Unfortunately due to my current implementation, even the original placer isn't a deployable object. I didn't want to create an inventory item since that would quickly create crazy balancing issues (migrate an entire field with ease). But I'll dig deeper into things to get all the details worked out. I've also realized from this whole craziness that I've been doing things in the prefab when I should be making a component, and that I've been looking at the geometric placement mod which has the bulk of the functions in the modmain.lua while I've been poking around in the prefabs.

 

On that note, is there a way to get at the GLOBAL.thingies while inside a prefab/component? Also, is there a way to setup functions and variables in modmain.lua and pass them into a prefab/component as an alternative?

Link to comment
Share on other sites

On that note, is there a way to get at the GLOBAL.thingies while inside a prefab/component? Also, is there a way to setup functions and variables in modmain.lua and pass them into a prefab/component as an alternative?
 GLOBAL.thingies are just "thingies" outside of the modmain. So instead of GLOBAL.TheWorld, just use TheWorld. The reason the modmain is set up differently (a sandbox) is to prevent people who don't know what they're doing from accidentally overwriting important variables in the global space.

 

As for the second one... I'm not sure exactly what you mean. You can replace functions/variables with AddPrefabPostInit and AddComponentPostInit. That's basically all that Geometric Placement does; it replaces a few functions in various things, usually with calls to the original versions of the functions.

Link to comment
Share on other sites

@%1; Blarg another snag, in the Geometric Placements mod, a function is called in modmain.lua

...local function default_test(inst, pt)	local tiletype = GLOBAL.GetGroundTypeAtPosition(pt)...

I'm trying to get at the GetGroundTypeAtPosition() function but from inside a component by trying

...function Clusterplantable:CanPlant(pt, offset)    local tiletype = GetGroundTypeAtPosition(pt)...

as suggested by taking out the GLOBAL in GLOBAL.thingies. But this gives me a "variable GetGroundTypeAtPosition() is not declared" error. Doing a find in files in the scripts I couldn't find this particular function call. I have DS, and DST but not RoG if that's part of the issue.

 

In short where can I find the original function GetGroundTypeAtPosition() and/or how to I get at it from within a component/somewhere not within modmain.lua?

Link to comment
Share on other sites

Thanks again for that clarification rezecib.

 

On an up note, I've found the function I've been looking for so I'll leave the information here for anyone trying to find something similar.

TheWorld.Map:CanDeployPlantAtPoint(pt, inst)--[[Argumentspt - The point you want to checkinst - An instance of the object you want to check itReturn valuesTrue - It's legal for the object to be deployed at the point providedFalse - Either something is in the way at the deployment point or the object passed in is not deployableOther notesMake sure the object being passed in is deployable, i.e. dug_sapling is ok, sapling will return false negative.--]]Example:local thingy = SpawnPrefab("dug_sapling")local pt = {x = 1, y = 2, z = 3}local deploy_OK = TheWorld.Map:CanDeployPlantAtPoint(pt, thingy)

This is from my poking and prodding of the little thing. It may not function exactly as described but there it is.

Link to comment
Share on other sites

@GhostRequiemZX, You should look at Deployable's CanDeploy, which is what calls that. That particular function (CanDeployPlantAtPoint) is only valid for some deployables (which may be okay for your particular use case, but even then you could still use the generalized CanDeploy).

 

Edit: Considering that you have to spawn a dug_sapling to get CanDeployPlantAtPoint to work at all, you could just spawn it and then use inst.components.deployable:CanDeploy(pt). That way you can easily generalize it to other deployables, too.

 

And instead of having to test to figure out how something works, you can use find-in-files to find its code and then read how it works, as long as it isn't going down into the C++ code.

Edited by rezecib
Link to comment
Share on other sites

rezecib Hah, I forgot why I was searching for an alternative in the first place. The first time I used inst.components.deployable:CanDeploy() it give me an error because I used a string instead of the object. After mucking around with other stuff I completely forgot about it. It does indeed work exactly the same.

 

I've been using the find-in-file thing repeatedly, it's more my silly syntax errors that make me wander around finding solution to non-problems than anything. That usually leads to the "kitchen sink" approach of throwing stuff at things and seeing what happens. As I mentioned in my original post, still new to the whole modding/programming thing. Following code is not trivial yet.

 

That said, thanks a lot for the assistance, progress has been made with just a few more little features to go before bug hunting.

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...