![]() |
| Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact |
| ||||||
This is a discussion on Some hints for AI Enemy within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; LuA Hi all I am working on my game for a while and I need some help with this: Everytime ...
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 |
![]() |
LuA
Hi all I am working on my game for a while and I need some help with this: Everytime the player presses circle a new enemy spawns soo enemyindex=0 enemy[enemyindex]={x=0,y=0,hp=100,image="e nemywalksright.png" if pad:circe() and enemyindex <= 10 then enemyindex=enemyidex+1 end Now I want to make a for loop i think where i can place all enemy actions and all enemies must act not at the same time, Because else the enemy images will overlay eachother if they are walking to the player so all enemies must have different time when they act.. What I am asking for is a nice for loop with the things that i mentioned.. Last edited by Predricted; 06-11-2009 at 07:26 PM.. |
|
|
|
|
|
#2 |
![]() ![]() Developer
|
your idea is correct on what to do, simple expand it to add a variable named action, than in the loop you can do:
Code:
for(int i=0;i<maxenemys;i++){
if(enemy[i].action == 0){
//don't do anything
}else if(enemy[i].action==1){
//attack
}else if(enemy[i].action==2){
//look for enemy
}
}
__________________
1. Failed....again... 2. http://slicer.gibbocool.com/ stay updated on all my projects |
|
|
|
|
|
#3 | |
![]() |
Quote:
Code:
if i>=maxenemys then i=0 end PS. your code is in C i was talking about lua but no big deal haha |
|
|
|
|
|
|
#4 |
![]() ![]() Developer
|
i understand, however it's been such a long time that i've used lua i don't fully remember how to do a for loop in it properly, however i kept my code as psuedo as possible, as to your question, no, here's what you do:
Code:
while true do for i = 0 i < maxenemys; i++ //like i said, don't remeber //code to handle enemys end //code to handle player like normal(doesn't neccissarly have to be below the loop, up to you end
__________________
1. Failed....again... 2. http://slicer.gibbocool.com/ stay updated on all my projects |
|
|
|
|
|
#5 |
![]() |
Okay I made this code but I get this error that i can't get rid off
Code:
enemy={}
enimg="images/enemy/imp_left.png"
enemy[1]={x=25,y=23,hp=100,image="images/enemy/imp_up.png",imageload=Image.load(enimg)}
enemies=1
while true do
screenwaitvblankstart
--movement enemy
if enemies > 1 then
for i=0, enemies do
i=i+1
if enemy[i].x > player1[1].x then
enemy[i].x=enemy[i].x-1
enemy[i].image="images/enemy/imp_left.png"
end
if enemy[i].x < player1[1].x then
enemy[i].x=enemy[i].x+1
enemy[i].image="images/enemy/imp_right.png"
end
if enemy[i].y > player1[1].y then
enemy[i].y=enemy[i].y-1
enemy[i].image="images/enemy/imp_up.png"
end
if enemy[i].y < player1[1].y then
enemy[i].y=enemy[i].y+1
enemy[i].image="images/enemy/imp_down.png"
end
enemy[i].imageload = Image.load(enemy[i].image)
screen.startDraw()
Image.blit(enemy[i].x,enemy[i].y,enemy[i].imageload)
screen.endDraw()
if i == enemies then
i=0
end
end
end
if pad:circle() and oldpad:circle() ~= pad:circle() then
enemies=enemies+1
enemy[enemies]={x=25,y=23,hp=100,image="images/enemy/imp_up.png",imageload=Image.load(enimg)}
end
script.lua:109: attempt to index value "?"(a nil value) it happens when i press circle -=Double Post Merge =- Code:
enemy={}
enimg="images/enemy/imp_left.png"
enemy[1]={x=25,y=23,hp=100,image=enemy_up.png",imageload=Image.load(enimg)}
enemies=1
while true do
screenwaitvblankstart
enemy movement begins here
--movement enemy
if enemies > 1 then
while true do
for i=0, enemies do
i=i+1
THE LINE BELOW THIS LINE IS THE LINE WHERE THE ERROR TAKES PLACE
if enemy[i].x > player1[1].x then
enemy[i].x=enemy[i].x-1
enemy[i].image="enemy_left.png"
end
if enemy[i].x < player1[1].x then
enemy[i].x=enemy[i].x+1
enemy[i].image="enemy_right.png"
end
if enemy[i].y > player1[1].y then
enemy[i].y=enemy[i].y-1
enemy[i].image="enemy_up.png"
end
if enemy[i].y < player1[1].y then
enemy[i].y=enemy[i].y+1
enemy[i].image="enemy_down.png"
end
enemy[i].imageload = Image.load(enemy[i].image)
screen.startDraw()
Image.blit(enemy[i].x,enemy[i].y,enemy[i].imageload)
screen.endDraw()
if i >= enemies then
i=0
end
end
end
end
now when you press circle enemies variable increases by 1
if pad:circle() and oldpad:circle() ~= pad:circle() then
enemies=enemies+1
enemy[enemies]={x=25,y=23,hp=100,image="enemy_up.png",imageload=Image.load(enimg)}
end
attempt to index field '?'(a nil value) Last edited by Predricted; 06-11-2009 at 08:12 PM.. Reason: Automerged Doublepost |
|
|
|
|
|
#6 |
![]() Enter Custom Title
|
for- loops in Lua start at 1 not 0, that's C. Also a better way to stop re-swaning enemies would be to use a bool.
Code:
maxNumOfEnemies = 5
maxEnemiesReached = false
--Note this doesn't need to be in a for-loop
if #enemies >= maxNumOfEnemies then
maxEnemiesReached = true;
else
maxEnemiesReached = false;
end
--then just add this
if pad:circle() and oldpad:circle() ~= pad:circle() and not maxEnemiesReached then
|
|
|
|
|
|
#7 | |
![]() |
Quote:
Okay now can you tell me how to use table.insert() I've tried this Code:
if pad:circle() and oldpad:circle() ~= pad:circle() then enemies=enemies+1 table.insert(enemy,x) table.insert(enemy,y) table.insert(enemy,hp) table.insert(enemy,image) table.insert(enemy,imageload) end that x,y,hp,image,imageload needs a variable because when a new enemy is created it will directly follow the enemy so it will use x and y in if statements but they don't have a variable.. |
|
|
|
|
|
|
#8 | |
![]() ![]() Developer
|
You need to insert a table into the enemy table.
__________________
![]() Check out my homebrew & C tutorials at http://insomniac.0x89.org/ Coder formerly known as Insomniac197 Quote:
|
|
|
|
|
|
|
#9 |
![]() Enter Custom Title
|
|
|
|
|
|
|
#10 | |
![]() |
Quote:
just use iteration over the table then you wont need all those hacks in your code. Code:
local key, enemy for key,enemy in pairs(enemies) do if enemy.y < player.y .... elseif else end end
__________________
-- Code Monkey : Sarien, Fishguts, Cracks and Crevices -- "Did IQ's just drop sharply while I was away?" (Ripley) |
|
|
|
|
|
|
#12 |
![]() Enter Custom Title
|
table.insert( table, {x = 0, y = 0, img = etc});
pos - postiton to insert into table - the table to insert too value - the values you want to be inserted |
|
|
|
|
|
#13 | |
![]() ![]() Developer
|
Quote:
Code:
for i,enemy in ipairs(enemies) do
if enemy.y < player.y
....
elseif
else
end
end
|
|
|
|
|
|
|
#14 |
![]() |
|
|
|
|
![]() |
| Tags |
| enemy , hints |
| Thread Tools | |
|
|