flowers and sheep: ecology game, newbie needs help

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.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Newbie game crashing need some help to get me going

Post by ejmr »

Code: Select all

 -- sun for flowers

	for i,v in ipairs(flowers) do
	flower.chlorophyll = flower.chlorophyll + dt*10
		if v.chlorophyll > 100		then
	
		v.clone = 1
	
		end
	
	end


It seems like you should be using the variable ‘v’ instead of ‘flower’, i.e.

Code: Select all

for i,v in ipairs(flowers) do
    v.chlorophyll = v.chlorophyll + dt*10
    if v.chlorophyll > 100 then
        v.clone = 1
    end
end
Later on when you create a flower and insert it into the ‘flowers’ table you do not give it a value for the ‘chlorophyll’ property, and so eventually that will crash the loop above since ‘chlorophyll’ will not exist.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Clödi
Prole
Posts: 7
Joined: Fri Sep 20, 2013 7:44 pm

Re: Newbie game crashing need some help to get me going

Post by Clödi »

Thank you a lot! Works great.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Newbie game crashing need some help to get me going

Post by ejmr »

Glad to hear it ^^

Good luck with your game. Just from looking at the code it sounds like an interesting idea.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Clödi
Prole
Posts: 7
Joined: Fri Sep 20, 2013 7:44 pm

Re: Newbie game crashing need some help to get me going

Post by Clödi »

Thank you! And yes, i think so too. It was interesting enough to get me to try coding again, after all.

Watching the flowers grow is already fun now, see attachment.
Attachments
Ecology.love
(6 KiB) Downloaded 68 times
Clödi
Prole
Posts: 7
Joined: Fri Sep 20, 2013 7:44 pm

Re: Newbie game crashing need some help to get me going

Post by Clödi »

Allright, i'm stuck again. The game still doesn't crash, though.

I'm trying to keep the flowers from planting onto each other, making a collision check. I don't want to check all the flowers in the table though, for economic reasons, just the ones that are close.

My idea was to make a table/array that has all the tiles of the map and stores what objects there are on each tile. Now i need to write a variable in the array in the right place when creating the objects in the load function, and later read that out when planting a flower in the game loop. How do i do that?

(This is probably terribly easy, but like the thread title suggests, i'm only fiddling around here.)

Edit: Right, forgot the code and the file, sorry.

Code: Select all

-- table for collision checking

occupied = {}
for i=0,map_h do
	rowx = {}
		for i=0,map_w do
		tiley = {}
		tiley.flower = 0
		table.insert(rowx,tiley)
		end
	table.insert(occupied,rowx)

end
   
-- blume

	flowergraphic = love.graphics.newImage("flower.png")
	
	flowers = {}
for i=0,4 do
	flower = {}
	flower.nutrition = 10
	flower.clone = 0
	flower.chlorophyll = 0
	flower.width = flowergraphic:getWidth()
	flower.height = flowergraphic:getHeight()
	flower.x = i * (flower.width)
	flower.y = flower.height
	flowerx = 0
	flowery = 0
	table.insert(flowers, flower)

end
EDIT: Nevermind, my way was wrong, i think i got this.
Attachments
ecology.love
(6.07 KiB) Downloaded 62 times
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Newbie: need some help to get me going

Post by ejmr »

I'm trying to keep the flowers from planting onto each other, making a collision check. I don't want to check all the flowers in the table though, for economic reasons, just the ones that are close.
If it is easier, in terms of the code you have to write, to check all of the flowers then you should do that. Even if that means the code is wasting time checking flowers that are not near others. If later on that ends up creating slow-down or lag then go back and modify the code, making it more complex so that it only checks necessary flowers.

To put it another way: write the simplest code even if it seems inefficient. You admit that you are an inexperienced programmer. Therefore, personally, I believe you should focus on writing the simplest code that works. Don’t worry right now if the code is wasting resources. Get the code working the way you want and then think about optimizing it later.

I also wanted to make a suggestion about the large ‘map’ table. To improve readability (in my opinion) you can put the table in other file and then load the table from there, like so:

Code: Select all

-- Let's say this is in a file called "map.lua"
return {
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }
   ...
}

-- Now this code below would be in your "main.lua" file, or wherever
-- you intend to use 'map'.

local map = require("map.lua")
Notice how the ‘map’ file consists of just a return statement followed by the huge table? The result is that when you require() the file it will return that table, which is why the example assigns the result of require() to a variable. It’s not something you need to do, just a personal suggestion that may help you organize things if you start feeling like there’s too much going on in one file too keep track of.

All that said, take a step back and instead try the simplest approach you can think of for the collision detection, even if it looks horribly inefficient. There is not much value in optimizing code that isn’t doing what you want in the first place.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Clödi
Prole
Posts: 7
Joined: Fri Sep 20, 2013 7:44 pm

Re: flowers and sheep: ecology game, newbie needs help

Post by Clödi »

Yes, you are right, thank you. I now tried the most simple way i could think of.

Try to plant a flower in a random position, if there is already one, try again, do this ten times, if that doesn't work just give up. Not very elegant, it will often result in no flower being planted, but it's a start.

In previous versions there also was the possibility of an infinite loop, if the random numbers always end up being 0. Not very likely, but this time i tried to rule out infinite loops by just trying ten times.

However, it doesn't work.

The mistake is most likely somewhere here.

Code: Select all

-- grow flower patches
	
	for ii,vv in ipairs(flowers) do
	
		if vv.clone == 1 then
		
			vv.clone = 0
			vv.chlorophyll = 0
			flowerx = vv.x
			flowery = vv.y
			success = 0
			newx = 0
			newy = 0
			
			for i=0,9 do
				
				if success == 0 then 
					a = 0
					b = 0
					for i,9 do
					
						if a + b == 0 then
							a = math.random(0,2)
							b = math.random(0,2)
						end
					end
					
					if a + b > 0 then 
				
						if a == 1 then
						directionx = -1
						elseif a == 0 then
							directionx = 0
						elseif a == 2 then
							directionx = 1
						end
		
						if b == 1 then
							directiony = -1
						elseif b == 0 then
							directiony = 0
						elseif b == 2 then
							directiony = 1
						end
			
						newx = flowerx + directionx*22
						newy = flowery + directiony*22

		
						for iii,vvv in ipairs(flowers) do
							if CheckCollision(newx,newy,22,22,vvv.x,vvv.y,vvv.width,vvv.height) then
								success = 0
							else
								success = 1
								local flower = {}
	
								flower.x = newx
								flower.y = newy
								flower.nutrition = 10
								flower.clone = 0
								flower.chlorophyll = 0
								flower.width = flowergraphic:getWidth()
								flower.height = flowergraphic:getHeight()
								table.insert(flowers, flower)
							end
						end
					end
				end
			end	
		end
		
	end
The map thing is on my list for later, definitely. But thank you for the tip, now i have a reference for when i try that.
Attachments
ecology.love
(6.29 KiB) Downloaded 63 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 8 guests