stack overflow

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

stack overflow

Post by vieuxchat »

Here is my code

Code: Select all

function elementAjoute()	
	local dagger_id

	hudPrint(beacon_furnace_2.id)
	dagger_id = trouveUneDague(beacon_furnace_2.surface)
	
	local e = findEntity(dagger_id)
	if e ~= nil then 
		hudPrint(e.name) 
		local s = spawn("ethereal_blade", party.level, beacon_furnace_2.x, beacon_furnace_2.y, beacon_furnace_2.facing, beacon_furnace_2.elevation)
		--beacon_furnace_2.surface:addItem(s.item) -- That line is causing me trouble
		e:destroyDelayed()
	end
	
end

function trouveUneDague(surface)
	for v,i in surface:contents() do
		hudPrint(i.go.id)
		if i.go.name == "dagger" then return i.go.id end
	end
	return "nil"
end
Why do I get a stack overflow at the line "beacon_furnace_2.surface:addItem(s.item)" ?
User avatar
Dr.Disaster
Posts: 2876
Joined: Wed Aug 15, 2012 11:48 am

Re: stack overflow

Post by Dr.Disaster »

AFAIK addItem() works with containers, monsters, sockets and surfaces and the syntax tells you want to use the surface part.

Yet in the code it seems you picked an item ("dagger") as the surface you want to add something to.
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Re: stack overflow

Post by vieuxchat »

I don't think so, the name "beacon_furnace_2.surface" means that the surface is owned by the beacon furnace.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: stack overflow

Post by minmay »

You get a stack overflow because you have a connector from the surface to elementAjoute(). So when you add an item to the surface inside elementAjoute(), it causes a recursive call to elementAjoute(), which adds another item to the surface, which causes another recursive call to elementAjoute(), which adds another item to the surface...
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Re: stack overflow

Post by vieuxchat »

I'm stupid.
You're right.

Thank you.
Post Reply