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.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Creating multiple selectable characters

Post by zorg »

Code: Select all

characters = {}
characters[1] = {
-- ...
}
characters[2] = {
-- ...
}
-- etc.
would work then; probably what (both of) you wanted.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Creating multiple selectable characters

Post by pactace »

what the heck that makes no sense
Very new programmer dont judge
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Creating multiple selectable characters

Post by Jasoco »

It makes total sense. Each character would be part of a single characters table. I call them Entities in my games though. Sometimes Actors. Either way it's all just a table with data in it. (That's what all of Lua is. Tables full of stuff.)
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Creating multiple selectable characters

Post by pactace »

hm so basically make a table such as

Code: Select all

characters = { player 1 
                                                                                       player 2
                                                                                       player 3
                                                                                       player 4}
then do all the

Code: Select all

player[1] = 	{
				x = 256,
				y = 256,
				x_vel = 0,
				y_vel = 0,
				jump_vel = -4000,
				speed = 700,
				flySpeed = 800,
				state = "",
				h = 55,
				w = 45,
				standing = false,
			}
	player[2] =    {
            x = 256,
            y = 256,
            x_vel = 0,
            y_vel = 0,
            jump_vel = -4000,
            speed = 4000,
            flySpeed = 8059,
            state = "",
            h = 55,
            w = 45,
            standing = false,
            }
	player[3] =    {
            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[4] =    {
            x = 256,
            y = 256,
            x_vel = 0,
            y_vel = 0,
            jump_vel = -4000,
            speed = 400,
            flySpeed = 859,
            state = "",
            h = 55,
            w = 45,
            standing = false,
            }
then make sure the player = the characters like

Code: Select all

player = player 1 
then have the things that swich them out such as

Code: Select all

function love.keypressed(k)
   if k == 1 then player = player[1]
   elseif k == 2 then player = player[2]
   elseif k == 3 then player = player[3]
   elseif k == 4 then player = player[4]
   end
right?
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:then have the things that swich them out such as
The key is a string, not a number, and you confuse the player table and the characters table, so I'd do something like:

Code: Select all

function love.keypressed(k)
   if characters[tonumber(k)] then
      player = characters[tonumber(k)]
   end
end
,EDIT: Oops, missed a whole page.
Help us help you: attach a .love.
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: Creating multiple selectable characters

Post by Muris »

I honestly think the best way to approach this is to make a function, where you create a table with common values, then return the table and fill the rest of the values manually, basically kind of like OOP aproach. Something along the lines:

Code: Select all

local function createCharacter( o )
	o = o or {}
	o.x = o.x or 256
	o.y = o.y or 256
	o.x_vel = o.x_vel or 0
	o.y_vel = o.y_vel or 0
	o.jump_vel = o.jump_vel or -4000
	o.speed = o.speed or 400
	o.flySpeed = o.flySpeed or 859
	o.state = o.state or ""
	o.h = o.h or 55
	o.w = o.h or 45
	o.standing = o.standing or false
	return o
end


local players = {
	createCharacter( { x = 100, y = 200 } ),
	createCharacter( { x = 200, y = 210 } ),
	createCharacter( { x = 300, y = 220 } ),
	createCharacter()
}
local fontHeight 
local playerIndex = 1

function love.load()
	fontHeight = love.graphics.getFont():getHeight()
end

function love.draw()
	local ypos = 5
	
	love.graphics.print( "Current player: " .. playerIndex, 5,ypos )
	ypos = ypos + fontHeight
	for j,v2 in pairs( players[playerIndex] ) do
		love.graphics.print( j .. " = " .. tostring(v2), 10,ypos ) 
		ypos = ypos + fontHeight
	end	
end

function love.keypressed(key)
	if key == " " then
		playerIndex = playerIndex + 1
		if playerIndex > #players then playerIndex = 1 end				
	end
end
You can change turn by pressing space. It shows all the values the object has so you can see the values of each separate object by pressing the space. I suggest digging more about object oriented programming, if you want to know more about this, like adding functions to each object and every object having unique identity (I probably made that up for describing oop). There are also some good implementations for classes in lua in the libraries: https://love2d.org/wiki/Category:Libraries
User avatar
pactace
Prole
Posts: 38
Joined: Fri Jan 30, 2015 1:25 am

Re: Creating multiple selectable characters

Post by pactace »

what is tonumber?
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 »

[manual]tonumber[/manual] converts strings to numbers.
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Creating multiple selectable characters

Post by Jasoco »

It converts a value to a number. For instance if you have a string like so "300" it will change it to 300. It basically converts number to other numbers or strings to numbers. It is used when you want to make sure a value is a number before trying to perform a math calculation on it. If it is already a number it will remain a number. Basically.

There's also tostring which converts other values to strings. Like numbers will become strings (300 becomes "300") and booleans become string versions "true" and "false". And nil values become "nil". You'll want to use this on booleans when trying to print their values to the screen. (Like for debugging)
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: Creating multiple selectable characters

Post by Muris »

Seems like people just answered. Something to add strings arent same as numbers:

Code: Select all


if 1 == "1" then
	print( "One is one")
else
	print( "One isn't one!")
end
This prints: "One isn't one!", this is because they are different types. It is same as false is not nil or false is not 0 nor 0 is not nil. I think in javascript you can do comparison 1 == "1" which returns true, but if you use strict comparison 1 === "1" in js, it returns false.

Also something to add on jasocos printing. If you try to add string to boolean, you will get an error, so you have to explicitly say, that you need a string version of boolean variable aka tostring(booleanVariable). Same goes with nil
In other words this throws an error:

Code: Select all

print(false .. " yo")
Post Reply

Who is online

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