Author Topic: Rendering a model between player and target (grappling hook related)  (Read 4900 times)

0 Members and 1 Guest are viewing this topic.

Offline redsniper

  • 211
  • Aim for the Top!
Rendering a model between player and target (grappling hook related)
A while ago in this thread, WMC threw together a mock-script, that's supposed to render a model between the player and a ship hit with a specific weapon. I've learned a little about the scripting system since then, but it's still pretty mysterious to me, and was wondering if someone could look at this and tell me what needs to be done to make it a workable script. Currently I get error messages about nil values, which I suppose could be fixed by initializing some of the variables, but I'm not sure of the exact best way to do that with FSO. Thanks.
Code: [Select]
#Conditional Hooks
$Application: Freespace 2
$On Game Init: [
GrappleHook.Grapples = {}
GrappleHook.HookModel = "hook"

function GrappleHook.addGrappleHook(weapon, shipHit)
local newGrapple = {}

--Don't add grapples if
--(1) Nil was passed
--(2) Weapon is dead
--(3) Weapon has no parent
if not weapon or not weapon:isValid() or not weapon.Parent:isValid()
return
end

--This is inaccurate - no real way to tell where this
--grapple came from on the source ship right now, besides
--explicitly telling it in the script
newGrapple.startShip = weapon.Parent
newGrapple.StartLocalPosition = ba.newVector(0, 0, 0)

--This is more accurate
newGrapple.EndShip = shipHit
newGrapple.EndLocalPosition = weapon.Position - shipHit.Position
--I don't remember if this is right or not
newGrapple.EndLocalOrientation = weapon.Orientation * shipHit.Orientation:getTranspose()

table.insert(GrappleHook.Grapples, newGrapple)
end

function GrappleHook.doFrame()
for k,g in pairs(GrappleHook.Grapples) do
--Renders grappling hook in the same position that it hit at
gr.drawModel(GrappleHook.HookModel, g.Endship.Position + g.EndLocalPosition, g.EndShip.Orientation * g.EndOrientation)
end
end
]

$Application: Freespace 2
$State: GS_STATE_PLAY_GAME
$On Frame: [
GrappleHook.doFrame()
]

$Application: Freespace 2
$Weapon Class: Grapple hook
$On Ship Collision: [
GrappleHook.addGrappleHook(hv.Weapon, hv.Ship)
]
#End
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline Wanderer

  • Wiki Warrior
  • 211
  • Mostly harmless
Re: Rendering a model between player and target (grappling hook related)
Hmm... Seems like he using 'GrappleHook' named table (IIRC only lua data structure). Though I'm not sure what is the gain. You could try adding 'GrappleHook = {}' just before the 'GrappleHook.Grapples = {}'.

I would probably also replace the 'GrappleHook.doFrame()' in the GS_STATE_PLAY_GAME after On Frame: with something like

Code: [Select]
time = mn.getMissionTime()

if time ~= nil and GrappleHook ~= nil then
   GrappleHook.doFrame()
end
Do not meddle in the affairs of coders for they are soggy and hard to light

 

Offline redsniper

  • 211
  • Aim for the Top!
Re: Rendering a model between player and target (grappling hook related)
Well, it don't get error messages anymore, but I'm not seeing any models rendered either.

This is the current script:
Code: [Select]
#Conditional Hooks
$Application: Freespace 2
$On Game Init: [
GrappleHook = {}
GrappleHook.Grapples = {}
GrappleHook.HookModel = "cable.pof"

function GrappleHook.addGrappleHook(weapon, shipHit)
local newGrapple = {}

--Don't add grapples if
--(1) Nil was passed
--(2) Weapon is dead
--(3) Weapon has no parent
if not weapon or not weapon:isValid() or not weapon.Parent:isValid()then
return
end

--This is inaccurate - no real way to tell where this
--grapple came from on the source ship right now, besides
--explicitly telling it in the script
--newGrapple.startShip = weapon.Parent
--newGrapple.StartLocalPosition = ba.newVector(0, 0, 0)

--This is more accurate
newGrapple.EndShip = shipHit
newGrapple.EndLocalPosition = weapon.Position - shipHit.Position
--I don't remember if this is right or not
newGrapple.EndLocalOrientation = weapon.Orientation * shipHit.Orientation:getTranspose()

table.insert(GrappleHook.Grapples, newGrapple)
end

function GrappleHook.doFrame()
for k,g in pairs(GrappleHook.Grapples) do
--Renders grappling hook in the same position that it hit at
gr.drawModel(GrappleHook.HookModel, g.Endship.Position + g.EndLocalPosition, g.EndShip.Orientation * g.EndOrientation)
end
end
]

$Application: Freespace 2
$State: GS_STATE_PLAY_GAME
$On Frame: [
time = mn.getMissionTime()

if time ~= nil and GrappleHook ~= nil then
GrappleHook.doFrame()
end
]

$Application: Freespace 2
$Weapon Class: Grapple
$On Ship Collision: [
GrappleHook.addGrappleHook(hv.Weapon, hv.Ship)
]
#End
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Rendering a model between player and target (grappling hook related)
You have to use the model render function in the On Object Render function. By the time the On Frame hook executes, it's already reset the 3D environment data to render the 2D HUD. You can use the On Frame function to reset a variable that you use to make sure you only render models with scripting once per frame. The On Object Render function is called every time an object renders, so if you rendered a model without checking for a variable, you'd be rendering it the same number of times as objects.

There may be an issue related to objects not being on the screen. If that's the case then try using the override hook to render the function (but return false, so the object you're leeching off of still renders).
-C

 

Offline redsniper

  • 211
  • Aim for the Top!
Re: Rendering a model between player and target (grappling hook related)
:confused:
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Rendering a model between player and target (grappling hook related)
-C

 

Offline redsniper

  • 211
  • Aim for the Top!
Re: Rendering a model between player and target (grappling hook related)
So... I need something like:
Code: [Select]
$On Object Render: [
gr.drawModel(...) ]
and
Code: [Select]
$On Frame: [
GrappleHook = ????? (something to reset it) ]
?
I'm not sure I understand what you're saying.
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Rendering a model between player and target (grappling hook related)
Ahh...

What I meant was that you can just do this:
Code: [Select]
$On Object Render: [
gr.drawModel()
]

However, it'll render that model every time an object is rendered (that's what that hook does - runs right after an object is rendered). So in order to make sure that anything in that hook doesn't render multiple times, you can set a variable to a value the first time that the hook is called. However, you need a way to reset it at the end of every frame, so drawModel still gets called every frame. So you want something like

Code: [Select]
$On Object Render: [
   if not g_Rendered then
      gr.drawModel()
   end
]

$On Frame: [
   g_Rendered = nil
]

That way every frame the rendered variable gets reset, so it doesn't exist for the first object render in the next frame.
-C

 

Offline redsniper

  • 211
  • Aim for the Top!
Re: Rendering a model between player and target (grappling hook related)
So now I get this:

LUA ERROR: [string "scripting.tbl - On Game Init"]:34: attempt to index field 'Endship' (a nil value)

As far as I can tell, this
Code: [Select]
$Application: Freespace 2
$Weapon Class: Grapple
$On Ship Collision: [
GrappleHook.addGrappleHook(hv.Weapon, hv.Ship)
is passing hv.Ship to the addGrapple function as shipHit, which is the source of the error. What exactly should I use in its place in order to refer to the ship that's been hit by a the weapon? or am I way off here?
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Rendering a model between player and target (grappling hook related)
That should be right, looking at the online SVN repo. Unless there's something wrong with the SetHookObjects function, it looks like everything there is OK.

Are you sure hv.Ship is nil? The only time that hook should be called is after the variable has been set.
-C

 

Offline redsniper

  • 211
  • Aim for the Top!
Re: Rendering a model between player and target (grappling hook related)
I'm not sure of anything. The error message says that Endship is a nil value which, as far as I can tell, should be equal to hv.Ship.
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Rendering a model between player and target (grappling hook related)
I don't see anything wrong with what you've posted. I would make sure that all the doFrame items are actually there and figure out what's causing EndShip to not get set properly, and check to make sure that hv.Ship is always returning a non-nil value and that the value is getting set in the Grapples list properly.
-C

 

Offline redsniper

  • 211
  • Aim for the Top!
Re: Rendering a model between player and target (grappling hook related)
So I'm trying this again with a newer build and apparently the nightly builds don't have the drawModel function. Did it just get moved somewhere else, or is there a different way of drawing models now? I've looked through the scripting.html that it spits out and I'm not seeing anything like that.
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Rendering a model between player and target (grappling hook related)
Unfortunately, I think that was part of the post-August changes I made which never made it into the main branch. That codebase is available on the Diaspora internal, and can now be publicly distributed, but somebody would need to copy that function from the post-August branch and put it in SVN and test it (I'm not going to). There are other changes in that branch that might be desirable (eg FTL effect upgrades for the BSG and Homeworld mods).
-C

 

Offline redsniper

  • 211
  • Aim for the Top!
Re: Rendering a model between player and target (grappling hook related)
So I'm SOL until someone puts it back in the main builds, or I have to use an older build?
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Rendering a model between player and target (grappling hook related)
Unfortunately, yes.
-C

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Rendering a model between player and target (grappling hook related)
Unfortunately, I think that was part of the post-August changes I made which never made it into the main branch. That codebase is available on the Diaspora internal, and can now be publicly distributed

To be honest, I'm not certain it can be actually. WMC left specific instructions as to its use and I'm still waiting on confirmation from MatthewPapa about one aspect before I can get it out there.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]