Page 1 of 1

logic to an rpg character speaking speech?

Posted: Sat Oct 14, 2017 6:31 am
by xThomas
so I'm trying to make an rpg character speech function. I'm wondering how many ways there are to do this, and what you would do

I had a working function but I deleted it because it sucked so much. I'm trying to make another one but for now what would you suggest?

Bonus: Another thing I might try is making characters shake like in Undertale. That's a really nice effect. But then every character needs.. to be... its own... table.. whoa. Well, I'm not sure how that would affect performance, if every text character is its own table. Hmm.. Well, I don't think it would be taxing at all unless I had thousands of characters. But I still don't know a good way to do that, without the initial function.

Targets: It would be drawing in little windows like the usual rpg game. battle window, shop window, or world window. (I don't actually have a window manager class yet, still learning how to do it)

Re: logic to an rpg character speaking speech?

Posted: Sat Oct 14, 2017 10:20 am
by Azhukar
Here's a shaky text function:

Code: Select all

local function shakyText(updatesPerSecond,maxDistance,repeats,opacity,text,x,y,...)
	love.graphics.print(text,x,y,...)
	local r,g,b,a = love.graphics.getColor()
	love.graphics.setColor(r,g,b,a*opacity)
	love.math.setRandomSeed(math.floor(love.timer.getTime()*updatesPerSecond))
	for i=1,repeats do
		local ox,oy = (love.math.random()-0.5)*maxDistance,(love.math.random()-0.5)*maxDistance
		love.graphics.print(text,x+ox,y+oy,...)
	end
	love.graphics.setColor(r,g,b,a)
end

function love.draw()
	love.graphics.setColor(255,255,255,255)
	shakyText(15,10,2,0.5,"testing shaky text",100,100)
end
First 4 arguments are for the shakiness, rest is equal to https://love2d.org/wiki/love.graphics.print

Re: logic to an rpg character speaking speech?

Posted: Sat Oct 14, 2017 11:15 pm
by xThomas
Ahh, that's pretty cool :) I actually explained my issue pretty badly though. Here's what I was trying to do was making RPG text that draws a string, one character at a time. Like a typewriter. I tried making more code but it caused a stack overflow. right now Im trying and failing to use coroutines (i have no idea how to use them... i get the concept, but actually initializing and yielding is confusing)

Re: logic to an rpg character speaking speech?

Posted: Sat Oct 14, 2017 11:47 pm
by Azhukar
Here's a string dripping function:

Code: Select all

local function dripText(text,charactersPerSecond,startTime)
	local currentTime = love.timer.getTime()
	if (currentTime <= startTime) then return "" end
	return text:sub(1,math.min(math.floor((currentTime-startTime)*charactersPerSecond),text:len()))
end

local myTextStartTime

function love.draw()
	love.graphics.setColor(255,255,255,255)
	myTextStartTime = myTextStartTime or love.timer.getTime()
	local text = dripText("testing shaky text",10,myTextStartTime)
	love.graphics.print(text,100,100)
end
You'll need to store the time from which you want the text to start being dripped.

Re: logic to an rpg character speaking speech?

Posted: Sun Oct 15, 2017 1:35 am
by xThomas
Thanks :)

Re: logic to an rpg character speaking speech?

Posted: Sun Oct 15, 2017 4:22 am
by xThomas
Here's one I made using coroutines. This is actually the first thing I've ever used them for :D

Code: Select all

function love.load()
    co = coroutine.create(f)
end

function f(str, rate)
    local StartTime = love.timer.getTime()
    local i,nexti = 0;
    local rate = rate or 10
    while i < #str do
        nexti = math.floor((love.timer.getTime() - StartTime)*rate)
        if nexti>i then i = nexti; print(str:sub(0,i)) end
        coroutine.yield()
    end
end
-- This function gets called once every frame
function love.update()
    coroutine.resume(co, 'What are you doing?')
end