Jump to content

lua of a model doesn't run when loading model via fpscontroller


AggrorJorn
 Share

Recommended Posts

The titel might be a bit unclear but I couldn't think of anything else. I have a candle model which you can drag into the scene. Via the attached lua file I load the particles that are used to show a small flame on top of the candle. If I drag the candle in the editor everything works fine.

 

In the fpscontroller script I load this model like the gun. The candle loads and positions itself fine, but the particles that create the light don't. It is like the lua file for the candle is not being used.

 

Is it even possible to load a model via script and that the attached script runs with it?

 

This is the script (before the loop):

--candle
candle = LoadMesh("abstract::candle.gmf",vwep)
candle:SetParent(fw.main.camera,0)
candle:SetPosition(Vec3(0.5,-0.5,1.35),0)

Link to comment
Share on other sites

require("scripts/class")

local class=CreateClass(...)

function class:CreateObject(model)
local object=self.super:CreateObject(model)

--Create light
object.light=CreatePointLight(4,model)
object.light:SetColorf(1,0.6,0.25,1,1)
object.light:SetPositionf(0,1.2,0,0)
object.light:SetShadowOffset(0,0.91,0)


	object.fire=CreateEmitter(100,100,Vec3(0,1,0),0,object.model)
	object.fire:SetPositionf(0,0.5,0,0)
	object.fire:Paint(LoadMaterial('abstract::candleflame.mat'),0)
	object.fire:SetRadius(0.02,0.02)
	object.fire:SetColorf(0.2,0.2,0.2,1,1)
	object.fire:SetWaver(0)
	object.fire:SetVelocity(Vec3(0,0.01,0),Vec3(0,0.01,0))

--Declare initial values
object.fluctuation=1.0
object.smoothedfluctuation=1.0

function object:SetKey(key,value)
	if key=="color" then
	elseif key=="intensity" then
	else
		return self.super:SetKey(key,value)
	end
	return 1
end


function object:GetKey(key,value)
	if key=="color" then
	elseif key=="intensity" then
	else
		return self.super:GetKey(key,value)
	end
	return value
end

function object:Render()
	self.fluctuation=self.fluctuation+math.random(-100,100)/1000.0*AppSpeed()
	self.fluctuation=math.min(1.0,self.fluctuation)
	self.fluctuation=math.max(0.2,self.fluctuation)
	self.smoothedfluctuation=Curve(self.fluctuation,self.smoothedfluctuation,5.0/AppSpeed())
	self.light:SetColorf(1.0*self.smoothedfluctuation,0.6*self.smoothedfluctuation,0.25*self.smoothedfluctuation,1,0)
end

end

Link to comment
Share on other sites

Rick gave me the following advice via the chat:

 

  • the lua scripts won't run if you load the object. the engine and editor has code in it to make the lua scripts run
  • you should be able to place the candle in the scene still and when you run your game, move it to the fw.camera location and offset it slightly in front to give the same effect
  • so in the candle lua file you can position the model relative to the camera
Link to comment
Share on other sites

briljant this works! It's not entirely perfect as the position sometimes changes when I go back from game mode to editor mode. However, I can just drag the candle inscreen when I'm done editing the level. here is the code I use and a screenie.

 

function class:CreateObject(model)
local object=self.super:CreateObject(model)


--position candle at the camera	
object.model:SetPosition(fw.main.camera:GetPosition(1))
object.model:SetParent(fw.main.camera,0)
object.model:SetPosition(Vec3(0.5,-0.5,1.35),0)

The flame isn't final yet. It looks more like a stick of dynamite....

Link to comment
Share on other sites

I like the feel of that. Very scary :)

 

On a side note I'm guessing that the candle is like that even when in editor mode. Which obviously is a pain. This is why I make a global string value that I set in the main lua file that tells me if I'm in editor mode or game mode. That way when editing the object doesn't get in the way.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...