Creating multiple selectable characters

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.
Head Over Heels
Prole
Posts: 9
Joined: Wed Feb 09, 2011 4:40 pm

Creating multiple selectable characters

Post by Head Over Heels »

Ok, this is the last of these topics as I really don't want to spam, but it's a key ingredient into what I'm investigating and the concept I'm coming up with.

I'm trying to work out how to approach the idea of having multiple selectable characters that I can simply toggle through to be playable.

So, say you have 4 characters on the screen at once in the same room, but can only control one at any one time, however you can switch between them with the press of a button or maybe using the 1-4 keys. How might I go about this in the script?

Cheers.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Creating multiple selectable characters

Post by bartbes »

If you have a table called playables you could do somthing like:

Code: Select all

player = next(playables, player) or next(playables)
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Creating multiple selectable characters

Post by Taehl »

I, personally, would have the four characters separately defined in a table (containing all game characters, or something). I would have a variable which references one of those tables. And I would make the controls handler act on that variable (NOT directly referencing the characters). To switch characters, point the variable at a different character. In other words:

Code: Select all

characters = {
	pc1={x=10,y=10, speed=50},
	pc2={x=20,y=20, speed=50},
	pc3={x=30,y=30, speed=20},
	pc4={x=40,y=40, speed=100},
}
player = characters.pc1

function love.keypressed(k)
	if k == 1 then player = characters.pc1
	elseif k == 2 then player = characters.pc2
	elseif k == 3 then player = characters.pc3
	elseif k == 4 then player = characters.pc4
	end
end

function love.update(dt)
	if love.keyboard.isDown("w") then player.y = player.y - player.speed * dt
	-- et cetera
	end
end
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Creating multiple selectable characters

Post by bartbes »

My switching code is shorter! :P
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Creating multiple selectable characters

Post by Taehl »

Yeah, but it's a little less easy to understand. I was just trying make my example as readable as possible.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: need to flag this might come in handy

Post by Jasoco »

pactace wrote:.
Please don't do that. There is both a Subscribe and Bookmark link at the bottom of every page.
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Creating multiple selectable characters

Post by pactace »

oh thanks didnt know that
Very new programmer dont judge
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Creating multiple selectable characters

Post by pactace »

Here a hypothisis but maybe this would work

Code: Select all

 characters = {
 player1
 player2
 player3
 player4
  }
           player1 = 	{
				x = 256,
				y = 256,
				x_vel = 0,
				y_vel = 0,
				jump_vel = -4000,
				speed = 400,
				flySpeed = 859,
				state = "",
				h = 55,
				w = 45,
				standing = false,
				}
player2 = 	{
				x = 256,
				y = 256,
				x_vel = 0,
				y_vel = 0,
				jump_vel = -4000,
				speed = 400,
				flySpeed = 859,
				state = "",
				h = 55,
				w = 45,
				standing = false,
				}
 player3 = 	{
				x = 256,
				y = 256,
				x_vel = 0,
				y_vel = 0,
				jump_vel = -4000,
				speed = 400,
				flySpeed = 859,
				state = "",
				h = 55,
				w = 45,
				standing = false,
				}
player4 = 	{
				x = 256,
				y = 256,
				x_vel = 0,
				y_vel = 0,
				jump_vel = -4000,
				speed = 400,
				flySpeed = 859,
				state = "",
				h = 55,
				w = 45,
				standing = false,
				}
 characters = Player
end
function love.keypressed(k)
   if k == 1 then player = player1
   elseif k == 2 then player = player2
   elseif k == 3 then player = player3
   elseif k == 4 then player = player4
   end
end
          


thats just my guess
Very new programmer dont judge
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Creating multiple selectable characters

Post by Robin »

pactace wrote:Here a hypothisis but maybe this would work
This doesn't just not work, it makes no sense.
What are you trying to do here?
Help us help you: attach a .love.
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Creating multiple selectable characters

Post by pactace »

I hadn't tested it before it made sense to me but this might make sense no promises

Code: Select all

player =    {
            x = 256,
            y = 256,
            x_vel = 0,
            y_vel = 0,
            jump_vel = -4000,
            speed = 400,
            flySpeed = 859,
            state = "",
            h = 55,
            w = 45,
            standing = false,
            }
player2 =    {
            x = 256,
            y = 256,
            x_vel = 0,
            y_vel = 0,
            jump_vel = -4000,
            speed = 400,
            flySpeed = 859,
            state = "",
            h = 55,
            w = 45,
            standing = false,
            }
 player3 =    {
            x = 256,
            y = 256,
            x_vel = 0,
            y_vel = 0,
            jump_vel = -4000,
            speed = 400,
            flySpeed = 859,
            state = "",
            h = 55,
            w = 45,
            standing = false,
            }
player4 =    {
            x = 256,
            y = 256,
            x_vel = 0,
            y_vel = 0,
            jump_vel = -4000,
            speed = 400,
            flySpeed = 859,
            state = "",
            h = 55,
            w = 45,
            standing = false,
            }
player = player

function love.keypressed(k)
   if k == 1 then player = player1
   elseif k == 2 then player = player
   elseif k == 3 then player = player3
   elseif k == 4 then player = player4
   end
end
Very new programmer dont judge
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 166 guests