Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

TIC-80

Fantasy computer for making, playing and sharing tiny games. 路 By Nesbox

Writing a function that returns a table??

A topic by bearknuckles created Oct 10, 2017 Views: 1,580 Replies: 8
Viewing posts 1 to 9

I've been working on procedural map generation in TIC-80 (ambitious I know...) And I've hit a problem that's got me stumped.


I need to write a function that builds a table of attributes for an object, that I can call later...

Something like:


Function build_obj()

 Local z={

 attribute1=blah,

attribute2=blah,

attribute3=spam}

 return z

end


From there I can create objects like so:


obj1=build_obj()

obj2=build_obj()

etc...


The problem is when I try referring to my objects:


If obj1.attribute==blah then

 do awesome stuffs

end


I always get an error saying

"Attempting to index a nil value"


I understand what this means... Somewhere in the deep recesses of my code, my table of attributes is getting lost. And so obj1 is being assigned no value.


I've been googling this problem... It could happen if I typed


obj1=build_obj 


By leaving off the parentheses I'm assigning the entire function to the variable object. I've double checked and this isn't my problem.


Anyone know what I'm doing wrong?? 

I did not find a problem here )

-- script: lua
function build_obj()
  local z={attribute1="blah",
           attribute2="blah",
           attribute3="spam"}
 return z
end
obj1=build_obj()
if obj1.attribute1=="blah" then
  trace("do awesome stuff")
end
function TIC()
    exit()
end
(1 edit)

it worked for you? 

great, thanks. I'll just look for problems in my hardware. maybe if i reinstall TIC-80 it'll resolve the issue. Or there may be a typo somewhere upstream in my code. i'm just glad it's not some elusive programming syntax problem.

(1 edit)

okay...Here's a simplified version of my generation code to test the application. it has all the problems of the original code (I've tested it extensively) If you run it in TIC-80, you'll see that the problem is in the get_obj() function. The attributes end up nil.

The get_attrib() function seems to retrieve the data correctly, but it may have a problem returning the value.

Or maybe there's a problem calling a function with a return value from within another function.

 I can't seem to figure out why there's a problem assigning my attributes. anybody got any ideas?


--script: lua

attribs={"spam","toast","blah"}
objs={}

--get a random attribute
function get_attrib()
 trace("getting attribute...",14)
 local random=math.random(1,3)
local attrib=attribs[random]
trace("gotten attribute is...")
trace(attrib,9)
trace("")
return attrib
end

--get an object with three attributes
function get_obj()
 trace("building object...",8)
 local z={
attrib1=get_attrib(),
attrib2=get_attrib(),
attrib3=get_attrib()}

  trace("objects returned attributes...")
trace(attrib1,6)
trace(attrib2,6)
  trace(attrib3,6)
trace("")

  --log the object in the "objs" table
  trace("logging object",5)
  trace("")
table.insert(objs,#objs+1,z)  

end

function TIC()
 
--test get_attrib() return directly
trace("getting a test attribute",8)
temp=get_attrib()
trace("test attribute...")
trace(temp,2)
trace("")

--log an object 
 get_obj()
--iterate through objects and test for
--certain attributes
trace("started object testing",8)
for i,v in pairs(objs) do
trace("object attributes are")
trace(attrib1,6)
trace(attrib2,6)
trace(attrib3,6)
trace("")
end
trace("end of process",6)
 exit()
end

Sorry, but this is a very strange code)
That's how it works:

--script: lua
attribs={"spam","toast","blah"}
--get a random attribute
function get_attrib()
  trace("getting attribute...",14)
  local random=math.random(1,3)
  local attrib=attribs[random]
  trace("gotten attribute is...")
  trace(attrib,9)
  trace("")
  return attrib
end
--get an object with three attributes
function get_obj()
  trace("building object...",8)
  local z={
    get_attrib(),
    get_attrib(),
    get_attrib()
  }
  trace("objects returned attributes...")
  trace(z[1],6)
  trace(z[2],6)
  trace(z[3],6)
  trace("")
  return z
end
function TIC()
  --test get_attrib() return directly
  trace("getting a test attribute",8)
  temp=get_attrib()
  trace("test attribute...")
  trace(temp,2)
  trace("")
  --log an object 
  local objs = get_obj()
  --iterate through objects and test for
  --certain attributes
  trace("started object testing",8)
  for i,v in pairs(objs) do
    trace("object attributes are")
    trace(v,6)
    trace("")
  end
  trace("end of process",6)
  exit()
end
(1 edit)

Okay... That code works...

I see that you indexed the attributes by number. That must be my problem... I'll do some more testing.

Thanks a million!

Btw... What is strange about the code? The way it's written or the way it works? I'm just curious if I'm doing something wrong 馃榾

(1 edit)

Oh of course!! How dumb of me馃榿 I was trying to call the attributes of z without using z.attrib1!!

I rewrote my original script to include it and everything works perfectly!!

(1 edit)

Creating and returning a table with attributes is no problem.

function make()
    return { a=1, b=2 }
end 

Remember to assign it to a variable.

t = make()

Accessing those attributes is easy.

result = t.a + t.b

When you say t.a you're really saying the string key 'a', so it's equivalent to t['a'].

In Lua, you can also index by number (e.g. using tables like arrays, full or sparse) or by boolean (true or false). So t[1] or t[3.14159] or t[true] or t[false].

You can also index by table or by function, in which case Lua uses the identity of the table or function (so each is unique, even if they have the same contents). You can do t[make] or even t[t].

The only thing you cannot index by is nil. That is illegal.

Thanks mlepage... I had been using Lua for about a week when I wrote this... And have since learned a lot about the language.

But I always appreciate anyone taking time to teach me stuff. 馃憤