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

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.
Locked
User avatar
slime
Solid Snayke
Posts: 3148
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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.
User avatar
murks
Party member
Posts: 185
Joined: Tue Jun 03, 2014 4:18 pm

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

Post 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.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

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

Post 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?
Last edited by Zilarrezko on Thu Sep 04, 2014 6:40 am, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

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

Post 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" }
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

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

Post 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.]
User avatar
murks
Party member
Posts: 185
Joined: Tue Jun 03, 2014 4:18 pm

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

Post 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
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

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

Post 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.
Locked

Who is online

Users browsing this forum: No registered users and 3 guests