Page 1 of 2

Creating multiple selectable characters

Posted: Sat Feb 12, 2011 8:50 am
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.

Re: Creating multiple selectable characters

Posted: Sat Feb 12, 2011 9:29 am
by bartbes
If you have a table called playables you could do somthing like:

Code: Select all

player = next(playables, player) or next(playables)

Re: Creating multiple selectable characters

Posted: Sat Feb 12, 2011 10:14 am
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

Re: Creating multiple selectable characters

Posted: Sat Feb 12, 2011 10:15 am
by bartbes
My switching code is shorter! :P

Re: Creating multiple selectable characters

Posted: Sat Feb 12, 2011 6:07 pm
by Taehl
Yeah, but it's a little less easy to understand. I was just trying make my example as readable as possible.

Re: need to flag this might come in handy

Posted: Thu Feb 05, 2015 7:56 am
by Jasoco
pactace wrote:.
Please don't do that. There is both a Subscribe and Bookmark link at the bottom of every page.

Re: Creating multiple selectable characters

Posted: Sun Feb 08, 2015 10:38 pm
by pactace
oh thanks didnt know that

Re: Creating multiple selectable characters

Posted: Tue Feb 10, 2015 3:39 am
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

Re: Creating multiple selectable characters

Posted: Tue Feb 10, 2015 9:10 am
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?

Re: Creating multiple selectable characters

Posted: Tue Feb 10, 2015 11:14 pm
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