Topic: Adding 1 to any variable.
Hey! I thought this was a useful tutorial so I'm gonna put it up.
If you want to add, lets say 1 bullet to a variable then first you need to set up a variable for the bullet before the function onSceneUpdate().
So first you write:
bullet = 0 (remember that the variable "Bullet" is not the same as "bullet")
and then the next part is to add increment. You do it like this.
Write a function for it before the onSceneUpdate():
function increment(i)
return i + 1
end
Then, inside onSceneUpdate function you write:
if onKeyDown("R") then
bullet = increment(bullet)
end
That will increase the bullets value by one. And if you want to see it happen in-game then just add a text to maratis from a true-type file (.ttf) and write Text0 = getObject("Text0") in the first row. And then inside onSceneUpdate function write:
setText(Text0, "Bullet: " .. bullet)
This will first set the text Text0 to the string (letters) "Ammo: " and then after that it will put the variable bullet. The two dots is for separating the string "Ammo: " and the variable bullet. So any time you want to separate any kind of texts or variables you have made just make two dots inbetween.
Next off if you want to decrease bullet, just add a decrement function before the onSceneUpdate function:
function decrement(i)
return i - 1
end
and write following code inside onSceneUpdate() function:
if onKeyDown("MOUSE_BUTTON1") then
bullet = decrement(bullet)
end
this will decrease the bullets value with 1.
This is the full code written together:
Text0 = getObject("Text0")
bullet = 0
function increment(i)
return i + 1
end
function decrement(i)
return i - 1
end
function onSceneUpdate()
if onKeyDown("R") then
bullet = increment(bullet)
end
if onKeyDown("MOUSE_BUTTON1") then
bullet = decrement(bullet)
end
end
Play around with it! See what you can do!
(Say what you think about the tutorial so I can fix any errors or any lack of clarity you might see)
Last edited by Pär (2013-01-13 12:58:09)