Page 2 of 2

Re: Super cool idea

Posted: 14 May 2010, 00:35
by Das Bruce
Btw, you don't need the brackets around the if statements conditional.

Code: Select all

if condition == 'value' then
Works just dandy.

Re: Super cool idea

Posted: 16 May 2010, 07:13
by oksnoop2
infolog http://pastebin.com/jFqAx5mx
Gadget http://pastebin.com/XktrXpZC

Getting slightly better at reading these things but this time i'm perplexed. I loaded it up and of course nothing happened, that did not surprise me, what gets me is that i don't see if anywhere in the infolog. There is no mention of spacerock. So what did i do wrong?

Re: Super cool idea

Posted: 16 May 2010, 11:18
by Pressure Line
concievably it works perfectly, just not as intended (ie it works but doesnt *do* anything)

Re: Super cool idea

Posted: 16 May 2010, 16:22
by zwzsg
enabled = false
:roll:

Also, you forgot the () after Spring.GetGaiaTeamID
And drop the -- $Id$ at the top, that's just CA versioning system.
And your code logic is flawed: Every time a meteor is created, you create a new meteor in the same place...
There should be no quoting around UnitDefNames[testUnitName].id

Re: Super cool idea

Posted: 16 May 2010, 18:33
by knorke
i am no lua expert but i think you have some errors in your code.
first the one zwzsg pointed out, when a meteor is created you create another meteor create another meteor create another meteor.

Oh thinking about it maybe you wanted to use UnitDestroyed?
So that when a meteor has died by crashing in the ground debris remains on the map. (But in this case it might be better to check for ground impact yourself instead of relaying on the springphysic to shatter the meteor on crashing down?)
I dont really get what your other UnitDestroyed function does?

also in line 42 you create a meteor
local unitID = Spring.CreateUnit("meteor", x, 0, z, "n", Spring.GetGaiaTeamID)
and then in line 44 you change its z-coordinate:
Spring.MoveCtrl.SetPosition(unitID, x, Spring.GetGroundHeight(x, z) + 10000, z)
you could just create it at (x, Spring.GetGroundHeight(x, z)+10000, z) instead of creating then moving. Unless of course Spring requires it to be done this way, but it seems one useless extra step to me.

for testing markers (http://springrts.com/wiki/Lua_UnsyncedCtrl#Markers) are quite usefull. like add a marker when/where a meteor is created or destroyed so you know whats going on.

line 23: if (name == "meteor" ) then
line 42: local unitID = Spring.CreateUnit("meteor", x, 0, z, "n", Spring.GetGaiaTeamID)
i think in cases like this you should use your variable testUnitName here instead of "meteor" so if you change the unitname you do not have to make changes all over the script and can instead just change one line.

Re: Super cool idea

Posted: 16 May 2010, 20:33
by oksnoop2
I guess the logic i'm shooting for is. A Rock is created then it falls to the ground and explodes, hurting near by units. Then just after it explodes it is replaced with a reclaimable feature. Is my code not congruent with my logic?

I switched enabled to true, and deleted that -- $Id$, when i got rid of the quotation marks around UnitDefNames[testUnitName].id i get this error in my infolog

Code: Select all

[      0] Failed to load: spacerock.lua  ([string "LuaRules/Gadgets/spacerock.lua"]:19: attempt to index field '?' (a nil value))

After reading in that tutorial that was linked here i know that nil means nothing. So it's fouling up there because it's calling for nothing...or something that's not there?

Re: Super cool idea

Posted: 16 May 2010, 22:32
by knorke
I guess the logic i'm shooting for is. A Rock is created then it falls to the ground and explodes, hurting near by units. Then just after it explodes it is replaced with a reclaimable feature. Is my code not congruent with my logic?
wouldnt you need something like falling_meteor and crashed_meteor then instead of just meteor?

And calling CreateUnit in the UnitCreated callin will cause UnitCreated to be once again. Then another unit will be created by CreateUnit. This will cause UnitCreated to called etc. A never ending circle!

Re: Super cool idea

Posted: 17 May 2010, 00:37
by SirMaverick
knorke wrote:And calling CreateUnit in the UnitCreated callin will cause UnitCreated to be once again. Then another unit will be created by CreateUnit. This will cause UnitCreated to called etc. A never ending circle!
It will fail at 2nd call to CreateUnit and you will see a error message stating that recursion is not permitted.

Re: Super cool idea

Posted: 17 May 2010, 08:02
by knorke
ah thats clever if the parser sees that.
but this:

Code: Select all

function widget:AddConsoleLine(msg, priority)
Spring.Echo ("blabla")
end
will spam your chat though.

Re: Super cool idea

Posted: 18 May 2010, 02:16
by SirMaverick
knorke wrote:will spam your chat though.
Just spam. Not freeze spring as UnitCreated would with recursion.

Re: Super cool idea

Posted: 23 May 2010, 05:21
by oksnoop2

Code: Select all

local testUnitDefID = UnitDefNames[testUnitName].id
What is this and what does it do in this gadget? I don't get its reason to exist.

Re: Super cool idea

Posted: 25 May 2010, 15:56
by zwzsg
It means that after that line, you can type testUnitDefID instead of UnitDefNames[testUnitName].id. Its reason to exist is to make your code shorter and easier to maintain. Well, in theory, because in practice it often does exactly the contrary. Not sure I explained anything there :roll: . At a lower level, local testUnitDefID = UnitDefNames[testUnitName].id creates a variable named testUnitDefID that contains the value of UnitDefNames[testUnitName].id at the time.

Re: Super cool idea

Posted: 02 Jun 2010, 00:18
by oksnoop2
I cut out a lot of crap that looked redundant or otherwise not useful.

http://codepad.org/sXxEJ5kU --inflog

http://pastebin.com/kNCJh5KJ --current gadget

I feel pretty close with this iteration, been working at it for a while but i don't know what to do with these errors.

Re: Super cool idea

Posted: 02 Jun 2010, 00:30
by quantum
Change Spring.GetGaiaTeamID to Spring.GetGaiaTeamID().

The omitting the () means you will use the function itself rather than what the function returns.

Re: Super cool idea

Posted: 07 Jun 2010, 00:04
by oksnoop2
http://pastebin.com/3wWvasLb gadget
http://pastebin.com/dDXyfxHL unit
So i gutted the gadget even more so and tada it works kind of.

I'm using a wall unit def for the time being because it automatically turns into feature. So right now it's spawning falling crap from the sky and wrecks are landing on the ground. What it's not doing yet.

Don't know how to make different meteors fall and how to change the rates, times, and locations they fall.
Not making any sound during the fall.
No kicking smoke trail on entry.
No damaging explosion on impact.
Edit: and how to adjust the rate at which it falls
So yeah just felt compelled to give an update, any comments or suggestions about my current problems are welcome. Feels good to be almost done with this stupid thing!

Re: Super cool idea

Posted: 11 Jun 2010, 07:02
by quantum
Fixed some stuff, but there is still no sound during the fall.

You need to replace the unit and the effects in the settings with suitable ones.

Code: Select all

function gadget:GetInfo()
  return {
    name      = "Space Rock",
    desc      = "Spawns rocks from space.",
    author    = "oksnoop2",
    date      = "4/24/2010",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true,  --  loaded by default?
  }
end


if not gadgetHandler:IsSyncedCode() then
  return false -- no unsynced code
end


----- Settings -----------------------------------------------------------------

local meteorDefName  = "f1" -- meteor unit name, use a unit with a death explosion
local meteorInterval = 1 -- time between the arrival of meteors, in seconds
local fallGravity = 1
local meteorSpawnHeight = 5000
local burnEffect1 = "smallburn" -- CEG used for the meteor trail, needs to be visible out of los
local burnEffect2 = "medburn"

----------------------------------------------------------------------------------


local meteors = {} -- meteor set

function gadget:GameFrame(frame)

  if frame % (30 * meteorInterval) == 0 then 
    -- pick a random location as the meteor spawn point
    local meteorSpawnX = math.random(Game.mapSizeX)
    local meteorSpawnZ = math.random(Game.mapSizeZ)
    local meteorSpawnY = Spring.GetGroundHeight(meteorSpawnX, meteorSpawnZ) + meteorSpawnHeight
    
    -- create the meteor
    local meteorID = Spring.CreateUnit(meteorDefName, meteorSpawnX, meteorSpawnY, meteorSpawnZ, "n", Spring.GetGaiaTeamID()) -- will ignore Y and spawn at ground level
    meteors[meteorID] = true -- put it in the meteor set
    
    Spring.MoveCtrl.Enable(meteorID) -- tell spring we'll take care of moving the meteor
    Spring.MoveCtrl.SetPosition(meteorID, meteorSpawnX, meteorSpawnY, meteorSpawnZ) -- this time the height will be set correctly
    Spring.MoveCtrl.SetGravity(meteorID, fallGravity) -- make gravity affect the meteor
  end
  
  for meteorID in pairs(meteors) do  -- loop through every meteor in the meteor set
    local x, y, z = Spring.GetUnitPosition(meteorID)
    local h = Spring.GetGroundHeight(x, z)
    if y < h then -- if the meteor below ground level
      Spring.DestroyUnit(meteorID) -- make it explode
      meteors[meteorID] = nil -- remove it from the meteor set
    else -- above ground, show the meteor trail
      Spring.SpawnCEG(burnEffect1, x, y + 30, z)
      Spring.SpawnCEG(burnEffect2, x, y, z)
    end
  end
end

Image