Strange error

Strange error

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
MrSpontaneous
Posts: 242
Joined: 09 Sep 2005, 22:39

Strange error

Post by MrSpontaneous »

Hello, I have been scripting a "box collector" which lets units pick up certain resources with out reclaiming them. It currently loads, but there is some disjoint with part of the other lua code which prevents it from working. Below is the relevant code and the error.

Code: Select all

function gadget:GetInfo()
	return {
		name = "Box collector",
		desc = "removes box features and gives resources to collecting player",
		author = "MrSpontaneous (ak Pasha Wrangell) with help from JK",
		date = "Nov 11 2007",
		license = "GPL",
		layer = 1,
		enabled = true
	}
end


if (gadgetHandler:IsSyncedCode()) then
---synced----

	local Box="ammobox"
	local BoxId=FeatureDefNames[ammobox].id 
	local BoxesOnMap={}
	local unitInRange={}
	local Resources={}
	
	function gadget:initialize()
	ud = spring.GetAllFeatures () 
	for n,u in ipairs(ud) do
		if spring.GetFeatureDefID(u) == BoxId 
			then  table.insert(BoxesOnMap, u) end
		end
	end

		function gadget:GameFrame(f)
			for n,u in ipairs(BoxesOnMap) do
				local x,y,z = spring.GetFeaturePosition(u) 
				local unitInRange = spring.GetUnitsInSphere(x,y,z, 100)--at x , y , z with radius 100
			if (unitInRange and #unitInRange>0 and unitInRange[1].CustomParams["CanCollect"]) then
				local remM, maxM, remE, maxE, left = Spring.GetFeatureResources(u)   --- [1] is metal, [3] is energy
				Spring.AddTeamResource(Spring.GetUnitTeam(UnitInRange[1]) , "m", remM)
				Spring.AddTeamResource(Spring.GetUnitTeam(UnitInRange[1]) , "e", remE) 
				Spring.DestroyFeature(u)
				BoxesOnMap[n] = nil 
			end
		end
	end
	
	function gadgetHandler:FeatureDestroyed(FeatureID, _)
		if (BoxesOnMap[FeatureID] and BoxesOnMap[FeatureID].expiry) then
			BoxesOnMap[FeatureID] = nil
		end
	end
	
	function gadgetHandler:FeatureCreated(featureID, allyTeam)
		if (featureID == BoxId) then
			table.insert(BoxesOnMap, featureID)
		end
	end
	
end
the relevant code in the gadgets.lua file:

Code: Select all

-- setup the FeatureDefNames{} table
do
  local tbl = {}
  for _,def in pairs(FeatureDefs) do
    tbl[def.name] = def
  end
  FeatureDefNames = tbl
end
and the error :
could not load FeatureDefNames as it = nil

so for some reason the table is not getting any values of the def names from the preloaded feature list.

Thank you
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Strange error

Post by jK »

First, you should at best copy the error message in original text, because you misinterpret it. It said you tried to index a nil value.
Also it isn't a problem with any other gadget or the gadgethandler. See the line where the error occurs:
local Box="ammobox"
local BoxId=FeatureDefNames[ammobox].id
ammobox is a undefined variable (a nil value). So why do you assign the variable Box with the correct unitname (a string), but don't use it?

Btw it seems you moddled up something, because there is something like this:

Code: Select all

  local atable = {"foo"="bar"}
  if ( atable["foo"] == atable.foo ) then
    print("this gets printed")
  end
User avatar
MrSpontaneous
Posts: 242
Joined: 09 Sep 2005, 22:39

Post by MrSpontaneous »

sorry, I do not have the mod, so I don't have the error message to copy (been sending the code over to forboding and he packs it with the files. )

What do you mean moddled?

thank you
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Post by Forboding Angel »

aI thought you had access to the svn :/ sorry :(

@JK
The ammobox is a corpse that drops when a unit is killed. Every unit drops the same box. THe idea is to add a tag in the unit FBI CanCollect=1; and basically any time a unit with that tag gets within the appropriate range the box is picked up and the appropriate resources go to the team that picked it up.
User avatar
MrSpontaneous
Posts: 242
Joined: 09 Sep 2005, 22:39

Re: Strange error

Post by MrSpontaneous »

Well, I exist again. Sort of dragging this up from the depths. I changed the code to:

Code: Select all

	local Box="ammobox"
	local BoxId=FeatureDefNames[Box].id 
	local BoxesOnMap={}
	local unitInRange={}
	local Resources={}
But I am still getting the same error.

I don't get it, maybe I just fail.

Thanks
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Strange error

Post by Forboding Angel »

Hey mrspontaneous, jump on the lobby and ask lurker (sorry lurk, remember that I lub u :P). He may be able to sort it out.

I've also been looking it over (my lua knowledge is still pretty sucky, but thanks to wasp I can actually for the most part interpet what I am reading now), and honestly I can't figure out why it's breaking.

It's prolly somehting stupid that I broke or something. That would be about par for the course for me. :lol:
Post Reply

Return to “Lua Scripts”