Jump to content

[CODE] X and Y axis- local function


Recommended Posts

Hello everyone. I am wondering how to make an event when something goes in X or Y axis in the DST world. For example chain of explosions (SpawnPrefab code) going from left to right or up to down. I made this code earlier by myself (just coppied some codelines from books of Wickerbottom). Maybe someone knows what should I change to make it linear, because right now it spawns prefabs randomly within certain area. Oh, and I have got sub-question for this, as you see in .lua below character must eat item with this prefab to activate it, I have tried to make it triggered with "OnHit" and error occured (yes, I made throwable weapon with parts of this prefab).

 

local Assets =
{
	Asset("ANIM", "anim/barrage.zip"), 
    Asset("ATLAS", "images/inventoryimages/barrage.xml"),    
	Asset("IMAGE", "images/inventoryimages/barrage.tex")	
}

local prefabs = 
{
 "explode", "shell"
}


local function barragecall(inst, target)
            local pt = target:GetPosition()
            local numtentacles = 50

           		   
            target:StartThread(function()
                for k = 1, numtentacles do
                    local theta = math.random() * 2 * PI
                    local radius = math.random(3, 8)
                    
					
                    -- we have to special case this one because birds can't land on creep
                    local result_offset = FindValidPositionByFan(theta, radius, 12, function(offset)
                        local pos = pt + offset
                        --NOTE: The first search includes invisible entities
                        return #TheSim:FindEntities(pos.x, 0, pos.z, 1, nil, { "INLIMBO", "FX" }) <= 0
                            and TheWorld.Map:IsDeployPointClear(pos, nil, 1)
                    end)

                   					
					
					 if result_offset ~= nil then
                        local x, z = pt.x + result_offset.x, pt.z + result_offset.z
                        local tentacle = SpawnPrefab("explode")												
                        tentacle.Transform:SetPosition(x, 0, z)
						local tentacle = SpawnPrefab("shell")												
                        tentacle.Transform:SetPosition(x, 0, z)
                        
						

                    end

                    Sleep(.5)
                end
            end)
			
	
end


local function fn(Sim)
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddNetwork()
	inst.entity:AddSoundEmitter()

    MakeInventoryPhysics(inst)
    
    inst.AnimState:SetBank("barrage")
    inst.AnimState:SetBuild("barrage")
    inst.AnimState:PlayAnimation("idle", true)    


	  
	inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
		
	
    inst:AddComponent("sanityaura")
    inst.components.sanityaura.aura = -TUNING.SANITYAURA_MED
	

		
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/inventoryimages/barrage.xml"
	--inst.components.inventoryitem:OnDropFn(barragecall) 
	
	inst:AddComponent("inspectable")
	
	MakeHauntableLaunch(inst)
	
	inst:AddComponent("edible")
	inst.components.edible:SetOnEatenFn(barragecall)
	
  
	
	

    
    return inst
end

return Prefab( "common/inventory/barrage", fn, Assets)

 

Link to comment
Share on other sites

If you are just making a line for explosions which don't really care about what they are spawning on.

--This should get you started
--local x, y, z = inst.Transform:GetWorldPosition()
--draw_line(inst, x, z, x+math.random(-10, 10), z+math.random(-10,10), 10)
local function draw_line(inst, startx, startz, endx, endz, n)
	if startx and startz and endx and endz and n then
		local x = startx
		local z = startz
		for i = 0, n, 1 do
			SpawnPrefab("explode_small").Transform:SetPosition(x,0,z)
			x = x + (endx-startx)/n
			z = z + (endz-startz)/n
		end
	end
end

This is just to get you started, you can adjust the inputs to be more what you want and adjust the spawn to have delays etc. in your own edits.

Link to comment
Share on other sites

I am not able to call this event into world. When I consume this prefab it just disappers without any effect or it crashes due to "stack overflow" on this line :

local x, y, z = inst.Transform:GetWorldPosition()

Whole section looks like this:

Quote

local function draw_line(inst, startx, startz, endx, endz, n)
local x, y, z = inst.Transform:GetWorldPosition()
draw_line(inst, x, z, x+math.random(-10, 10), z+math.random(-10,10), 10)
   local numtentacles = 3
    if startx and startz and endx and endz and n then
        local x = startx
        local z = startz
        for i = 0, n, 1 do
            SpawnPrefab("shell").Transform:SetPosition(x,0,z)
            x = x + (endx-startx)/n
            z = z + (endz-startz)/n
        end
    end

    
end

I don't know why you left those 2 lines with "--" in front of it. Maybe I should not add them to code, but code didn't work at all without it. In the other hand when I add those 2 lines it crashes.

--local x, y, z = inst.Transform:GetWorldPosition()
--draw_line(inst, x, z, x+math.random(-10, 10), z+math.random(-10,10), 10)

 

 

 

 

Link to comment
Share on other sites

The code he commented out, calls the draw_line function defined below it, which draws a line from the world position, to a randomized other point. Normally you cannot call functions which haven't been declared yet, so you should move the two commented out lines down below the function, to stop his code from crashing when uncommenting those two lines. That's why he commented them out. He just wanted to show you the function example, and an example of how to call the function.

It would help, if you could tell us the crash report. It's in the server log, usually at the bottom, or around where the mods are being loaded in.

That said, it looks like you bastardized his code completely. You moved the call to the draw_line function INTO the draw_line function, which makes it execute the function recursively, resulting in a stackoverflow. Remove the draw_line function call, and you're good.

I would expect it to also crash on the line:

SpawnPrefab("shell").Transform:SetPosition(x,0,z)

...since I believe SpawnPrefab is a global function, meaning you might need to do:

GLOBAL.SpawnPrefab("shell").Transform:SetPosition(x,0,z)

Edited by Ultroman
Link to comment
Share on other sites

Whole prefab file looks like this inside:

local Assets =
{
	Asset("ANIM", "anim/barrage.zip"), 
    Asset("ATLAS", "images/inventoryimages/barrage.xml"),    
	Asset("IMAGE", "images/inventoryimages/barrage.tex")	
}

local prefabs = 
{
 "explode", "shell"
}



local function draw_line(inst, startx, startz, endx, endz, n)
   local numtentacles = 3
	if startx and startz and endx and endz and n then
		local x = startx
		local z = startz
		for i = 0, n, 1 do
			GLOBAL.SpawnPrefab("shell").Transform:SetPosition(x,0,z)
			x = x + (endx-startx)/n
			z = z + (endz-startz)/n
		end
	end

	
end
--local x, y, z = inst.Transform:GetWorldPosition()
--draw_line(inst, x, z, x+math.random(-10, 10), z+math.random(-10,10), 10)


local function fn(Sim)
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddNetwork()
	inst.entity:AddSoundEmitter()

    MakeInventoryPhysics(inst)
    
    inst.AnimState:SetBank("barrage")
    inst.AnimState:SetBuild("barrage")
    inst.AnimState:PlayAnimation("idle", true)    


	  
	inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
		
	
    inst:AddComponent("sanityaura")
    inst.components.sanityaura.aura = -TUNING.SANITYAURA_MED
	

		
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/inventoryimages/barrage.xml"
	--inst.components.inventoryitem:OnDropFn(barragecall) 
	
	inst:AddComponent("inspectable")
	
	MakeHauntableLaunch(inst)
	
	inst:AddComponent("edible")
	inst.components.edible:SetOnEatenFn(draw_line)
	
  
	
	

    
    return inst
end

return Prefab( "common/inventory/barrage", fn, Assets)

It doesn't crash but it also doesn't work.

Link to comment
Share on other sites

The draw_line function doesn't properly look like the function expected by SetOnEatenFn. The parameter list needs to match the parameters of the oneaten function-call in the Edible component's OnEaten function. It only takes an instance and an "eater" parameter, while yours has several.

It seems like you're not grasping the basics of lua-scripting. You should do some tutorials before starting to mod a complex game like DST.

Edited by Ultroman
Link to comment
Share on other sites

local function spawnShell(x, z)
	GLOBAL.SpawnPrefab("shell").Transform:SetPosition(x,0,z)
end

local function onEaten(inst, eater)
	local pos = inst:GetPosition()
	spawnShell(pos.x, pos.z)
end

Place that before the line with "local function fn(Sim)", and instead of this in your fn(Sim) function:

inst.components.edible:SetOnEatenFn(draw_line)

do this:

inst.components.edible:SetOnEatenFn(onEaten)

Though I doubt this is what you're actually trying to do. The onEaten function isn't the same as e.g. onThrown or onDeath. I would really recommend doing a couple of tutorials and reading the lua documentation, before going to war with DST. Also, just reading other mods really teaches you a lot.

Edited by Ultroman
Link to comment
Share on other sites

Thank  you. It works now, but only one artillery shell is falling from the sky. I will figure it out myself. I know that I am miserable at modding. I have tried learning lua programming, even basics, but I didn't know where to start. In my opinion it doesn't layer the DST prefab files, it is so bizzare to me. I also watched and read lots of tutorials, as you see it didn't help me at all. I think the best solution is just to stop this circus. I just need to complete my current project and then I won't bother anyone on this forum anymore.

Link to comment
Share on other sites

Don't give up, man! You're not wasting anybody's time. I spend my time helping people on this forum on my own volition, because I like to help people, and I think everyone deserves a helping hand at programming. It's a difficult skill to learn, but you can do SO much with it. Many use LUA for mods and other scripting purposes, and when you understand one such language, the others are much easier to grasp. Even World of Warcraft uses LUA scripting for mods.

Here's a really good basics tutorial, with an up-beat tone to it. I think it'll help you get started with a smile on your face.

http://luatut.com/crash_course.html

To make use of the code the other guy wrote, to spawn more shells in a line, simply add this underneath the spawnShell function:

local function spawnLineOfShells(startx, startz, endx, endz, numOfShells)
	local x = startx
	local z = startz
	for i = 0, n, numOfShells do
            spawnShell(x, z)
            x = x + (endx-startx)/n
            z = z + (endz-startz)/n
        end
end

and change onEaten to:

local function onEaten(inst, eater)
	local pos = inst:GetPosition()
	spawnLineOfShells(pos.x - 10, pos.z - 10, pos.x + 10, pos.z + 10, 8)
end

This will spawn 8 shells, in a line going from the position of the player, minus 10 on both x and z axis, to the position of the player, plus 10 on both x and z axis.

Take the basics tutorial, and then come back and look at this code. I bet when you come back to it, you'll look at it and have several "Aha!" moments ;)

Edited by Ultroman
Link to comment
Share on other sites

Thanks again. It works fine, I just had to make some changes due to stop occuring errors. I will use that that tutorial you linked. I hope I will understand it. Thank you for all your help, and motivation.

I will paste prefab file below just in case. Maybe someone else will use it.

local Assets =
{
	Asset("ANIM", "anim/barrage.zip"), 
    Asset("ATLAS", "images/inventoryimages/barrage.xml"),    
	Asset("IMAGE", "images/inventoryimages/barrage.tex")	
}

local prefabs = 
{
 "explode", "shell"
}


local function draw_line(inst, startx, startz, endx, endz, n)   
	if startx and startz and endx and endz and n then
		local x = startx
		local z = startz
		for i = 0, n, 1 do
			GLOBAL.SpawnPrefab("shell").Transform:SetPosition(x,0,z)
			x = x + (endx-startx)/n
			z = z + (endz-startz)/n
		end
	end

	
end
--local x, y, z = inst.Transform:GetWorldPosition()
--draw_line(inst, x, z, x+math.random(-10, 10), z+math.random(-10,10), 10)

local function spawnShell(x, z)
	SpawnPrefab("shell").Transform:SetPosition(x,0,z)	
end

local function spawnLineOfShells(startx, startz, endx, endz, n, numOfShells)
	
	local x = startx
	local z = startz
	for i = 0, n, 1 do
            spawnShell(x, z)
            x = x + (endx-startx)/n
            z = z + (endz-startz)/n
    end
end

local function onEaten(inst, eater)
	local pos = inst:GetPosition()
	spawnLineOfShells(pos.x - 10, pos.z - 10, pos.x + 10, pos.z + 10, 8) 
end

local function fn(Sim)
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddNetwork()
	inst.entity:AddSoundEmitter()

    MakeInventoryPhysics(inst)
    
    inst.AnimState:SetBank("barrage")
    inst.AnimState:SetBuild("barrage")
    inst.AnimState:PlayAnimation("idle", true)    


	  
	inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
		
	
    inst:AddComponent("sanityaura")
    inst.components.sanityaura.aura = -TUNING.SANITYAURA_MED
	

		
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.atlasname = "images/inventoryimages/barrage.xml"
	
	
	inst:AddComponent("inspectable")
	
	MakeHauntableLaunch(inst)
	
	inst:AddComponent("edible")
	inst.components.edible:SetOnEatenFn(onEaten) 
	
  
	
	

    
    return inst
end

return Prefab( "common/inventory/barrage", fn, Assets)

 

Link to comment
Share on other sites

You're right. I messed up the for-loop. Sorry about that.

What you have now isn't quite right either. You have an extra parameter, but fortunately for you, lua doesn't care if you leave trailing parameters empty in the function call. It should be:

local function spawnLineOfShells(startx, startz, endx, endz, numOfShells)
	
	local x = startx
	local z = startz
	for i = 0, numOfShells, 1 do
            spawnShell(x, z)
            x = x + (endx-startx)/n
            z = z + (endz-startz)/n
    end
end

Good luck in your ventures! See you on the forum :)

Edited by Ultroman
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...