Jump to content
  • entries
    945
  • comments
    5,899
  • views
    929,914

December 7, 2009


Josh

2,690 views

 Share

It's 12:30 in the morning, but I have the model scripts working the way I want. Thanks to "Nilium" for his tips, and to everyone who gave their feedback. I am confident in this revision. I have a lot of scripts that need to be slightly altered, but it's just a matter of adjusting the function structure, not really changing any existing code.

 

Here's the light_ambient script:

require("scripts/class")
require("scripts/linkedlist")

local class=CreateClass(...)

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

function object:Update()
	AmbientLight(self.model.color)
end

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

function object:Kill(model)
	local model,object

	--Iterate through all instances of this class.
	--If any instances are found, use them to set
	--the ambient light value.
	for model,object in pairs(self.class.instances) do
		if object~=self then
			object:Update()
			break
		end
	end
	self.super:Kill()
end

object.model:SetKey("intensity","0.5")

--Call the update function before finishing.
--This can only be called after the function is declared.
--We don't actually need to call it here since setting the intensity key
--will do that for us, but it's still a good idea as a general rule.
object:Update()
end

function class:Cleanup()
--Restore default ambient light setting.
AmbientLight(Vec3(0.5,0.5,0.5))
self.super:Cleanup()
end

 Share

7 Comments


Recommended Comments

I think that looks pretty good. So how would we go about making global variables with this new single state design since that would be one of the benefits.

Link to comment

Oh, I thought you were worried about global names colliding, but that works for me. We still might run into issues with custom objects that we all create. Might encourage some kind of variable naming convention. Those kind of bugs might be tough to find.

Link to comment

So you want global variables, but you don't?

 

I suggest storing any per-class variable as member of the class:

 

class.myglobal=3

 

That should keep your per-class variables out of trouble, and your other scripts can still access them.

Link to comment

I'm loving it. Seems cleaner than the original implementation, yet it adds functionality. Can't beat that. When do you think it will be available for us to start playing with it?

Link to comment

So you want global variables, but you don't?

 

I suggest storing any per-class variable as member of the class:

 

class.myglobal=3

 

That should keep your per-class variables out of trouble, and your other scripts can still access them.

 

 

I want global variables but with a way to avoid naming collisions. Something like namespaces.

 

Are all these classes global? So if I create a variable apart of the class I can access it via the class in another file?

Link to comment
Guest
Add a comment...

×   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.

×
×
  • Create New...