[total newbie] GetPiecePosDir(attemp to call global)

[total newbie] GetPiecePosDir(attemp to call global)

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

Moderator: Moderators

Post Reply
tino415
Posts: 10
Joined: 20 Feb 2013, 09:55

[total newbie] GetPiecePosDir(attemp to call global)

Post by tino415 »

Why I can't use GetPiecePosDir (attemp to call global). Is there any workoaround ?? Or i made any mistake ?

Code: Select all

local base = piece "base"
local ltracks = {}
local rtracks = {}
-- declares all the pieces we'll use in the script.

for i=0,42 do
	local tname = "lt" .. i
	ltracks[i] = piece (tname)
end

for i=0,42 do
	local tname = "rt" .. i
	rtracks[i] = piece (tname)
end

function script.StartMoving()
	movetrack()
end

function movetrack()
	local i = 0
	while true do
		local tx,ty,tz,rx,ry,rz = GetPiecePosDir ( rtracks[i+1] )
		Move(rtracks[i], x_axis, tx, 4)
		Move(rtracks[i], y_axis, ty, 4)
		Move(rtracks[i], z_axis, tz, 4)
		if i == 42 then break
		else i=i+1 end
	end
end

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

function script.HitByWeapon(x,z,weaponDef,damage)
    -- This stops the unit taking damage until it's been built.
	if GetUnitValue(COB.BUILD_PERCENT_LEFT)>2 then return 0
	else return damage
	end
end
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by FLOZi »

Spring.GetPiecePosDir
tino415
Posts: 10
Joined: 20 Feb 2013, 09:55

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by tino415 »

FLOZi wrote:Spring.GetPiecePosDir
I get error "attempt to call field...", :(
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by FLOZi »

I'm a muppet, didn't realise that function is in LUS itself:

Spring.UnitScript.GetPiecePosDir

Only certain functions are automagically localised for LUS scripts:

https://github.com/spring/spring/blob/d ... header.lua
tino415
Posts: 10
Joined: 20 Feb 2013, 09:55

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by tino415 »

FLOZi wrote:I'm a muppet, didn't realise that function is in LUS itself:

Spring.UnitScript.GetPiecePosDir

Only certain functions are automagically localised for LUS scripts:

https://github.com/spring/spring/blob/d ... header.lua
Thank's
tino415
Posts: 10
Joined: 20 Feb 2013, 09:55

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by tino415 »

Code: Select all

local rtracks = {}
local GetPieceTranslation = UnitScript.GetPieceTranslation
-- declares all the pieces we'll use in the script.

for i=0,42 do
	local tname = "lt" .. i
	ltracks[i] = piece (tname)
end

for i=0,42 do
	local tname = "rt" .. i
	rtracks[i] = piece (tname)
end

function script.StartMoving()
	movetrack()
end

function movetrack()
	local i = 0
	while (true) do
		-- local atx,aty,atz = UnitScript.GetPieceTranslation ( rtracks[i] )
		local tx,ty,tz = GetPieceTranslation ( rtracks[i+1] )
		Move(rtracks[i], x_axis, tx, 5)
		Move(rtracks[i], y_axis, ty, 5)
		Move(rtracks[i], z_axis, tz, 5)
		if i == 41 then 
			local tx,ty,tz = GetPieceTranslation ( rtracks[0] )
			Move(rtracks[42], x_axis, tx, 1.2)
			Move(rtracks[42], y_axis, ty, 1.2)
			Move(rtracks[42], z_axis, tz, 1.2)
			break
		else i=i+1 end
	end
end

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

function script.HitByWeapon(x,z,weaponDef,damage)
    -- This stops the unit taking damage until it's been built.
	if GetUnitValue(COB.BUILD_PERCENT_LEFT)>2 then return 0
	else return damage
	end
end
I want to get track possition and next track position and then move track 1 to possition of track 2, but it seems not working... Why ??
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by knorke »

The idea is good, has potential :)
but it seems not working... Why ??
hm one thing is you need some Sleep() or WaitForMove() function to give the pieces time to float to their new positions.
Is there an error message or problem with logic? Errors are logged here:
http://springrts.com/wiki/Infolog.txt

if you upload mod with model it is easier for others to test
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by FLOZi »

Even an image of the model would be useful here - the biggest question is 'is it worth doing something this complex?'
tino415
Posts: 10
Joined: 20 Feb 2013, 09:55

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by tino415 »

Here is the mod, it's only for experimenting(i'm doing litle research how many time what takes), unit name is tracker, and it can't by builded, this mod I have only for experimenting, I'm think, there I can use animation script's from spring tanks, but in game i preparing to do I gona need more complex shapes of tracks then two wheel on one site :-D
Attachments
emptymod.zip
Game mod, I use onli for experimenting...
(2.31 MiB) Downloaded 9 times
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by knorke »

oh something just come to my mind to consider4:
Move command is relative to pieces original position, as is GetPiecePosDir iirc.
So if you do
local tx,ty,tz,rx,ry,rz = GetPiecePosDir ( rtracks[i+1] )
it will return 0,0,0,0,0,0
tino415
Posts: 10
Joined: 20 Feb 2013, 09:55

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by tino415 »

knorke wrote:oh something just come to my mind to consider4:
Move command is relative to pieces original position, as is GetPiecePosDir iirc.
So if you do
local tx,ty,tz,rx,ry,rz = GetPiecePosDir ( rtracks[i+1] )
it will return 0,0,0,0,0,0
Is there a way to get possition relative to parent piece ??
So I gona need to do full possition table...
Last edited by tino415 on 04 Mar 2013, 18:59, edited 1 time in total.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by FLOZi »

tino415 wrote:
knorke wrote:oh something just come to my mind to consider4:
Move command is relative to pieces original position, as is GetPiecePosDir iirc.
So if you do
local tx,ty,tz,rx,ry,rz = GetPiecePosDir ( rtracks[i+1] )
it will return 0,0,0,0,0,0
Is there a way to get possition relative to parent piece ??
Spring.UnitScript.GetPieceTranslation ( piece ) -> number x, y, z

c.f. http://springrts.com/wiki/Animation-LuaCallouts
tino415
Posts: 10
Joined: 20 Feb 2013, 09:55

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by tino415 »

FLOZi wrote:
tino415 wrote:
knorke wrote:oh something just come to my mind to consider4:
Move command is relative to pieces original position, as is GetPiecePosDir iirc.
So if you do
local tx,ty,tz,rx,ry,rz = GetPiecePosDir ( rtracks[i+1] )
it will return 0,0,0,0,0,0
Is there a way to get possition relative to parent piece ??
Spring.UnitScript.GetPieceTranslation ( piece ) -> number x, y, z

c.f. http://springrts.com/wiki/Animation-LuaCallouts
But it also return zeros, so im think it is also onli detect changes agains standard possitions...
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: [total newbie] GetPiecePosDir(attemp to call global)

Post by knorke »

There is no function to get piece position relative to unit center, unless I missed something here.

The idea of making each piece float to position of previous piece has some elegance but without easy "GetPiecePositionRelativeToUnitCenter" function realising it seems a bit cumbersome.

There is Spring.GetUnitPiecePosDir which gives the piece position in world coordinates. So one could also get unit position, and from those two calculate what you need, but watch out that you also have to take into account unit rotation around all axis.

Instead you could try setting up a table of "waypoint positions"
like
trackPosition[1] = {x=100, z=200}
trackPosition[2] = {x=110, z=205}
trackPosition[3] = {x=115, z=210}
...
and then have the piece float to next waypoint like
Move (ltracks , x_axis, trackPosition[t].x)

But how to get the waypoints without trial&error?
1) Make script that exports piece positions of spawned unit. If you spawn the unit at 0,0,0 with correct rotation, GetPiecePositionRelativeToUnitCenter is easy to calculate.

2) The units track pieces must be at correct waypoint position before export - how?
Either by setting them in 3d editor or via a simple script or with
unit poser
Post Reply

Return to “Lua Scripts”