Page 1 of 2

Quick question: separate weapon Lua file not working

Posted: 12 Oct 2009, 22:51
by PTSnoop
I'm trying to have gcfly.lua (the unit) and gcspikelaser.lua (the weapon) as different lua files. It's not going so well.

Everything works fine when it's all in the one file; when I cut the weapon info into another file, my unit won't attack any more.

Do I need to put something else into either file? If so, what?

gcfly.lua :

Code: Select all

local unitName = "gcfly"

local unitDef = 
{
-- Internal settings
	BuildPic = "gcfly.bmp",
	Category = "CUBE UNIT NOTAIR ALL",
	Name = "GC Fly",
	ObjectName = "gcfly.s3o",
	Side = "GC",
	TEDClass = "TANK",
	UnitName = "gcfly",
	
-- Unit limitations and properties
	ActivateWhenBuilt = 1,
	BuildTime = 1000,
	Description = "Light Scout",
	MaxDamage = 60,
	RadarDistance = 0,
	SightDistance = 500,
	SoundCategory = "GC_FLY",
	Upright = 1,
	
-- Energy and metal related
	BuildCostEnergy = 20,
	BuildCostMetal = 20,
	
-- Pathfinding and related
	Acceleration = 0.2,
	BrakeRate = 0.05,
	movementclass = "GC2x2",
	FootprintX = 2,
	FootprintZ = 2,
	MaxSlope = 50,
	MaxVelocity = 6.0,
	MaxWaterDepth = 12,
	TurnRate = 1200,
	
-- Abilities
	Builder = 0,
	Reclaimable = 0,
	CanAttack = 1,
	CanGuard = 1,
	CanMove = 1,
	CanPatrol = 1,
	LeaveTracks = 0,
	
-- Abilities new to Spring
	
-- Weapons and related
	weapons = {
		[1] = {
			def = "gcspikelaser",
		}
	},
	ExplodeAs = "GCFLYDEATH",
	SelfDestructAs = "GCFLYDEATH",
	SelfDestructCountdown = 5,
	NoChaseCategory = "VTOL",
}

-- Weapon info cut from here

unitDef.weaponDefs = weaponDefs

return lowerkeys({ [unitName] = unitDef })
gcspikelaser.lua:

Code: Select all

local weaponDefs  = {
	gcspikelaser  =  {
		weaponType = "BeamLaser",
		areaOfEffect = 8,
		avoidFeature = 1,
		avoidFriendly = 1,
		BeamTime = 0.4,
		canAttackGround = 1,
		collisionSize = 8,
		commandFire = 0,
		coreThickness = 0.25,
		craterBoost = 0,
		craterMult = 0,
		explosionSpeed = 5,
		impulseBoost = 0,
		impulseFactor = 0,
		intensity = 0.8,
		LaserFlareSize = 7.5,
		lineofsight = 1,
		minIntensity = 0.9,
		noSelfDamage = 1,
		range = 450,
		reloadtime = 1.5,
		rgbcolor = "1.0 0.0 0.0",
		rgbcolor2 = "1 1 1",
		thickness = 5,
		turret = 1,
-- 		texture1 = "laserfalloff",
-- 		texture2 = "laserend",
		explosiongenerator = "custom:GCSPIKELASER_FX",
		weaponVelocity = 400,
		damage = {
			default = 25,
		},
	},
}

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 00:13
by smoth
Please try and keep it to one thread that way you do not flood the page with questions posts

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 00:30
by Argh
Just a quick guess, haven't had time to read the weaponDef code through (again, my apologies if this is wrong, I don't use Lua weaponDefs):

Code: Select all

 def = "gcspikelaser",
Should probably be:

WeaponDefNames[gcspikelaser]

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 00:39
by PTSnoop
Why did I not notice the Mod Question Repository thread before? I'll try to limit my question-flooding to that one thread in future.

Argh: replacing the line gives no change to gameplay, and

Code: Select all

[      0] Error parsing units/gcfly.lua: error = 2, units/gcfly.lua, [string "units/gcfly.lua"]:55: attempt to index global 'WeaponDefNames' (a nil value)
in the infolog.

Is what I'm trying even possible? I can't find any other mods that do it this way, and all I have to go off is this one post I found a while ago...

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 01:36
by Argh
OK, so basically, the weapon stops working.

I think that what you're doing needs to be included in weaponDefs.lua, to work correctly.

Open base/springcontent.sdz, look at the weapondefs.lua

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 01:48
by PTSnoop
It looks like there's a thing in weapondefs.lua for loading weapons from lua files in the /weapons folder...

Code: Select all

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Load the raw LUA format weapondef files
--  (these will override the TDF versions)
--

local luaFiles = VFS.DirList('weapons/', '*.lua')

for _, filename in ipairs(luaFiles) do
  local wdEnv = {}
  wdEnv._G = wdEnv
  wdEnv.Shared = shared
  wdEnv.GetFilename = function() return filename end
  setmetatable(wdEnv, { __index = system })
  local success, wds = pcall(VFS.Include, filename, wdEnv)
  if (not success) then
    Spring.Echo('Error parsing ' .. filename .. ': ' .. wd)
  elseif (wds == nil) then
    Spring.Echo('Missing return table from: ' .. filename)
  else
    for wdName, wd in pairs(wds) do
      if ((type(wdName) == 'string') and (type(wd) == 'table')) then
        wd.filename = filename
        weaponDefs[wdName] = wd
      end
    end
  end  
end
By the way, I tried moving the gcspikelaser.lua file into the /units folder; it didn't make any difference.

...but now I look at infolog.txt it now has

Code: Select all

[      0] Bad return table from: units/gcspikelaser.lua
Does this mean anything to anyone?

EDIT: After moving gcspikelaser.lua back into /weapons, I now get

Code: Select all

[      0] Missing return table from: weapons/gcspikelaser.lua

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 01:50
by Argh

Code: Select all

local luaFiles = VFS.DirList('weapons/', '*.lua')
Hold up, I'll mess with it for a second.

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 02:24
by lurker
Argh's wrong.

Your problem is quite simple. You need an equivalent to this line in your weapondef: return lowerkeys({ [unitName] = unitDef })

Since you already put it in a local weaponDefs table, you need to add this line:
return weaponDefs

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 02:31
by Argh

Code: Select all

weaponDef = {
   gcspikelaser  =  {
      name                    = [[lazer]],
      range = 450,
      turret = true,
      weaponVelocity = 400,
      weaponType = [[LaserCannon]],
      reloadTime=1.0,
      areaOfEffect= 16,
      tolerance = 1500,
      damage = {
         default = 25,
      },
   },
}
return lowerkeys({ gcspikelaser = weaponDef })
Results in a not-crash, but the weapon's variables are clearly not right. Lemme add "local"... nope. It seems to assign the Unit some sort of null weapon instead, so far as I can tell.

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 02:46
by PTSnoop
Adding "return weaponDefs" removes the infolog error but doesn't change gameplay. Also, no change with "return lowerkeys({ [unitName] = unitDef })" or "return lowerkeys({ gcspikelaser = weaponDef })".

After trying out a few more permutations of earlier suggestions, using "return weaponDefs" in gcspikelaser.lua and "weapons = { WeaponDefNames[gcspikelaser] }" in gcfly.lua brings back the "Attack" button and aims the spike but doesn't fire anything. Curiouser and curiouser...

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 02:51
by Argh
After trying out a few more permutations of earlier suggestions, using "return weaponDefs" in gcspikelaser.lua and "weapons = { WeaponDefNames[gcspikelaser] }" in gcfly.lua brings back the "Attack" button and aims the spike but doesn't fire anything.
Yeah, that's what I'm seeing, even with this cut-down, practically all-default version, too.

I am getting the weapon's range, and it looks like it's 8 (teeny tiny range circle).

And, I should note, all crashes refer to 'wd' being nil, because it's being invoked before it's created, lol:

Code: Select all

Spring.Echo('Error parsing ' .. filename .. ': ' .. wd)

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 02:55
by Argh
Victory!

1. I'm stupid; lowerkeys isn't a function that's available in raw Lua weaponDef.

Do not use upper-case letters for weapon variables (other than stuff between square brackets, weapon types are case-sensitive strings) and make sure that booleans are "true" or "false", not 0 / 1.

2. See 1.

Code: Select all

local weaponDef = {
   gcspikelaser  =  {
      name                    = [[lazer]],
      range = 450,
      turret = true,
      weaponvelocity = 400,
      weapontype = [[LaserCannon]],
      reloadtime=1.0,
      areaofeffect= 16,
      tolerance = 1500,
      damage = {
         default = 25,
      },
   },
}

return weaponDef


Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 03:08
by PTSnoop
Almost victory!

...but I'm pretty sure the problem is just my lack of understanding of Lua syntax.

At the moment, what I have is a small chunk of laser-texture acting like a ballistic projectile, with no explosion. I'm pretty sure the things that aren't working are the

Code: Select all

		explosiongenerators = 
		{
			"custom:GCSPIKELASER_FX",
		},
and the

Code: Select all

		weaponType = "BeamLaser",
What should these be?

EDIT: Oh, right, don't use capital letters. I'll try small letters now.
EDIT2: The BeamLaser is BeamLasering perfectly now, but still no explosion. So close...

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 03:18
by Argh

Code: Select all

[[custom:GCSPIKELASER_FX]],
...Assuming that that is its name, but I suspect:

Code: Select all

[[custom:gcspikelaser_fx]],
... is what you need to use, since it's in raw-lua land.

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 03:26
by PTSnoop
Changing to square brackets doesn't change anything.

Code: Select all

explosiongenerator = "custom:GCSPIKELASER_FX",
But removing the curly brackets makes everything work fine!

SUCCESS!

Thanks for your help, Argh!

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 03:31
by Argh
Hmm, I'll keep the curly-brackets thing in mind. I hope damage doesn't work that way, lol.

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 20:32
by PTSnoop
Just when I thought I'd got this fixed...

Another unit (cubesmall.lua) with a different weapon (cubescoutlightning.lua) crashes Spring with the same code as the GC Fly from before. The infolog gives:
[ 0] Error parsing units/cubesmall.lua: error = 2, units/cubesmall.lua, [string "units/cubesmall.lua"]:55: attempt to index global 'WeaponDefNames' (a nil value)
[ 0] Error parsing units/gcfly.lua: error = 2, units/gcfly.lua, [string "units/gcfly.lua"]:54: attempt to index global 'WeaponDefNames' (a nil value)
...the GC Fly works fine in gameplay; the Cube Scout crashes completely with the weapon in place, but works fine (albeit without a weapon) with the weapon code commented out.

Confused!

Is the problem really with the WeaponDefNames[cubescoutlightning] line as infolog claims, or have I just missed out a comma or something?

units/cubesmall.lua:

Code: Select all

local unitName = "cubesmall"

local unitDef =
{
-- Internal settings
	BuildPic = "cubesmall.bmp",
	Category = "CUBE UNIT NOTAIR ALL",
	Name = "Cube Scout",
	ObjectName = "cubesmall.s3o",
	Side = "CUBE",
	TEDClass = "TANK",
	UnitName = "cubesmall",
	
-- Unit limitations and properties
	ActivateWhenBuilt = 1,
	BuildTime = 1000,
	Description = "Light Scout",
	MaxDamage = 75,
	RadarDistance = 0,
	SightDistance = 1000,
	SoundCategory = "CUBE_TANK",
	Upright = 0,
	
-- Energy and metal related
	BuildCostEnergy = 20,
	BuildCostMetal = 20,
	
-- Pathfinding and related
	Acceleration = 0.15,
	BrakeRate = 0.1,
	movementclass = "Default2x2",
	FootprintX = 2,
	FootprintZ = 2,
	MaxSlope = 10,
	MaxVelocity = 4.0,
	MaxWaterDepth = 12,
	TurnRate = 900,
	
-- Abilities
	Builder = 0,
	Reclaimable = 0,
	CanAttack = 1,
	CanGuard = 1,
	CanMove = 1,
	CanPatrol = 1,
	LeaveTracks = 0,
	
-- Abilities new to Spring
	
-- Weapons and related
--[[	weapons = {
		[1] = {
			WeaponDefNames[cubescoutlightning]
		}
	},]]--
	ExplodeAs = "CUBESCOUTDEATH",
	SelfDestructAs = "CUBESCOUTDEATH",

	SelfDestructCountdown = 5,
	NoChaseCategory = "VTOL",
}

return lowerkeys({ [unitName] = unitDef })
weapons/cubescoutlightning.lua:

Code: Select all

local weaponDef  = 
{
	cubescoutlightning = 
	{
		weapontype = "LightingCannon",
		areaofeffect = 16,
		avoidfeature = 0,
		avoidfriendly = 0,
		canattackground = 1,
		collidefriendly = 0,
		commandfire = 0,
		craterboost = 0,
		cratermult = 0,
		explosionspeed = 5,
		impulseboost = 0,
		impulsefactor = 0,
		noselfdamage = 1,
		range = 350,
		reloadtime = 1,
		rgbcolor = "0.1 0.2 0.5",
		turret = 1,
		texture1 = "lightning",
		explosiongenerator = "custom:LIGHTNINGGUN_FX",
		weaponvelocity = 400,
		damage =
			{
			default = 10,
			},
	}
}
return weaponDef

Re: Quick question: separate weapon Lua file not working

Posted: 13 Oct 2009, 22:06
by Argh
These are booleans. Set to true / false.

Code: Select all

      avoidfeature = 0,
      avoidfriendly = 0,
      canattackground = 1,
      collidefriendly = 0,
      commandfire = 0,
      noselfdamage = 1,
      turret = 1,
Other than that, and goofs with LIGHTNINGGUN_FX (quite possible, especially if it's causing a crash), looks good from here.

Re: Quick question: separate weapon Lua file not working

Posted: 14 Oct 2009, 00:21
by PTSnoop
Okay, this now makes even less sense.

I've got sidedata.tdf set up so

Code: Select all

[SIDE0]
	{
		name = CUBE;
		commander = cubesmall;
	}
[SIDE1]
	{
		name = GC;
		commander = gcfly;
	}
I've got rid of the old cubesmall.lua for now, copied and pasted the working gcfly.lua as a new cubesmall.lua , and changed

Code: Select all

local unitName = "cubesmall"
and

Code: Select all

	UnitName = "cubesmall",
. Everything else is the same, so what I should get is an identical GCFly unit with the unitname "cubesmall". Right?

GC vs GC (original Flies) works fine; change either of the sides to Cube and I get "Couldn't find unittype cubesmall" errors!

:shock:

What is going on here??? The only other difference between these units is the BOS/COB scripting, and I can't see that being the cause of the problem...

---

EDIT: What's that gcfly.tdf doing there? I thought I got rid of that. Meh, it looks like it's been loading the unit from tdf instead of lua and that's why it's been working. So back to the drawing board with the lua weapon problem.

But at least it's not being inconsistent now...

EDIT2: After a bit more tweaking, I'm now pretty sure the problem's with this line:

Code: Select all

         WeaponDefNames[cubescoutlightning]
...but I've got no idea what it should be instead.

Re: Quick question: separate weapon Lua file not working

Posted: 14 Oct 2009, 22:54
by Argh

Code: Select all

weapons = {
    {
      def = cubescoutlightning,
    },
},
I think. I'll have to test that, I just was building the weaponDef for readability via TDF-generated unitDef, didn't look at the Lua unitDef side.