Page 13 of 91

Re: "Questions that don't deserve their own thread" thread

Posted: Wed Sep 03, 2014 6:42 pm
by slime
Whatthefuck wrote:Is there any way to load .lua files in threads? I tried doing require "filename", but it errors out in the thread.
You have to require love.filesystem (and any other love modules you might want to use) in the thread's code.
See the [wiki]love.thread[/wiki] wiki page.

Re: "Questions that don't deserve their own thread" thread

Posted: Wed Sep 03, 2014 9:00 pm
by bartbes
murks wrote:Well, ok, it's just that in the middleclass examples it is assigned to a local variable
MiddleClass-Commons is an extension to middleclass, not a replacement. So you first require middleclass however you would normally, and then you require middleclass-commons after, and the latter only needs to be done once.

Re: "Questions that don't deserve their own thread" thread

Posted: Wed Sep 03, 2014 9:14 pm
by murks
But how would I go about requiring middleclass-commons after I required middleclass to a local variable?
Require middleclass-comons to another local variable? To the same? I doubt that any of that would work.

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Sep 04, 2014 6:07 am
by Zilarrezko
Quick question, learning C++ and just learned about what references can do. Was wondering if Lua has something like that, because I was looking for something like it.

i.g. C++

Code: Select all

int add(int& num1, int num2){
    num1 += num2
    return num1
}

void main(){
   int x = 10

   add(x, 5)
   std::cout << x << std::endl;
}
Output: 15

or to put it more directly for what I want it...

Code: Select all

Banana = "fruit"
pantry = {}
pantry.orange = banana     --believe it or not, logic does apply here

function rotfood()
     pantry.orange = "rotten"
end
But I was the Banana to rot too without directly referencing the banana. I'd like it to be this way... Transfer variable1 to variable2. When variable2 changes, I want variable1 to change. any linking of variables?I'm just trying not to have to more lines of code to transfer the variable1 to variable2 and then from variable2 back to variable1. Possible in Lua?

EDIT:
I'm sorry, that C++ thing doesn't even do that. But I'm still learning. Is there still anyway to accomplish what I explained in the above paragraph?

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Sep 04, 2014 6:22 am
by bartbes
murks wrote:But how would I go about requiring middleclass-commons after I required middleclass to a local variable?
Require middleclass-comons to another local variable? To the same? I doubt that any of that would work.
As I told you, middleclass-commons doesn't return anything, so don't assign it to anything.

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Sep 04, 2014 7:08 am
by Plu
Zilarrekzo, tables handles in Lua are references, so the example you list will already replace the string "fruit" in the table with "rotten".

However, that only works on tables, not primitives, so the initial variable "Banana", which is a string, will not also be updated.

If you want to do this kind of thing, you need to use tables all the way for it:

Code: Select all

Banana = { "Fruit" }
pantry = { }
pantry.orange = Banana

function rotfood()
  pantry.orange[1] = "rotten" -- remember that pantry.ornage is now the table { "Fruit" }, not just a string.
end

-- Banana is now { "rotten" }

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Sep 04, 2014 7:36 am
by Robin
murks wrote:But how would I go about requiring middleclass-commons after I required middleclass to a local variable?
Require middleclass-comons to another local variable? To the same? I doubt that any of that would work.

Code: Select all

local class = require 'middleclass'
require 'middleclass-commons'

-- just use class
The thing is, you don't need to use MiddleClass-Commons, you just need to make it available for other libraries that you're using. It creates a specific global variable that lets other libraries know it's there, but you don't need to do anything else with it.

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Sep 04, 2014 7:48 am
by Zilarrezko
Thanks plu...
hmm, So how does this work in something like this?

Code: Select all

something.color = {1, 2, 3, 4} --numbers don't matter

function initObject(target)
     return {
          target = target
     }
end

anObject = initObject(something.color[1])

function object:update(dt) --I know the code doesn't work, I'm just trying to throw a visualization
     self.target = dt              
end
I'd like it to be that... but it's ending up being...

Code: Select all

something.color = {1, 2, 3, 4} --numbers don't matter

function initObject(objtitle, target)
     return {
          objtitle = objtitle
          target = target
     }
end

anObject = initObject("insertawesomename", something.color[1])

function object:update(dt) --I know the code doesn't work, I'm just trying to throw a visualization
     self.target = dt
     self:changevar(self.target)              
end

function parentObject:changevar(objtitle, var) --Pretend there's a parent object somewhere
     if objtitle == "insertawesomename" then
          something.color[1] = var
     elseif objtitle == "yay" then
          something.Abrahamlincolnwithacoln = var
    .........
     end
end
Not a huge deal, but requires some more hand work. But do I have to make every value in something.color a table? I guess I could just run a for loop at the beginning of draw... But it's going to be a loooong draw function (almost 200 lines and I'm not even 1/4 the way done yet.). [I bet everyone that tried to help me on that OOP post I made is pretty happy with me and my OOP skills now if they saw my code.]

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Sep 04, 2014 8:25 am
by murks
Robin wrote:
murks wrote:But how would I go about requiring middleclass-commons after I required middleclass to a local variable?
Require middleclass-comons to another local variable? To the same? I doubt that any of that would work.

Code: Select all

local class = require 'middleclass'
require 'middleclass-commons'

-- just use class
The thing is, you don't need to use MiddleClass-Commons, you just need to make it available for other libraries that you're using. It creates a specific global variable that lets other libraries know it's there, but you don't need to do anything else with it.
Thanks Robin, that makes it a bit clearer. I wonder why middleclass-commons isn't part of middleclass in the first place as it is only a couple of lines.

And by the way, give my regards to Batman :P

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Sep 04, 2014 2:22 pm
by Plu
I'm really not sure what you're trying to accomplish, Zilarrezko.

Can you make a new thread and try to explain what kind of functionality you're trying to create? The examples don't really make clear what the outcome of this whole puzzle is supposed to be, so I'm not really sure how to help.