Running other files like servers in threads

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
User avatar
JustaDebianDistro
Prole
Posts: 1
Joined: Tue Dec 19, 2017 8:28 pm
Contact:

Running other files like servers in threads

Post by JustaDebianDistro »

So I guess I'm a bit lost here my friend and I started working on game and our progress has been halted due to the lack of understanding of how threads work aperently. So if someone could please take the time and explain what I did wrong that would be rad.

Goal: We would like to have the server run when you hit the host server button and that would also double as single player where you could disable/enable player joining like minecraft kinda.

Problem: The problem that we are having is that the thread that the server should run on won't stay running and just stops.

Curent Progress: This is how I thought threads worked but this does not work how I thought it would, it runs the server file once and if you put the thread:start in love.update well then it just keeps restarting the server like expcted.

Code: Select all

--main.lua 
thread2 = love.thread.newThread("server.lua")
thread2:start()
I also tried making a main.lua that started 2 threads but that did not work.

Code: Select all

--main.lua 
thread1 = love.thread.newThread("client.lua")
thread2 = love.thread.newThread("server.lua")
thread2:start()
thread1:start()
This is also not a problem with the app as the server part does work fine if you start it as its own app. This is just me not understanding the thread part and yes I did look at older posts to try and find something helpful before posting but I didn't see any thing that was able to help with this problem.

Our project's github is
https://github.com/NetDelve/laughing-engine/
so you can look more at what we have if needed or if you just want to check it out.
Attachments
laughing-engine.love
(4.85 MiB) Downloaded 105 times
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Running other files like servers in threads

Post by zorg »

Hi and welcome to the forums!

The main thread that's started by default when you run a löve project goes through a few internal functions, and then arrives at a function called love.run; that includes some init code, a call to love.load, and then an infinite loop that polls events, calls love.update, love.draw, love.present and sleeps the thread (which matters more when vsync is turned off, but it happens in both cases)

In short, löve already gave you an infinite loop, the "game loop", as part of love.run in the main thread, but you're lacking such a thing when you create another thread without such code.

You need to define a loop (not necessarily infinite, could work in many ways, but at least a counting for loop shouldn't be used; repeat-until, while-do-end can work) and check for termination conditions and other things inside it; if you want it to work at a set speed, i'd suggest you put a love.timer.sleep in there as well, 0.001 is fine for the parameter, and you can just use love.timer.getTime in the thread (i think) and accumulate dt yourselves, and by that, everything's similar to what you can do in the main thread (with the exception that graphics functions can't be called from any thread other than the main one, and there are a few other functions that are not thread-safe or can't be called from other threads, not just in love.graphics or love.window)

You also need to require love.* if you want to use those modules inside a thread (with the exception of love.filesystem and love.thread, iirc); the above restrictions withstanding.

If by any chance you do have a loop, then you might have gotten an error, you need to catch those with love.threaderror, i think.
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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Brotein and 5 guests