Jump to content
  • entries
    943
  • comments
    5,899
  • views
    923,763

Entities and scripts


Josh

2,312 views

 Share

Below you can see the properties editor. When you select a script attached to an entity, the properties for that script appear on the right. Here we have a simple "Pulse" script that changes the color of the entity along a sine curve. This can be used for making lights that pulsate slowly, or continually turn on and off.

blogentry-1-0-57067800-1313690874_thumb.jpg

 

Here's what the script looks like. The "--in" tag at the end of the Pause and Resume functions indicate that these functions can be activated in the flowgraph editor. (A node can be connected to them to call them.)

--float speed 1
--color color0 0,0,0,0
--color color1 1,1,1,1
--bool recursive

function actor:Pause()--in
self.paused=true
end

function actor:Resume()--in
self.paused=false
end

function actor:Update()
if ~self.paused then
	local i = math.sin(AppTime()*self.speed*0.01)
	self.entity:SetColor(i*self.color0+(1-i)*self.color1,self.recursive)
end
end

Here's our "mover" script, which just performs simple movement and rotation without physics:

--vec3 translation 0,0,0
--vec3 rotation 0,0,0
--bool global true

function actor:Start()
self.paused=false
end

function actor:Pause()--in
self.paused=true
end

function actor:Resume()--in
self.paused=false
end

function actor:SetTranslation(translation)--in
self.translation = translation
end

function actor:SetRotation(rotation)--in
self.rotation = rotation
end

function actor:Update()
if ~self.paused then
	if self.movespeed~=Vec3(0) then
		self.entity:Move(self.translation[0],self.translation[1],self.translation[2],self.global)
	end
	if self.turnspeed~=Vec3(0) then
		self.entity:Turn(self.rotation[0],self.rotation[1],self.rotation[2],self.global)
	end
end
end

Want an entity to animate in a loop? Attach this script to the entity:

--int sequence
--float speed 1
--bool paused false

function actor:Start()
self.frame=0
end

function actor:Pause()--in
self.paused=true
end

function actor:Resume()--in
self.paused=false
end

function actor:Draw()
if ~self.paused then
	self.frame=self.frame+AppSpeed()
	self.entity:Animate(self.frame,self.sequence)
end
end

What if we wanted a one-shot animation that plays when something triggers it? We can define an "Enable" function, connect another node to it, and some other event can cause the script to play the animation through once. Here's "PlayOnce.lua":

--int sequence
--float speed 1
--bool enabled false

function actor:Start()
self.frame=0
self.enabled=true
end

function actor:Enable()--in
self.enabled=true
self.frame=0
end

function actor:Disable()--in
self.enabled=false
end

function actor:Draw()
if self.enabled then
	local length=self.entity:GetAnimationLength(self.sequence)
	self.frame=self.frame+AppSpeed()
	if self.frame>length then
		self.frame=length
		self.enabled=false
	end
	self.entity:Animate(self.frame,self.sequence)
end
end

By using these general-purpose scripts we can set up game interactions and interesting behaviors, without coding...but if we need to code something new, the tools are a couple clicks away.

 Share

5 Comments


Recommended Comments

how do multiple functions from different scripts work with each other? like if you attached the animation script and the mover script together on one object, how would you differentiate between the two Pause functions? this seems alot more complicated than to just create one script which handles what you need.... :)

 

and shouldn't that animation script be using actor:Draw() instead of Update()?

Link to comment

how do multiple functions from different scripts work with each other? like if you attached the animation script and the mover script together on one object, how would you differentiate between the two Pause functions?

An "actor" is a script attached to an entity, with a set of properties. Each actor has it's own set of variables in Lua that don't interfere with each other. You can have multiple actors, even actors of the same type, attached to one entity, and they are each in their own "space". In the flowgraph, you will actually be looking at actors, not entities.

 

this seems alot more complicated than to just create one script which handles what you need....

To you and me, it's trivial to just write exactly what you want a script to do. To 99% of the potential market, programming is an utter impossibility, but they can attach a few behaviors to an entity and adjust some settings. It allows game development through visual trial and error.

 

and shouldn't that animation script be using actor:Draw() instead of Update()?

Good eyes!

 

On one hand, this seems like a lot of work just to allow designers to do things without code that programmers can easily do. On the other hand, this means you can code a few scripts, give them to a designer, and let them make some game maps for you with interesting variations of the gameplay mechanisms you programmed.

Link to comment
Guest Red Ocktober

Posted

i can see where this could make a non programmer more comfortable... and a non-lua scripter (myself), more inclined to use lua...

 

this allows him/her to be able to break up the different tasks an actor might have to do into their own separate scripts...

 

question...

 

will scripts be able to share variables between themselves... and, if so, will this add to the processing overhead...

 

 

--Mike

Link to comment

I believe you can do stuff like this so all actors can access a per-entity value:

function actor:Update()
self.entity.myvalue="Hello"
end

Normally you would want to keep them in their own separate spaces, within the actor table.

 

Programming the editor to handle the multi-script attachments and properties, when scripts or objects may be reloaded at any time, is really incredibly gnarly, which is why I have never done anything like this before. :)

Link to comment
Guest Red Ocktober

Posted

excellent... that should be good enough to get the job done...

 

you've done quite a bit there young man... quite a bit...

 

 

 

--Mike

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