Please help me with my building script?

Please help me with my building script?

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

Moderator: Moderators

Post Reply
Nimja
Posts: 8
Joined: 03 Dec 2014, 18:42

Please help me with my building script?

Post by Nimja »

I just started making a game in spring, by starting with the Example Game as a base. I want to make the base build buildings in a manner similar to the builder unit. So I made it a builder with a large range, and changed its script to this:

Code: Select all

local unitName = "base"

local unitDef =
{
-- Internal settings
	BuildPic = "base.bmp",
	Category = "BUILDING",
	ObjectName = "base2.s3o",
	Side = "TANK",
	TEDClass = "PLANT",
	script = "base.lua",
	
-- Unit limitations and properties
	ActivateWhenBuilt = true,
	BuildTime = 1000,
	commander = true,
	Description = "Builds buildings",
	MaxDamage = 1500,
	Name = "Base",
	RadarDistance = 0,
	SightDistance = 400,
	SoundCategory = "BUILDING",
	Upright = 1,
	levelground = 1,
	
-- Energy and metal related
	BuildCostmetal = 0,
	BuildCostEnergy = 0,
	EnergyStorage = 0,
	EnergyUse = 0,
	MetalStorage = 0,
	EnergyMake = 0, 
	MakesMetal = 0, 
	MetalMake = 0,
	
-- Pathfinding and related
	FootprintX = 4,
	FootprintZ = 4,
	MaxSlope = 10,
	MaxWaterDepth = 0,
	YardMap = "cccc cccc cccc cccc",

-- Abilities
	Builder = 1,
	BuildDistance = 1000,
	WorkerTime = 1,
        Reclaimable = 0,
	ShowNanoSpray = 0,
	CanBeAssisted = 0,
	buildoptions = 
	{
		"powerplant",
	},
	
-- Abilities new to Spring
	
-- Weapons and related

	ExplodeAs = "BUILDINGLARGEDEATH",
	SelfDestructAs = "BUILDINGLARGEDEATH",
	SelfDestructCountdown = 5,
}

return lowerkeys({ [unitName] = unitDef })

Code: Select all

local building, platform, nanopoint = piece "building", piece "platform", piece "nanopoint"
-- declares all the pieces we'll be using.
local SIG_BUILD = 2

-- local RESTORE_DELAY = Spring.UnitScript.GetLongestReloadTime(unitID) * 2

function script.Create()
	return 0
end

function script.StartBuilding()
--called whenever construction begins.
	Signal(SIG_BUILD)
	SetSignalMask(SIG_BUILD)
-- Stops the unit from "building" and "not building" at the same time. This would be confusing.

    --Start building!
	SetUnitValue(COB.INBUILDSTANCE, 1)

	return 1
end

function script.StopBuilding()
	Signal(SIG_BUILD)
	SetSignalMask(SIG_BUILD)
-- Stops the unit from "building" and "not building" at the same time. This would be confusing.
	SetUnitValue(COB.INBUILDSTANCE, 0)
    --Stops building!
	Sleep(1)
	return 0
end

function script.Killed(recentDamage, maxHealth)
	return 0
end

function script.HitByWeapon(x,z,weaponDef,damage)
    -- This stops the unit being damaged if it's not been built yet.
	if GetUnitValue(COB.BUILD_PERCENT_LEFT)>2 then return 0
	else return damage
	end
end
It's just the code for the builder unit,with the code about turning the turret removed. But when I start the game,the base does not build the building, and never gives me a "place the building" prompt I would get with the builder unit. So, could anyone please explain to me where my code has gone wrong?


Also, as a secondary question, can more than one script be attached to a unit?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Please help me with my building script?

Post by FLOZi »

Too busy atm to properly head-parse your code, so hopefully this may be of use: http://springrts.com/wiki/User:Flozi/An ... es#Factory

Long story short, factories require a slightly different setup.

Each unit can only have one script, but one script can be used for any number of units.
Nimja
Posts: 8
Joined: 03 Dec 2014, 18:42

Re: Please help me with my building script?

Post by Nimja »

I used your code, but the problem still persists: the building the base builds appears at the pad,but I can't tell the base where to build the building.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Please help me with my building script?

Post by FLOZi »

Ah, I re-read your post.

Ok this is a little more complex - static 'Building's cannot build other static 'Building's, so you will need to do some tweaking.

Probably the easiest thing to do is to make the 'base' unit an 'ImmobileUnit' as opposed to a 'Building' - remove the yardmap tag in it's unitdef. (And, apologies, return it to the builder-style script rather than factory)

If you want a factory to also build other structures, some wizardry will be required.
Nimja
Posts: 8
Joined: 03 Dec 2014, 18:42

Re: Please help me with my building script?

Post by Nimja »

OK, so the building now appears as the set of blueprints on the map, which is progress, but it doesn't get actually built...
On a related note,what is it in the engine that differentiates buildings and units that allows units to build like this but buildings not to?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Please help me with my building script?

Post by FLOZi »

Nimja wrote:OK, so the building now appears as the set of blueprints on the map, which is progress, but it doesn't get actually built...
Woo, progress! :-) OK, your unit needs to call

Code: Select all

Spring.SetUnitNanoPieces(unitID, nanoPieces)
where nanoPieces is a list of piece nums, c.f.: http://springrts.com/wiki/User:Flozi/An ... es#Builder and entry for that function here http://springrts.com/wiki/Lua_SyncedCtrl#Unit_Control

On a related note,what is it in the engine that differentiates buildings and units that allows units to build like this but buildings not to?
Short answer: the unitdef tags
  • yardmap
  • canFly
  • maxVelocity
  • movementClass
Allow you as a game dev to determine which kind of unit it will be.

Long answer: https://github.com/spring/spring/blob/d ... tDef.h#L56
Determines it internally. There are certain restrictions internally on what kind of unit can do what, it is simply inherent in the design (certain special attributes are actually represented by a different C++ class internally, e.g. transports, metal extractors, factories etc)
Nimja
Posts: 8
Joined: 03 Dec 2014, 18:42

Re: Please help me with my building script?

Post by Nimja »

I added the line

Code: Select all

Spring.SetUnitNanoPieces(unitID, nanopoint)
(The base has only one nanopiece)
To the code, but nothing changes, and the blueprints don't even show up. I know there is a building there because when I press shift, the footprint of the building shows up. So I changed the code back.

What makes the nanopieces so vital to the building? are they the part that the engine considers to be actually building?
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Please help me with my building script?

Post by PicassoCT »

The Nano piece is vital because it is where the nano- spray sfx come out. Which is somewhat hardcoded in spring.

But if construction is actually started.. maybe you should activate showNanospray for once in your unitdef.

finally could you post your infolog? Its where errors in the script show up.
Nimja
Posts: 8
Joined: 03 Dec 2014, 18:42

Re: Please help me with my building script?

Post by Nimja »

So I switched on nanoparticles, and they do appear.
I found the infolog, here is the bit where errors start to crop up:
[f=0000000] Loading Unit Definitions
[f=0000000] Error: Couldn't find WeaponDef NOWEAPON and explodeAs for base is missing!
[f=0000000] Error: Couldn't find WeaponDef NOWEAPON and selfDestructAs for base is missing!
[f=0000000] Error: Couldn't find WeaponDef NOWEAPON and explodeAs for powerplant is missing!
[f=0000000] Error: Couldn't find WeaponDef NOWEAPON and selfDestructAs for powerplant is missing!
[f=0000000] Warning: powerplant: Given yardmap requires 3 extra char(s)!
[f=0000000] Loading Feature Definitions
[f=0000000] [IPathManager::GetInstance] using DEFAULT path-manager
[f=0000000] Initializing Map Features
[f=0000000] [Sound] Error: Unable to open audio file: sounds/mappoint.wav
[f=0000000] Creating ShadowHandler & DecalHandler
[f=0000000] FBO::maxSamples: 4
[f=0000000] Creating GroundDrawer
[f=0000000] Loading Map Tiles
[f=0000000] Loading Square Textures
[f=0000000] CSMFGroundTextures::ConvolveHeightMap: 25 ms
[f=0000000] Switching to ROAM Mesh Rendering
[f=0000000] Creating TreeDrawer
[f=0000000] Creating ProjectileDrawer & UnitDrawer
[f=0000000] Creating Projectile Textures
[f=0000000] Creating Water
[f=0000000] Game::LoadInterface (Camera&Mouse): 316 ms
[f=0000000] [Sound] Error: Unable to open audio file: sounds/multiselect.wav
[f=0000000] Game::LoadInterface (Console): 0 ms
[f=0000000] [Sound] Error: Unable to open audio file: FailedCommand
[f=0000000] [Sound] Error: CSound::GetSoundId: could not find sound: FailedCommand
[f=0000000] Loading LuaRules
[f=0000000] Loaded SYNCED gadget: Lua unit script framework <unit_script.lua>
[f=0000000] Loaded SYNCED gadget: Spawn <game_spawn.lua>
[f=0000000] Loading LuaGaia
[f=0000000] Loading LuaUI
Also, if it helps, here is where I put the SetUnitNanoPieces line:

Code: Select all

function script.StartBuilding(
   Signal(SIG_BUILD)
   SetSignalMask(SIG_BUILD)
   SetUnitValue(COB.INBUILDSTANCE, 1)
   Spring.SetUnitNanoPieces(unitID, nanoPoints)
   return 1
end
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Please help me with my building script?

Post by PicassoCT »

Code: Select all

Warning: powerplant: Given yardmap requires 3 extra char(s)!


Fixing them one at a time
yardmap is always the size of footprint*footprint

3*3 yardmap

is

Code: Select all

ccc
ccc
ccc
is the SIG_BUILD defined in your script? It needs to be a power of two.. like 1 2 4 8 16

if it still doesent work - maybe the texture of the powerplant has opacity set..
or the script is faulty..
test-replace it with this one - change the center pieces name too the nanopiece

Code: Select all

function script.HitByWeapon ( x, z, weaponDefID, damage ) 
end
center=piece"center"

function script.Create()
end

function script.StartBuilding()
 SetUnitValue(COB.INBUILDSTANCE, 1)
end


function script.StopBuilding()
 SetUnitValue(COB.INBUILDSTANCE, 0)
end
function script.Killed(recentDamage,_)


return 1
end


----aimining & fire weapon
function script.AimFromWeapon1() 
	return center 
end



function script.QueryWeapon1() 
	return center
end

function script.AimWeapon1( Heading ,pitch)	
	--aiming animation: instantly turn the gun towards the enemy

	return true

end
 

function script.FireWeapon1()	

	return true
end



function script.StartMoving()

end

function script.StopMoving()
		
		
end

function script.Activate()

return 1
end

function script.Deactivate()

return 0
end

function script.QueryBuildInfo() 
  return center 
end

function script.QueryNanoPiece()
     return center
end

Nimja
Posts: 8
Joined: 03 Dec 2014, 18:42

Re: Please help me with my building script?

Post by Nimja »

PicassoCT wrote:

Code: Select all

Warning: powerplant: Given yardmap requires 3 extra char(s)!

is the SIG_BUILD defined in your script? It needs to be a power of two.. like 1 2 4 8 16
[/quote]
yes,it is. It is set to 2.
[quote="PicassoCT"]
if it still doesent work - maybe the texture of the powerplant has opacity set..[/quote]No, it is the same building from the Example Game. Also, If it did have transparnecy, I would still be able to select them one the wireframe disappears, which I cant.[quote="PicassoCT"]
or the script is faulty..
test-replace it with this one - change the center pieces name too the nanopiece

[code]function script.HitByWeapon ( x, z, weaponDefID, damage ) 
end
center=piece"center"

function script.Create()
end

function script.StartBuilding()
 SetUnitValue(COB.INBUILDSTANCE, 1)
end


function script.StopBuilding()
 SetUnitValue(COB.INBUILDSTANCE, 0)
end
function script.Killed(recentDamage,_)


return 1
end


----aimining & fire weapon
function script.AimFromWeapon1() 
	return center 
end



function script.QueryWeapon1() 
	return center
end

function script.AimWeapon1( Heading ,pitch)	
	--aiming animation: instantly turn the gun towards the enemy

	return true

end
 

function script.FireWeapon1()	

	return true
end



function script.StartMoving()

end

function script.StopMoving()
		
		
end

function script.Activate()

return 1
end

function script.Deactivate()

return 0
end

function script.QueryBuildInfo() 
  return center 
end

function script.QueryNanoPiece()
     return center
end

Same problem...
infolog now looks like this:
[f=0000000] Error: Couldn't find WeaponDef NOWEAPON and explodeAs for base is missing!
[f=0000000] Error: Couldn't find WeaponDef NOWEAPON and selfDestructAs for base is missing!
[f=0000000] Error: Couldn't find WeaponDef NOWEAPON and explodeAs for powerplant is missing!
[f=0000000] Error: Couldn't find WeaponDef NOWEAPON and selfDestructAs for powerplant is missing!
[f=0000000] Loading Feature Definitions
[f=0000000] [IPathManager::GetInstance] using DEFAULT path-manager
[f=0000000] Initializing Map Features
[f=0000000] [Sound] Error: Unable to open audio file: sounds/mappoint.wav
[f=0000000] Creating ShadowHandler & DecalHandler
[f=0000000] FBO::maxSamples: 4
[f=0000000] Creating GroundDrawer
[f=0000000] Loading Map Tiles
[f=0000000] Loading Square Textures
[f=0000000] CSMFGroundTextures::ConvolveHeightMap: 26 ms
[f=0000000] Switching to ROAM Mesh Rendering
[f=0000000] Creating TreeDrawer
[f=0000000] Creating ProjectileDrawer & UnitDrawer
[f=0000000] Creating Projectile Textures
[f=0000000] Creating Water
[f=0000000] Game::LoadInterface (Camera&Mouse): 595 ms
[f=0000000] [Sound] Error: Unable to open audio file: sounds/multiselect.wav
[f=0000000] Game::LoadInterface (Console): 0 ms
[f=0000000] [Sound] Error: Unable to open audio file: FailedCommand
[f=0000000] [Sound] Error: CSound::GetSoundId: could not find sound: FailedCommand
[f=0000000] Loading LuaRules
[f=0000000] Loaded SYNCED gadget: Lua unit script framework <unit_script.lua>
[f=0000000] Loaded SYNCED gadget: Spawn <game_spawn.lua>
[f=0000000] Loading LuaGaia
[f=0000000] Loading LuaUI
[f=0000000] LuaSocketEnabled: yes
[f=0000000] Using LUAUI_DIRNAME = LuaUI/
[f=0000000] Reloaded ctrlpanel from file: LuaUI/ctrlpanel.txt
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Please help me with my building script?

Post by knorke »

Code: Select all

   Spring.SetUnitNanoPieces(unitID, nanoPoints)
Did you define nanoPoints anywhere?
It must be a table of pieces that exist in the 3D model, like:

Code: Select all

local nanoPoints = {}
nanoPoints[1] = piece ("nuzzle1")
nanoPoints[2] = piece ("nuzzle2")
/edit
That infolog has nothing relevant to unit scripts.
Is this your first unit script?
Builders are bit strange, some things need to be done "bit stupid" and it has more pitfalls thank normal units with weapons. For example does the player have resources to begin the construction?
The SetUnitNanoPieces thing is a "new way" of doing that, it requires bit knowing about tables, older/other way is just returning pieces from function which is bit easier to understand and more in line with how other functions work.
For start try the "stumpy tutorial" linked here:
http://springrts.com/wiki/Animation-Lua ... er_reading
Nimja
Posts: 8
Joined: 03 Dec 2014, 18:42

Re: Please help me with my building script?

Post by Nimja »

I somehow fixed the problem, I don't know how, all I did was tweak a few resource and building parameters in the unitdef. But now it works!
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Please help me with my building script?

Post by PicassoCT »

shoot the screen...
Nimja
Posts: 8
Joined: 03 Dec 2014, 18:42

Re: Please help me with my building script?

Post by Nimja »

Image
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Please help me with my building script?

Post by FLOZi »

knorke wrote:For example does the player have resources to begin the construction?
Post Reply

Return to “Lua Scripts”