Draw and keypressed events inthe middle of a function

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.
Post Reply
Kemsha
Prole
Posts: 2
Joined: Tue Aug 01, 2017 4:02 pm

Draw and keypressed events inthe middle of a function

Post by Kemsha »

Hi guys,

I’ve begun to play around with LÖVE and decided to make a roguelike game but I’m having a problem and was wondering if you could help me.
I think I understand the concept of draw() and I’m using several global variables to decide what to effectively show (dungeon, character sheet, talent usage, etc).
My problem right now is with talents(or skills). I defined them like this:

Code: Select all

local talent = Talents:new( {
				name     = "Magic arrow",
				range    = 10,
				description = "Shoot a magic arrow",
				level = 1
				use = function(user)
					-- Select a target
					local target = getTarget(user, 10)		
					-- do something to target
					target:takeDamage(10)
					-- do something else
					user:spendMana(5)
					end				
				} )
So, the question is, how can I make this work? I want to allow the player to choose one target and then do something to that target and maybe some other stuff
If I use those global variables, I could probably do the targeting part, but how would I then continue to execute the rest of the code?
Is there a way to “interrupt” the function execution, do the drawing and the target selection, and then continue where I left off?

Thanks guys

PS: Another unrelated question: In that example above, is there a way to use in the function the other fields of the talent? For instance to have the damage be proportional to the level?
alloyed
Citizen
Posts: 80
Joined: Thu May 28, 2015 8:45 pm
Contact:

Re: Draw and keypressed events inthe middle of a function

Post by alloyed »

In a way this feels like giving a new driver the keys to an ultrafancy sports car, but the exact thing you are looking for is coroutines.
http://lua.space/gamedev/using-lua-coro ... create-rpg

and here is an online discussion asking whether or not this is a "good" idea, and the subtle high-level implications of doing things like this.
https://news.ycombinator.com/item?id=13366153

This might seem overwhelming, which is perfectly reasonable. If understanding how to use a language is a ladder, things like coroutines/other flow control is on the tippy-top rung. A much easier to understand alternative is a state machine, which you might right like so:

Code: Select all

state = {name = "find_target", user = user, distance = 10}
update = function(dt)
	if state.name == "find_target" then
		if state.user clicked on target then
			state = { name = "damage_target", user = state.user, target = target }
		end 
	elseif state.name == "damage_target" then
		-- play damage animation
		if animation is finished then
			state = { "spend_mana" } 
		end
		... etc ...
	end
end
This may seem tedious, and it kind of is, but it's a great learning exercise, and finding ways to make this less tedious will teach you even more.
Kemsha
Prole
Posts: 2
Joined: Tue Aug 01, 2017 4:02 pm

Re: Draw and keypressed events inthe middle of a function

Post by Kemsha »

Well, I haven't got much experience in LÖVE or LUA but I've programed for many years so I'll take good care of your car ;)

Anyway, I've seen your link and managed to make it work in my game!

Many thanks!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests