RTS enemies x, y position overlap from table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
A_Fast_Cat
Prole
Posts: 8
Joined: Sat Jun 22, 2024 12:05 pm

RTS enemies x, y position overlap from table

Post by A_Fast_Cat »

Hi, I'm continuing work on this RTS project but have run into another problem....

Currently I am trying to make a working enemies module that has option for both ranged and melee minions. The logic to make them move and attack has been done. The program on the press of the spacebar will spawn a minion that will go and attack the user

Problem is: seems that all the minions have the same x, y if you spam space. I dont know how to stop them all occupy the same space.
I also cant seem to make them spawn from the spawn point consistently. After the first minion is spawned they seem to spawn on that initial minions current location rather than at the spawn point.

what should be happening: after spawning at the spawn point minions move to engage the user. No enemies are overlapping and all occupy their own space around the user.

I hope this makes sense...any help is greatly appreciated! please let me know if you need any further info or if any of the code does not make sense :))

PS feel free to use this code if you stumble across it. hope it helps ^^
Attachments
attempt1MinionOneVsOne.zip
(16.11 KiB) Downloaded 326 times
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: RTS enemies x, y position overlap from table

Post by knorke »

Code: Select all

function love.keypressed (key, scancode) 
  if key == "space" then
    table.insert(listOfMinions, m1) 
  end
end
On every keypress you are adding the same "m1" minion to the list.
The m1 minion is defined in love.load() and you are not adding its values but the m1 reference itself.

I did this and it seems to work:

Code: Select all

function love.keypressed (key, scancode) 
  if key == "space" then
  local newMinion = Minion(window_width * 0.9, window_height/2, 12, 100, 10, 120)
    table.insert(listOfMinions, newMinion) 
  end
end
A_Fast_Cat
Prole
Posts: 8
Joined: Sat Jun 22, 2024 12:05 pm

Re: RTS enemies x, y position overlap from table

Post by A_Fast_Cat »

ahhh its always the simplist things haha. Thank you so much!

I have some ideas to stop the overlap issue but can anyone explaing what #<a table> does over using ipairs<a table>

im not entirely sure but i think i need to do somehow iterate through the table using i,v in ipairs and get v+ 1 too, aka minion and the next minion along. Then itll be easy to work out if the x,y of each minion are overlapping....
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: RTS enemies x, y position overlap from table

Post by knorke »

You need some "collision detection" and "collision reaction."
The units are circles, so as first step you need to figure out which circles overlap.

#table returns the number of elements in an array.
for-loops with #table or ipairs both work, but only if the array uses integer number as index and has no "holes."
[1][2][3][4] will work.
[1][2][7][10] will not loop as expected.
This will become relevant, if you delete destroyed units.
Either use pairs (not: ipairs) or if you deleted an element then move all other elements accordingly to fill the hole.

Hope that is useful for googleing, those are both quite common problems.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 1 guest