Don't make a tile engine more complex than it is - it's just a table with a bunch of tables inside it and each entry (tile) has some properties.
Printable View
Don't make a tile engine more complex than it is - it's just a table with a bunch of tables inside it and each entry (tile) has some properties.
i need i tiny bit of help!!!
I have a variable like this
enemy.health = 100
and another one for the player
i would like to know where to put this if statement
if enemy.health = 0 then
dofile("Story2.lua)
end
do i put it in the main loop or outside of it and also is that the correct if statement to use :?
requires tablesZitat:
Zitat von dan369
my code was messed up although right i just edited it to give it popper line formattingCode:player = { health = 100 }
enemy = { health = 100 }
while true do
-- game stuff for shooting or whatever
if enemy.health == 0 then
dofile("story2.lua")
end
end -- end of while loop
Dang it. I had a great post all typed up and I lost it. Oh well; I'll try again.
Where you put the if statement depends on the complexity of your script and what you need the if statement to do. If the script is not very complex or the if statement is a big part of the game or application you are making, you can put it in the main loop. However, if you might need to only do the if statement under certain conditions, or if you would need to be able to have more control over the if statement, I would put it in a function outside of the main loop. Then, if need be, you can put that function inside of the main loop; you'll have more control over the if statement and all of your code.
Secondly, the if statement script you provided is incorrect.
It should be:Code:if enemy.health = 0 then
dofile("Story2.lua)
end
Here's why:Code:if enemy.health == 0 then
dofile("Story2.lua)
end
In lua, "=" and "==" have completely different meanings and uses. "=" is used to set a variable equal to something, such as in "red = Color.new(255,0,0)", or in "gamestate = 1". The symbol "==", however, is used to compare a variable to something, such as in your code, "if enemy.health == 0 then", or "if gamestate == 1 then".
I remember this difference in the way I say (or rather think) the names of the symbols. For "=" I think "equals", and for "==" I think "is equal to". This helps me keep them separated in my mind.
As for comparison symbols, there are others besides "==". In lua there are "<, >, <=, >=, and ~=. In order, these are "less than", "greater than", "less than or equal to", "greater than or equal to", and "not equal to". You can use these to compare variables to other variables or to numbers. For instance, "if enemy.health <= 0 then" would allow you to do the action even if the enemy has less than 0 health, as if he was "overkilled". Another example, "if gamestate ~= 1 then", would execute the code only if the variable gamestate is not equal to 1. For a final example, you can use these when using timers, such as in "if timer < 0 then", where timer is the variable connected to the timer in the script. The code would then only execute if the the timer has run below 0.
I hope this has helped you and others! I know you aren't the only one who didn't understand this concept; I see it being used incorrectly fairly often.
thanks i get it know
No problem. :)
And there was no second quote in the dofile.
You wrote:
when it should be:Code:dofile("Story2.lua)
Code:dofile("Story2.lua")
Ah, nice catch. Didn't see that.
This may be a dumb question, but is there an easy way to restart my script? (Eg. Game Over! Press O to play a new game").
Or is the best thing to do just reset all the variables and things in a function.
littlevish - A typical 'restructure program flow or screw good practice' scenario.
Bad practice would be unloading all used resources then dofile("thisscript.lua"). One method of good practice would be creating a program that flows via 'game state's so restarting your script would be as easy as settingthe game state to the initial game state and initialize it. ('main menu' or 'intro' typically).
Zitat:
Zitat von littlevish
look at the lua snippets i posted a good game state system there, thanks to sg57 who gave me the idea by posting his and then i made my ownZitat:
Zitat von SG57
LINK
thanks
ok i have an error message which i have never seen before!!
THE ERROR:
581: attempt to perform arithmetic on global 'Moving' ( a boolean value) WAT does this mean and how would u fix it??
Show us the few lines before and after 581. Also, a boolen value is a value that holds either 'true' or 'false'.Zitat:
Zitat von dan369
i know wat i did wrong i but
where it should beCode:if Moving-Left == true then
at least i know wat boolean error means thanks!!!Code:
if Moving_Left == true then
-= Double Post =-
since that it has been working fine!! but there is one promblem when it loads up the menu the backgroung image doesn't change, it loads up the menu-image but it doesn't load up the level image is there a way to clear the screen when X is pressed on menu = 1 so that it would load up the other backgroung image
screen:clear()
use game states aswell and at the beginning of each state put
screen:clear()
if you want something to show in every state put it outside of the states
when you press X the game state changes to "Game"
Should it be like this
Code:if pad:cross() and gamestate = "Game" then
screen:clear()
Load_First_level()
end
i would use this methodZitat:
Zitat von dan369
here is what i posted in the snippets not so long ago
Code:white = Color.new(255, 255, 255)
black = Color.new(0, 0, 0)
red = Color.new(255, 0, 0)
green = Color.new(0, 255, 0)
blue = Color.new(0, 0, 255)
orange = Color.new(235, 115, 0)
yellow = Color.new(255, 255, 0)
state = { loading = 1, menu = 2, game = 3 }
current_state = state.loading
function Set_State(state_to_change_to)
current_state = state_to_change_to
end
function Get_State()
return current_state
end
function Start_States()
if Get_State() == state.loading then
-- do loading stuff
screen:clear()
screen:print(10, 10, "loading state", white)
if pad:cross() and oldpad:cross() ~= pad:cross() then
Set_State(state.menu)
end
elseif Get_State() == state.menu then
-- do menu stuff
screen:clear()
screen:print(10, 10, "menu state", white)
if pad:cross() and oldpad:cross() ~= pad:cross() then
Set_State(state.game)
end
elseif Get_State() == state.game then
-- do game stuff
screen:clear()
screen:print(10, 10, "game state", white)
if pad:cross() and oldpad:cross() ~= pad:cross() then
Set_State(state.loading)
end
end
end
oldpad = Controls.read()
while true do
pad = Controls.read()
screen:clear()
-- main
Start_States()
-- main
if Controls.read():start() then
break
end
screen.flip()
screen.waitVblankStart()
oldpad = pad
end
so say i have 3 scripts, index(starting one), menu( menu) game(game) how would u incorporate that with this???
it is better to have 3 scripts in most projects and structure them to fit your needs but if you want to keep your separate files then i would do this
in each of your files, make the information inside them a function
so go into game.lua and on the first line type function Game(), and on the last line another end - do this for menu.lua aswell, then have this file structure,
index.lua
main.luaCode:dofile("main.lua")
then use the Get_State() and Set_State(state) commands to swap through eachCode:white = Color.new(255, 255, 255)
black = Color.new(0, 0, 0)
red = Color.new(255, 0, 0)
green = Color.new(0, 255, 0)
blue = Color.new(0, 0, 255)
orange = Color.new(235, 115, 0)
yellow = Color.new(255, 255, 0)
dofile("game.lua")
dofile("menu.lua")
state = { menu = 1, game = 2 }
current_state = state.loading
function Set_State(state_to_change_to)
current_state = state_to_change_to
end
function Get_State()
return current_state
end
function Start_States()
if Get_State() == state.menu then
-- do menu stuff
Menu()
if pad:cross() and oldpad:cross() ~= pad:cross() then
Set_State(state.game)
end
elseif Get_State() == state.game then
-- do game stuff
Game
if pad:cross() and oldpad:cross() ~= pad:cross() then
Set_State(state.menu)
end
end
end
oldpad = Controls.read()
while true do
pad = Controls.read()
screen:clear()
-- main
Start_States()
-- main
if Controls.read():start() then
break
end
screen.flip()
screen.waitVblankStart()
oldpad = pad
end
i get this know in the script file i'll put the dofile command in the script
is there possibly an easy way the actual promblem is that when i press X on start(selected 1) it doesn't chande the background img to the actual one i want it keeps the menu image, is there a way when X is pressed on selected 1 that it will clear the screen black and the blit the other image??
-= Double Post =-
i have FIXEDDD IT Turns out i was that image was blitting underneath DUHHHH since i have fix that i have only one little tweak to do thanks
omg ive been looking all over the internet on how to load tiles from a tile sheet then input them into a table so then i can use them in my tile engine and blit them to the screen with the corrosponding number.
how the hell can i do it?
ive been looking at like zelda demo/LUMM all those things with tile engines in and i have no idea on how to mod it to my game.
can some one please give me a code snippet or a link to a tutorial or site on how to load 32x32 tiles from a tile sheet and input them into a table.
thxz for any replys
Luke
Can't find where I went wrong...
It returns nil when I do showEnemy()
Code:
--vars
enemies = 0
test = 1
enemyImg = Image.load("enemy.png")
screenColor = Color.new(255,255,255)
--tables
player = {x=200, y=100, width=25, height=25, speed=10, img = Image.load("player.png")}
block = {x=0, y=0, width=20, height=20, img = Image.load("block.png")}
enemy = { }
function newEnemy()
enemies = enemies + 1
new = enemies + 1
enemy[enemies] = {x=math.random(0,460), Y=math.random(0,252), width=25, height=25, speed=5, direction=math.random(1,4)}
showEnemies()
end
function showEnemies()
--directions: 1 up, 2 right, 3 down, 4 left
for i=1, enemies do
screen:blit(enemy[i].x, enemy[i].y, enemyImg)
if enemy[i].direction == 1 then
if enemy[i].y >= 0 then
enemy[i].direction = 3
else
enemy[i].y = enemy[i].y - enemy[i].speed
end
elseif enemy[i].direction == 2 then
if enemy[i].x >= 480 - enemy[i].width then
enemy[i].direction = 4
else
enemy[i].x = enemy[i].x + enemy[i].speed
end
elseif enemy[i].direction == 3 then
if enemy[i].y >= 272-enemy[i].width then
enemy[i].direction = 1
else
enemy[i].y = enemy[i].y + enemy[i].speed
end
elseif enemy[i].direction == 4 then
if enemy[i].x >= 0 then
enemy[i].direction = 1
else
enemy[i].y = enemy[i].y - enemy[i].speed
end
end
end
end
while true do
pad = Controls.read()
screen:clear(screenColor)
if pad:up() then
player.y = player.y - player.speed
elseif pad:down() then
player.y = player.y + player.speed
elseif pad:left() then
player.x = player.x - player.speed
elseif pad:right() then
player.x = player.x + player.speed
end
if pad:start() then break end
if enemies < 1 then
newEnemy()
else
showEnemies()
end
screen:blit(player.x,player.y,player.img)
screen:blit(block.x,block.y,block.img)
screen.flip()
screen.waitVblankStart()
end
littlevish - Lua player interprets scripts as it runs through it, not all at once then runs through it - your calling showEnemies in yoiur newEnemy function, and to newEnemy's scope, there's no such thing as showEnemies.
Basically - just move the showEnemies function above the newEnemy.
and please excuse what i said on that. those were my n00b days.Zitat:
Zitat von FaT3oYCG
not entirely true. the speedup is not nearly as much as i make it sound.Zitat:
* Found a major flaw in sprite sheet animation blitting. The problem was, the lib calculated the width to height ratio in the blit function, when it should have been done in the loading function. It works both ways, but doing it the the blitting function meant that the lib was dividing (with is a costly calculation) every frame unnecessarily. I fixed it so that it calculates only once, in the loading function. This should provide a MASSIVE speed-up when using sprite sheets.
ahhhh...Zitat:
Zitat von Grimfate126
http://lh5.google.com/LastNYCHero/Rr...ibhOQ/n00b.JPG
First milestone to success is admitting it...
lol. truth hurts :(Zitat:
Zitat von SG57
if only vista let me code again...
edit: nvm, it was a case-sensitive problem
did somebody help me correctly delete some files from flash1?
i cant assign flash 1 to delete custom ptf theme :( with my script...
i thought you could copy paste delete files from flash with lua player anyway ?, but if you can't use luaplayer hm or another version that has flash assign and un assign and then use the remove file option
System.removeFile(path)
can you write example, how can i correctly assign flash1?
???Code:System.assign(flash1:)
System.removeFile(flash1:/some.file)
System.unassign(flash1:)
you have to un assign first, there was some example scripts that came with sg57's mod, search for the download on dl.qj.net, or pm him
Last time I checked, the flash1 is in write mode to begin with.Zitat:
Zitat von lps
so how i can to delete the file?Zitat:
Zitat von Judas
System.removeFile("flash1 :/folder/file.ext")
hmmmmm...
you dont understand((
how can i correctly mount f1, delete file, unmount f1 ?
so basically you want me to do it for youZitat:
Zitat von lps
How ironic.Zitat:
Zitat von FaT3oYCG
Why do you need to un-mount the flash1? Delete the file, and be done with it, that's it. No more work required.Zitat:
Zitat von lps
if you do need to mount it i dont know the specific commands, as i have said there are examples in the mod that was madeZitat:
Zitat von Judas
if you do just mount it and delete then unmount
it is probably something like
System.assign("flash0:")
and
System.unassign("flash0:" )
and then do what i said