Jump to content
  • Advertisement
Sign in to follow this  
Tutorial Doctor

I have some new habits.

This topic is 2014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I have some new programming habits. I need to know if they are practical.

 

Why use True/False?

on = True
off = False
 
if light.on then
     JumpForJoy()
end

Sometimes I don't like the names of standard functions:

function Display(text)
    print(text)
end

I really like to use Synonyms for functions that do similar but different tasks. 

Begin()
Start()
Commence()
Play()
 
End()
Finish()
Conclude()
Stop()

Being descriptive with my argument names:

function Walk(speed)
       speed = true
end
 
if slowly then
     MakeASadPuppyFace()
end
 
Walk(slowly)

 

Anyone have any peculiar programming habits that could be of some help?

 

In other words, any way I can make my code more human-readable?

Edited by Tutorial Doctor

Share this post


Link to post
Share on other sites
Advertisement

I don't understand what's going on here.

 

1) In your first code snippet, you define on and off (Lua uses lowercase true and false keywords), but not light. Saying

if (light.on) then

is the same as

if (light.on == true) then

if that's what you're asking.

If you mean using

if (light == true) then

then to me, that's not clear as "light" would be an an instance of a light object. light.on or lightOn would be better.

 

2) As you're using Lua, you could just write

local display = print;

This also preserves print's vararg ability.

 

3) As long as you're clear and consistent (if you use "start" in one place, you use it in all others, and the correct antonym "stop" in all others too), this doesn't matter.

 

4) What is going on here? Your function won't work properly because only tables, userdata and functions are passed by address in Lua. Also "slowly" is an unclear name.

Share this post


Link to post
Share on other sites

Good points. 

 

the code:

if light.on then
 
end

is an example of how I might use "on" to mean "true."

 

so the code would be :

light.on = true
light.off = false
 
if light.on then
 
end

This is distinguished from:

fan.on = true
fan.off = false

I have actually used the quickly and slowly adjectives by assigning numerical values to them, which set the speed of the walk. 

if Walk(slowly) then
velocity = 1
end
 
if Walk(quickly) then 
velocity = 3
end

Note: I am using the word velocity instead of speed here (my actual code is not so confusing, I will post a snippet in another reply.) I also don't set the velocity this way, but this is one way I could do it. 

 

The walk function would return the boolean value:

function Walk(speed)
    speed = true
    return speed
end

I used this for my senses system so that I could type:

if See(door) then
   TurnLeft()
end

Where the See() function checks a collision between an object and the door object, and returns true if the collision happens. 

 

Then I can see all types of things. 

if See(person) then
if See(wall) then

Slowly and quickly are interpretations of the numerical values. For instance, when you think "slowly" you think not as fast as "quickly."

 

I can explicitly change the interpretation by adjusting the numerical value to make quickly mean "3 times as fast." 

Edited by Tutorial Doctor

Share this post


Link to post
Share on other sites

Example from a working script:

-----------------------------------------------------------------------------------
-- Maratis
-- Jules script test
-----------------------------------------------------------------------------------
on = true
off = false

-- Get objects
Player = getObject("Player")
Head = getObject("Head")
eyes = getObject("Eyes")
ears = getObject("Ears")
box = getObject("Box")
monkey = getObject("Monkey")
building = getObject("Building")
smallBox = getObject("small box")
door = getObject("door")

soundplay1 = off
soundplay2 = off
soundplay3 = off
soundplay4 = off


seeBox = getObject("SeeBox")--sound
seeMonkey = getObject("SeeMonkey")--sound
seeBuilding = getObject("SeeBuilding")--sound
tack= getObject("Tack")--sound
rightway = getObject("rightway")

--Text Overlay
camera = getObject("Camera0")
gui = getScene("gui")

--Get text Objects from another Scene to overlay on first scene
see = getObject(gui,"see")
touch = getObject(gui,"touch")
hear = getObject(gui,"hear")
collect = getObject(gui,"collect")
state = getObject(gui,"state")
hear_distance =getObject(gui,"hearDistance")

--Project a scene over a camera
enableCameraLayer(camera,gui)

--Emotional States
mood = 5 --initial mood (normal)
sad = false
happy = false
normal = true

--setText(txt_readout,distance)


--SENSES
--SEE
function See(object)
	if isCollisionBetween(eyes,object) then
		seenObject = getName(object)
		setText(see, "I see a ".. seenObject)
		seeing = true
		
		return true
	else 
		--setText(see,"I see nothing")
		seeing = false
	end
end

--TOUCH
function Touch(object)
	if isCollisionBetween(Player,object) then
		touchedObject = getName(object)
		setText(touch, "I feel a ".. touchedObject)
		touching = true
	else setText(touch,"I feel nothing")
		touching = false
	end
end

--SPEAK
function Speak()
	if See(box) then
		if soundplay1 == off then
			playSound(seeBox)
		end
		soundplay1 = on
	else 
		soundplay1 = off
	end
	
	if See(monkey) then
		if soundplay2 == off then
			playSound(seeMonkey)
		end
		soundplay2 = on
	else
		soundplay2 = off
	end
	
	if  See(building) then
		if soundplay3 == off then
			playSound(seeBuilding)
		end
		soundplay3 = on
	else
		soundplay3 = off
	end
	
	if  See(door) then
		if soundplay4 == off then
			playSound(rightway)
		end
		soundplay4 = on
	else
		soundplay4 = off
	end
end

--ACTIONS
function Jump(object)
	if seeing then
		setGravity({0,0,-4})
		addCentralForce(object,{0,0,9},"local")
	end
end

function Destroy(r,s,t)
	deactivate(r)
	deactivate(s)
	deactivate(t)
end

function Collect(object)
	if isCollisionBetween(Player,object) then
		Destroy(object)
		playSound(tack)
		setText(collect,"I collected a " .. getName(object))
	end
end


--mood = {sad = false, normal = false, happy = false}
--mood.sad, mood.normal, moode.happy

function ChangeEmotion()
	
	if mood >=0 and mood < 4 then
		sad = true
		normal = false
		happy = false
		--setText(state,"I am sad")
	end
		
	if mood>=4 and mood <=6 then
		sad = false
		normal = true
		happy = false
		--setText(state,"I am normal")
	end
		
	if mood >6 and mood <=10 then
		sad = false
		normal = false
		happy = true
		--setText(state,"I am happy")
	end
end

function TriggerEmotionChange()
	if See(box) then
		mood = 10
		setText(state, getName(box) .. "es " .. "make me glad")
	elseif See(monkey) then
		mood = 3
		setText(state, getName(monkey) .. "s " .. "make me sad")
	elseif See(building) then
		mood = 5
		setText(state, getName(building) .. "s " .. "make me turn around in circles")
	
	elseif See(door) then
		mood = 5
		setText(state, "This is a locked " .. getName(door))
	else
		setText(state,"Thinking...")
	end
end


-- Scene Update
function onSceneUpdate()
	ChangeEmotion()
	TriggerEmotionChange()
	Move()
	
	Touch(building)
	Touch(monkey)
	Touch(box)
	Touch(door)
	
	See(box)
	See(monkey)
	--See(building)
	Speak()
	
	Hear(monkey)
	Collect(smallBox)
	--Collect(box)
	--Collect(monkey)
	See(door)
	
	--Jump(Player)
	--Destroy(txt_seeing,txt_hearing,txt_touching)
	
end

Edited by Tutorial Doctor

Share this post


Link to post
Share on other sites
light.on = true
light.off = false
 
if light.on then
 
end

This could get confusing. When you want to toggle the on/off state, you have to remember to set both so that you don't have a light that's both on and off. Also, off is implied by not on and so isn't even required.

 

 

Your Walk() example is confusing too. It returns true in all cases and isn't clear what's going on. For the see example, using "canSee(object)" would be better as just "see(object)" could imply that you want to look at the object instead.

 

 

In your full code example, your functions like "see" and "touch" are doing multiple things and as I said, from their name aren't clear on purpose.

Share this post


Link to post
Share on other sites

For the see example, using "canSee(object)" would be better as just "see(object)" could imply that you want to look at the object instead.

 

Great suggestion! I actually threw together that script without considering how it would sound grammatically (I do have to fix this). I should be returning "seen" not true. 

 

I was thinking of 

if Seen(object)

but I want to use the word "seen" as a Boolean.

 

something like 

if boy.seen 

The see() and touch() functions are doing multiple things for the purposes of the level I am doing, but all they basically do is get a collision and return a Boolean. 

 

Now that I think about it, I need to compile a Demo of my Senses system (although I wanted to clean it up first). 

 

Edit: I could do: 

if boy.Sees(object) then
Edited by Tutorial Doctor

Share this post


Link to post
Share on other sites

Is lua different to where

light.on = true
light.off = false

isnt just setting attribute values?? 

Share this post


Link to post
Share on other sites

Tutorial doctor this is all an ellaborate ruse isn't it? No one in their right mind would share such valuable programming techniques.

Share this post


Link to post
Share on other sites

How the Synonyms are used:

menu = Menu() --Creates a new menu
menu.Start() --Starts the menu
menu.End() --Stops the menu
----------------------------------------------------------------------------------------

--getScene("sceneName")
--getScenesNumber()

cut_scene = newCutScene() --Creates a new cut-scene
cut_scene.Play()		 --Plays the new cut-scene
cut_scene.Stop() --Stops the new cut-secene

--getScene("sceneName")
--getScenesNumber()

cut_scene_2 = newCutScene() --Creates a new cut-scene
cut_scene_2.Play()	changeScene(scene) --Plays the new cut-scene
cut_scene_2.Stop() --Stops the new cut-secene
-------------------------------------------------------------------------------------
--loadLevel("levels/myLevel.level")

level1 = Level() --Creates a new level
level1.Begin() = 		loadLevel("levelName") --Starts the new level
level1.Finish() --Stops the new level
-------------------------------------------------------------------------------------
--getObject(« objectName »)
--activate(object)
--deactivate(object)
--isVisible(object)
--isActive(object)

prop1 = Prop()=	--Creates a new prop
prop1.Create(room) --Creates the mesh for the new prop
prop1.Destroy()
Edited by Tutorial Doctor

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!