Page 1 of 2

text adventure game question

Posted: Sat Sep 28, 2013 7:47 pm
by Fatmat927
okay i just started my text adventure game and now i know i will be using gamestates for sure but i was wondering 2 things: first should i use tables or is it unnecessary(not so sure how to write this word) and should i do it all on my main.lua or do different .lua files for my three main paths u take at start? or even split it more than 3-4 files?

Re: text adventure game question

Posted: Sat Sep 28, 2013 7:56 pm
by ejmr
Are you asking if you should or should not use separate Lua files to store the content for each path?

Personally I would say use separate files. But using tables and separate files are not mutually exclusive choices. You could still represent the chunks of text in each file as part of a table if you feel like that is the best way to represent the data.

Re: text adventure game question

Posted: Sat Sep 28, 2013 8:36 pm
by Fatmat927
another question just to add can i make multiple love.draw in the same .lua file?

Re: text adventure game question

Posted: Sat Sep 28, 2013 8:46 pm
by Plu
You can only have one active love.draw function, but you can juggle them around if you use gamestates. Basically ->

Code: Select all


function drawfunc1()
  -- code here
end

function drawfunc2()
  -- code here
end

love.draw = drawfunc1

-- in your love.update:
if gamestate == 'someState' then
  love.draw = drawfunc2
end

If that makes sense to you

Re: text adventure game question

Posted: Sat Sep 28, 2013 9:11 pm
by Fatmat927
thx ill try it

Re: text adventure game question

Posted: Sat Sep 28, 2013 9:24 pm
by Fatmat927
dosent show anything of what i made heres my code if i made an error:

Code: Select all

	function love.load()
	WindowWidth = love.graphics.getWidth()
	WindowHeight = love.graphics.getHeight()
	gamestate = "intro"
if gamestate == "intro" then
	love.graphics.setBackgroundColor(0, 50, 0)
	darktree = love.graphics.newImage("dark tree.png")
	stars = love.graphics.newImage("stars.png")
	mage = love.graphics.newImage("mage.png")
	warrior = love.graphics.newImage("warrior.png")
	love.draw = drawfunc1
	end

	function drawfunc1()
		love.graphics.setColor(0, 0, 70)
		love.graphics.rectangle("fill", 0, 0, WindowWidth, WindowHeight / 4)
		love.graphics.setColor(255, 255, 255)
		love.graphics.draw(stars , 0, 0, 0, 1, 0.75, 0, 0)
		love.graphics.draw(darktree, WindowWidth / 2 - 300, WindowHeight / 2 - 200, 0, 1, 1, 0, 0)
		love.graphics.draw(darktree, WindowWidth / 2 + 150, WindowHeight / 2 - 300, 0, 1, 1, 0, 0)
		love.graphics.draw(darktree, WindowWidth / 2 + 100, WindowHeight / 2 - 100, 0, 1, 1, 0, 0)
		love.graphics.setNewFont(12)
		love.graphics.print("Welcome to the world of Dahamar, you now have to choose your path. Move with the arrows keys.", 100, 200, 0, 1, 1, 0, 0)
		love.graphics.setColor(255, 10, 10)
		love.graphics.setNewFont(30)
		love.graphics.print("WARRIOR", 350, 30, 0, 1, 1, 0, 0)
		love.graphics.setColor(10, 10, 255)
		love.graphics.print("MAGE", 30 , 300, 0, 1, 1, 0, 0)
		love.graphics.setColor(0, 0, 0)
		love.graphics.print("THIEF/ASSASSIN", 540, 300, 0, 1, 1, 0, 0)
	end
end
btw i just wanted to see if it would work to replace so i didnt do a second draw function

Re: text adventure game question

Posted: Sat Sep 28, 2013 10:10 pm
by ejmr
You are going to want to declare the 'gamestate' variable outside of the love.load() function, otherwise other functions will not be able to see its value or change it. For example, you would not be able to change 'gamestate' (and change drawing functions based on that) from within love.update().

Re: text adventure game question

Posted: Sat Sep 28, 2013 11:06 pm
by Fatmat927
thx works for now

Re: text adventure game question

Posted: Sun Sep 29, 2013 1:17 am
by Fatmat927
can anybody tell me what's wrong with this im trying to do so that when you press to "up" key it go to next gamestate as in the title it's for my text adventure game i think the error is in the bottom where I have my keypressed function because the rest was working and still works or maybe it's where i putted the gamestate but i don't know where else I should've placed it im still pretty new so plz help mee

here's the code:

Code: Select all

gamestate = "intro"
if gamestate == "intro" then
	function love.load()
		WindowWidth = love.graphics.getWidth()
		WindowHeight = love.graphics.getHeight()
		love.graphics.setBackgroundColor(0, 50, 0)
		darktree = love.graphics.newImage("dark tree.png")
		stars = love.graphics.newImage("stars.png")
		mage = love.graphics.newImage("mage.png")
		warrior = love.graphics.newImage("warrior.png")
	end


	function love.update(dt)
		love.draw = drawintro
	end
	
	function drawintro()
		love.graphics.setColor(0, 0, 70)
		love.graphics.rectangle("fill", 0, 0, WindowWidth, WindowHeight / 4)
		love.graphics.setColor(255, 255, 255)
		love.graphics.draw(stars , 0, 0, 0, 1, 0.75, 0, 0)
		love.graphics.draw(darktree, WindowWidth / 2 - 300, WindowHeight / 2 - 200, 0, 1, 1, 0, 0)
		love.graphics.draw(darktree, WindowWidth / 2 + 150, WindowHeight / 2 - 300, 0, 1, 1, 0, 0)
		love.graphics.draw(darktree, WindowWidth / 2 + 100, WindowHeight / 2 - 100, 0, 1, 1, 0, 0)
		love.graphics.setNewFont(12)
		love.graphics.print("Welcome to the world of Dahamar, you now have to choose your path. Move with the arrows keys.", 100, 200, 0, 1, 1, 0, 0)
		love.graphics.setColor(255, 10, 10)
		love.graphics.setNewFont(30)
		love.graphics.print("WARRIOR", 350, 30, 0, 1, 1, 0, 0)
		love.graphics.setColor(10, 10, 255)
		love.graphics.print("MAGE", 30 , 300, 0, 1, 1, 0, 0)
		love.graphics.setColor(0, 0, 0)
		love.graphics.print("THIEF/ASSASSIN", 540, 300, 0, 1, 1, 0, 0)
	end
end
	
	function love.keypressed(key)
	--commencer le jeu en tant que le warrior
		if key == "up" then
		gamestate = "warrior1"
		end
	end
if gamestate == "warrior1" then
		love.draw = drawwarrior1

	function drawwarrior1()
	love.graphics.draw(warrior, WindowWidth / 2 - 75, WindowHeight / 2 - 110, 0, 1, 1, 0, 0)
	end
end

Re: text adventure game question

Posted: Sun Sep 29, 2013 1:28 am
by ejmr

Code: Select all

-- Using the keyword 'local' here is not required, but personally I
-- believe it is a good habit.  It restricts the variable 'gamestate'
-- to just this file.  It helps hide data in that way so that you avoid
-- conflicts with other files (e.g. accidentally re-using a name).
local gamestate = "intro"

-- You had originally wrapped these function definitions in an if-statement.
-- That is unnecessary.
function love.load()
    WindowWidth = love.graphics.getWidth()
    WindowHeight = love.graphics.getHeight()
    love.graphics.setBackgroundColor(0, 50, 0)
    darktree = love.graphics.newImage("dark tree.png")
    stars = love.graphics.newImage("stars.png")
    mage = love.graphics.newImage("mage.png")
    warrior = love.graphics.newImage("warrior.png")
end

-- You should define drawinto() before you assign it anything.
function drawintro()
    love.graphics.setColor(0, 0, 70)
    love.graphics.rectangle("fill", 0, 0, WindowWidth, WindowHeight / 4)
    love.graphics.setColor(255, 255, 255)
    love.graphics.draw(stars , 0, 0, 0, 1, 0.75, 0, 0)
    love.graphics.draw(darktree, WindowWidth / 2 - 300, WindowHeight / 2 - 200, 0, 1, 1, 0, 0)
    love.graphics.draw(darktree, WindowWidth / 2 + 150, WindowHeight / 2 - 300, 0, 1, 1, 0, 0)
    love.graphics.draw(darktree, WindowWidth / 2 + 100, WindowHeight / 2 - 100, 0, 1, 1, 0, 0)
    love.graphics.setNewFont(12)
    love.graphics.print("Welcome to the world of Dahamar, you now have to choose your path. Move with the arrows keys.", 100, 200, 0, 1, 1, 0, 0)
    love.graphics.setColor(255, 10, 10)
    love.graphics.setNewFont(30)
    love.graphics.print("WARRIOR", 350, 30, 0, 1, 1, 0, 0)
    love.graphics.setColor(10, 10, 255)
    love.graphics.print("MAGE", 30 , 300, 0, 1, 1, 0, 0)
    love.graphics.setColor(0, 0, 0)
    love.graphics.print("THIEF/ASSASSIN", 540, 300, 0, 1, 1, 0, 0)
end

-- Defining this earlier for the same reason as drawintro().
function drawwarrior1()
    love.graphics.draw(warrior, WindowWidth / 2 - 75, WindowHeight / 2 - 110, 0, 1, 1, 0, 0)
end

-- This is the most important part: LÖVE calls love.update() over and
-- over and over, for every 'tick' in the game.  That means that
-- love.draw() will always use the drawinto() function (for all
-- intents and purposes) since love.update() does nothing but assign
-- drawinto() to the love.draw() function.  Because the engine calls
-- love.update() so often that means you realistically do not have the
-- time to make the engine use any other drawing function.
--
-- So for that reason you want to change love.draw() here based on the
-- value of the 'gamestate' variable.
function love.update(dt)
    if gamestate == "intro" then
        love.draw = drawintro
    elseif gamestate == "warrior1" then
        love.draw = drawwarrior1
    end
end

function love.keypressed(key)
    if key == "up" then
        gamestate = "warrior1"
    end
end