Some assistance with Dynamic spawning of entities?

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
dementia_patient
Prole
Posts: 7
Joined: Sun Jan 06, 2013 2:50 am

Some assistance with Dynamic spawning of entities?

Post by dementia_patient »

I've been trying to make a 1944 clone as a sort of "test" game to test how much I've learned and what I still need to learn. Well I've encountered something I can't seem to figure out: Random spawning. I've attached my .love for the "game" and as you can see, it isn't much at all yet. I'm fairly happy I've learned enough about what I'm doing to make even that. Anyways, if someone could explain how random spawning would work with the code I have set up. Also, any tips of how I could make what I already have more efficient I'd appreciate. I know there are faster ways of implementing some stuff and I figure the sooner I get into the habit of doing them, the better.

But mainly, I just want to know how random spawning works because I can't seem to figure it out.

Also, what are some of the best games to clone for super-beginners? I've got a few on the list, but I wanted to start with 1944 just because it's a bit more complicated than something like pong but I don't think it's toooo hard.

Thanks for you time, guys.
Attachments
1944.love
(10.19 KiB) Downloaded 146 times
benhumphreys
Prole
Posts: 31
Joined: Thu Sep 13, 2012 2:10 am

Re: Some assistance with Dynamic spawning of entities?

Post by benhumphreys »

I don't know what 1944 is, but it seems you have a scrolling shooter.

I looked briefly at your code and it seems you have library-like class called Entities that handles live entities.
There's even a comment on lines 9 and 10 in main.lua that says:

Code: Select all

--format for creating entities is as follows:
--nameofent = ents.Create("entname", xpos, ypos)
Although you don't seem to use that anywhere...

Do you understand your code? It seems you just copypasted it from youtube as the comment at the top of entities.lua suggests.


Anyway to create entities dynamically and at random, you want to call entities.Create at random during your main.update() function.

There are a million ways to do this but one is to spawn an enemy every random() seconds.

Pick a number between 3 and 5 seconds. Spawn an entity. One simple way to do this is to have a running timer:

Code: Select all

function love.update(dt)
  -- ... existing stuff
  timeSinceLastEntity = timeSinceLastEntity + dt
  if timeSinceLastEntity > randomWait then
    -- Spawn a bad guy at a random point somewhere on the screen.
    ents:Create('badguy', math.random(love.graphics.getWidth()), math.random(love.graphics.getHeight())

    randomWait = math.random(3000, 5000) -- dt is in milliseconds, so our limit needs to be between 3000 and 5000, also math.random() produces integers only so math.random(3,5) * 1000 would only give you three different values
    timeSinceLastEntity = 0
  end
Hope this helps.
dementia_patient
Prole
Posts: 7
Joined: Sun Jan 06, 2013 2:50 am

Re: Some assistance with Dynamic spawning of entities?

Post by dementia_patient »

Lol I do understand my code. Actually I got fed up with that guy's stuff and deleted all of it and started from scratch. Anyways I guess my question was answered with the "math.random()" statement. I guess for some reason I overlooked that when I was reading through the libraries. Thank you!!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests