Jump to content

LUA GetKey, SetKey - How to use?


Scott Richmond
 Share

Recommended Posts

What I want to do is set some custom entity keys in C++ (easy) and than retrieve those keys and values in the LUA Update for each entity and do some work. Unfortunately I'm struggling to work out how the LUA Set/GetKey command works:

 

function base_GetKey(model,key,value)
local entity=entitytable[model]
local intensity,r,g,b,a

if entity==nil then return value end
if entity.model==nil then return end
if key=="position" then
	return entity.model.position.x..","..entity.model.position.y..","..entity.model.position.z
elseif key=="mass" then		
	return entity.model.mass
elseif key=="sweptcollision" then
	return entity.model:GetSweptCollisionMode()
elseif key=="rotation" then
	return entity.model.rotation.x..","..entity.model.rotation.y..","..entity.model.rotation.z 
elseif key=="buoyancy" then
	return entity.model.buoyant
elseif key=="scale" then
	return entity.model.scale.x..","..entity.model.scale.y..","..entity.model.scale.z 
elseif key=="gravity" then
	return model.usegravity
elseif key=="friction" then
	return model.staticfriction..","..model.kineticfriction
elseif key=="color" then
	intensity=tonumber(entity.model:GetKey("intensity","1"))
	return (entity.model.color.x*255.0/intensity)..","..(entity.model.color.y*255.0/intensity)..","..(entity.model.color.z*255.0/intensity)..","..(entity.model.color.w*255.0)
elseif key=="castshadows" then
	return entity.model:GetShadowMode()
elseif key=="material" then
	if entity.model.material~=nil then
		return entity.model.material:GetName()
	end
elseif key=="active" then
	if entity.active==0 then return 0 else return 1 end
elseif key=="enabled" then
	if entity.enabled==0 then return 0 else return 1 end
end
return value
end

 

I've pulled this from the base LUA file. How do I specify a model and key, and get the value? It sort of looks like this function doesn't really pull down keys that aren't already defined right there.

 

Any help would be great. :)

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

Link to comment
Share on other sites

The value parameter is the value that is stored for the key. The Lua function can be used to override that value. Normally the Lua GetKey() function just returns the value it receives. When any of those special keys are encountered, the return value is something else.

 

You can retrieve keys from any entity at any time in Lua like this:

 

entity:GetKey("mykey")

 

Internally this will first look up the key for "mykey", then run it through the "GetKey()" script function and return whatever value that returns.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Still having some through setting and getting keys. Heres my simple code for an animated model:

--Include the base script for all classes
dofile("Scripts/base.lua")

--This function builds the interface for the properties editor
function InitDialog(grid)

--Add the base interface for all classes
base_InitDialog(grid)

--Now we can add our own custom properties

end

--Spawn function for creating new instances
function Spawn(model)
local entity=base_Spawn(model)

entity:SetKey("State", "IDLE")

end

--Update function, called during every UpdateWorld()
function Update(world)
currentState = entity:GetKey("State")
frame = (AppTime()/100.0) % (endframe-startframe) + startframe

if currentState == "IDLE" then
	endframe = 100
	startframe = 1
	frame = (AppTime()/100.0) % (endframe-startframe) + startframe
	entity.model:Animate(frame,blend,0,1)

end

end

 

engine.log says:

Lua error: ...nd keeper/data/models/characters/crawler/crawler.lua:18: attempt to call method 'SetKey' (a nil value)

 

What am I doing wrong here?

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

Link to comment
Share on other sites

Thanks for your input Rick, but I worked it out a few minutes ago. The reason was that the Update function didn't seem to have access to the entity/model globals (if they are globals?). Either way I took a page out of the Windmill.lua book and its all working fine now. FYI - Simple script to do animation based on an entities 'State' key:

--Include the base script for all classes
dofile("Scripts/base.lua")

--This function builds the interface for the properties editor
function InitDialog(grid)

--Add the base interface for all classes
base_InitDialog(grid)

--Now we can add our own custom properties

end

--Spawn function for creating new instances
function Spawn(model)
local entity=base_Spawn(model)

entity.model:SetKey("State", "IDLE")

--An update function for one instance. Declaring the function as part of the entity table allows us to use "self" for the table
function entity:Update()
	currentState = entity.model:GetKey("State")

	-- Animation frames: Idle 0-69 Walk 70-130 Run 131-171

	if currentState == "IDLE" then
		endframe = 69
		startframe = 0
		blend = 1.0
		frame = (AppTime()/100.0) % (endframe-startframe) + startframe
		entity.model:Animate(frame,blend,0,1)
	end
end
end

--Update function, called during every UpdateWorld()
function Update(world)
if world==world_main then
	local model,entity
	for model,entity in pairs(entitytable) do
		if model.world==world then
			entity:Update()
		end
	end
 end
end

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

Link to comment
Share on other sites

Looks like you haven't created a SetKey function. Look at the switch.lua script to see one example of it's use. Your SetKey function could be as simple as:

 

function SetKey(model,key,value)
return base_SetKey(model,key,value)

 

but usually you'll want to add some code in there that will actually look at the key and value sent and do something to an entity variable. So something like:

 

function SetKey(model,key,value)
local entity=entitytable[model]
if entity==nil then return 1 end
if key=='State' then
	entity.state = value
       else
	return base_SetKey(model,key,value)
end
return 1
end

 

You'll also need to implement GetKey

Windows 7 x64 - Q6700 @ 2.66GHz - 4GB RAM - 8800 GTX

ZBrush - Blender

Link to comment
Share on other sites

Thanks for your input Rick, but I worked it out a few minutes ago. The reason was that the Update function didn't seem to have access to the entity/model globals (if they are globals?). Either way I took a page out of the Windmill.lua book and its all working fine now. FYI - Simple script to do animation based on an entities 'State' key:

--Include the base script for all classes
dofile("Scripts/base.lua")

--This function builds the interface for the properties editor
function InitDialog(grid)

--Add the base interface for all classes
base_InitDialog(grid)

--Now we can add our own custom properties

end

--Spawn function for creating new instances
function Spawn(model)
local entity=base_Spawn(model)

entity.model:SetKey("State", "IDLE")

--An update function for one instance. Declaring the function as part of the entity table allows us to use "self" for the table
function entity:Update()
	currentState = entity.model:GetKey("State")

	-- Animation frames: Idle 0-69 Walk 70-130 Run 131-171

	if currentState == "IDLE" then
		endframe = 69
		startframe = 0
		blend = 1.0
		frame = (AppTime()/100.0) % (endframe-startframe) + startframe
		entity.model:Animate(frame,blend,0,1)
	end
end
end

--Update function, called during every UpdateWorld()
function Update(world)
if world==world_main then
	local model,entity
	for model,entity in pairs(entitytable) do
		if model.world==world then
			entity:Update()
		end
	end
 end
end

 

 

I'm not a big fan of that way because if you create any other models in this script file, they will all call this Update() method you have and you may not want that. Just something to think about.

Link to comment
Share on other sites

Thanks for the replies guys. I do agree and I'm trying to improve it but I'm still having issues. The current script I have is:

--Include the base script for all classes
dofile("Scripts/base.lua")

--This function builds the interface for the properties editor
function InitDialog(grid)

--Add the base interface for all classes
base_InitDialog(grid)

--Now we can add our own custom properties

end

--Spawn function for creating new instances
function Spawn(model)
entity=base_Spawn(model)

SetKey(entity, "State", "IDLE")
end

--Update function, called during every UpdateWorld()
function Update(world)
currentState = GetKey(entity, "State", "NONE")

-- Animation frames: Idle 0-69 Walk 70-130 Run 131-171

if currentState == "IDLE" then
	endframe = 69
	startframe = 0
	blend = 1.0
	frame = (AppTime()/100.0) % (endframe-startframe) + startframe
	entity.model:Animate(frame,blend,0,1)
end
end

function SetKey(model,key,value)
return base_SetKey(model,key,value)
end

function GetKey(model,key,value)
return base_GetKey(model,key,value)
end

 

Engine.log doesn't report any problems, yet the model doesn't animate which suggests Get/Setkey isn't working as expected. My guess is that I'm not doing those correctly. I'll admit I'm not sure I get the entity and model objects. I'm still confused as to why I have to define the Get/Setkey functions in the first place, aren't they already defined in base.lua? Or are those functions local only?

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

Link to comment
Share on other sites

To be honest I don't know. I'm not even sure that the code you pasted there does. I'm only just getting into LUA. Any explanations would be greatly appreciated. :)

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

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