generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Vectors

Previous Thread :: Next Thread 
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 02:40 AM
Can someone explain how they work? I was planning to use a vector3 values as a table index

sides = {
[Vector3.new(1, 0, 0)] = {"east", "west"},
[Vector3.new(0, 0, 1)] = {"north", "south"}
}

but I was having trouble indexing values from the table

local inc = Vector3.new() --inc would have a negative or positive number on either the x or z axis
dirA = sides[inc/inc][1]
dirB = sides[inc/inc][2]

I thought of a clever method for getting around this, by instead returning values from a function into the table index

sides = {
[(function() return 0, 0, 1 end)()] = {"east", "west"},
[(function() return 1, 0, 0 end)()] = {"north", "south"}
}

and indexing it in a similar way

local inc = Vector3.new()
dirA = sides[(function(vec) return vec.x, vec.y, vec.z end)(inc/inc)][1]
dirB = sides[(function(vec) return vec.x, vec.y, vec.z end)(inc/inc)][2]

but this method feels a bit hacky... Anyways I'm wondering if there is a cleaner solution I can use with or without vectors and why it won't work with vectors. I'm guessing it has something to do with how vectors classes are created but I haven't looked much into that
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 03:14 AM
almost forgot

local toNum = function(n)
--Transforms number to 0 for cases like 0/0
return val == 1 and val or 0
end

local inc = Vector3.new()
dirA = sides[(function(vec) return toNum(vec.x), 0, toNum(vec.z) end)(inc/inc)][1]
dirB = sides[(function(vec) return toNum(vec.x), 0, toNum(vec.z) end)(inc/inc)][2]
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
11 Jun 2012 04:53 AM
[ Content Deleted ]
Report Abuse
NVI is not online. NVI
Joined: 11 Jan 2009
Total Posts: 4744
11 Jun 2012 04:54 AM
HEY OYSI CHECK YOUR MESSAGES
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
11 Jun 2012 04:59 AM
[ Content Deleted ]
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 09:52 AM
Unfortunately what I'm trying to do doesn't work like that
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 10:01 AM
" And your solution is also terrible, because it doesn't work"

It works fine though??
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 10:28 AM
if it helps im just trying to find two sides based off a given vector. Its not for a compass or anything like that but like i said before "I'm wondering if there is a cleaner solution I can use with or without vectors and why it won't work with vectors. "
i'm guessing that __eq checks the contents of a vector which is why 'Vector3.new() == Vector3.new()' returns true but using a vector as a index is similar to how a table may act??

tab = {{1, 2, 3} = true}
print(tab[{1, 2, 3}]) -----> nil
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
11 Jun 2012 10:39 AM
Asside from being a bad idea to index tables with floating point values: As you can see, your idea does not work at all.

You would not be surprised if you saw the following result:
a = {}
a[{}] = 3
print(a[{}]) -->
Right? Just because both tables are empty does not mean that they are the same table.

That's the same for vectors. Just because both vectors have the same data in them, they are not the same vector3 object. You called Vector3.NEW, so it creates a NEW vector3 object, it does not try to find an existing one with the desired dimensions to return.

However, you _can_ do what you want, you just need a custom way of hashing the vector3s that you use as they keys into a data type that will work, such as a number or string.

You could just tostring the vector3s, but that will have issues because when you do tostring on a zero vectors, you could get any one of 0,-0,0 or 0,0,-0, ect.. even though you would like those values to be the same.

A better solution would be looking at a reasonable range of the values of the vector3 and using that to encode all of the components into a single number. Say they all fall in the range -100 to 100:
function Vector3Hash(v)
return v.x + v.y*1000 + v.z*1000*1000
end

Now you can use the results returned from this to do the hash.

Although it's still not a good idea to be using numbers for the hash keys unless you're completely sure that they're integers.
Report Abuse
Radioaktiivinen is not online. Radioaktiivinen
Joined: 25 Apr 2009
Total Posts: 18629
11 Jun 2012 11:25 AM
When i wanted to do that, i just made a function to transform the vector into a string (round the numbers, put in a string separated by spaces or watever) and put the actual vector inside the value (which is a table) so i dont have to use hax to transform the string back to a vector or something.


But that probably lags, but who cares.
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 11:30 AM
^late lol

"As you can see, your idea does not work at all."
"And your solution is also terrible, because it doesn't work"

Are you guys talking about my original plan or the alternative? I know the one with vectors doesn't work but the alternative works just fine...
Oh nvm I just tested it. I accidently messed up the toNum function when I posted it here.

local toNum = function(val) ---------------------Put n instead of val ^.^
--Transforms number to 0 for cases like 0/0
return val == 1 and val or 0
end

sides = {
[(function() return 0, 0, 1 end)()] = {"east", "west"},
[(function() return 1, 0, 0 end)()] = {"north", "south"}
}

local inc = Vector3.new(0, 0, 0) ----Positive or negative number along either x _or_ z axis
dirA = sides[(function(vec) return toNum(vec.x), 0, toNum(vec.z) end)(inc/inc)][1]
dirB = sides[(function(vec) return toNum(vec.x), 0, toNum(vec.z) end)(inc/inc)][2]

print(dirA, dirB)


It works fine like that, but anyways thanks for help ^.^
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 11:30 AM
"^late lol"

ignore that
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
11 Jun 2012 12:50 PM
This codes is not doing what you think it's doing:

    sides = {
        [(function() return 0, 0, 1 end)()] = {"east", "west"},
        [(function() return 1, 0, 0 end)()] = {"north", "south"}
    }

Proof? Look at what this does:

    print( sides[0][0] ) --east

When you return multiple value from a function in a place that expects one value, the rest are discarded. Your table is only using the X coordinate for lookup.
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
11 Jun 2012 01:00 PM
It works fine like that, but anyways thanks for help ^.^"

That does not work as you expect as pointed out above. I'll elaborate:

When you have a list of variables, and you use it in a context where only a single variable is expected, then only the first value form the list will be taken for use:

a[(function() return 1, 0 end)()] = "hello"
print(a[(function() return 1, 1 end)()]) --> hello

What you need to do is create a function which converts your data into a single value. No matter how much you try Lua is not going to do this for you, it has no mechanism for that, and for good reason, there's no reliable way for it to tell when you want two values to be the treated the same, and when you want them to be treated as different. In the case of a vector3 Lua does not even know what data the Vector3 contains, all it knows is that it's a pointer to "something" in the Roblox app, and it has a pointer which will extract unknown things from that "something".
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 01:19 PM
Yes i did notice wierd results from it
table.foreach(sides, print)

but i didn't completly ignore your post(s). what i'm doing only uses those two values so my current method seems to work well enough, but i plan to try getting my full script working before i begin worrying about this again
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 01:26 PM
"When you return multiple value from a function in a place that expects one value, the rest are discarded. Your table is only using the X coordinate for lookup."

not really
"
[(function() return 0, 0, 1 end)()]
[(function() return 1, 0, 0 end)()]
"

when i used table.foreach they were converted to 0 or 1 and they were indexed in the same way. when i added another index, similar to the other two, it just overwrote one of the others
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
11 Jun 2012 03:56 PM
Yes, really. This:

    {
        [(function() return 0, 0, 1 end)()] = "foo"
        [(function() return 1, 0, 0 end)()] = "bar"
    }

Is **identical in every way** to:

    {
        [0] = "foo"
        [1] = "bar"
    }

They are indistinguisable to lua.
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 04:06 PM
again my mistake. anyways i have it figured out now so thank you
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
11 Jun 2012 04:08 PM
"Is **identical in every way** to:"

Except you can do some fancy stuff with it. For an example, have the value change every time the value is accessed, or make it print the value when accessed, etc.

Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
11 Jun 2012 04:37 PM
"have the value change every time the value is accessed"

Except that you'd also need considerably more code than that to do that. Don't give him the idea that:
a[(function() return math.random(1,2) end)()] = 5
Will make the index where 5 is stored change every time you try to access it.
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
11 Jun 2012 04:41 PM
"a[(function() return math.random(1,2) end)()] = 5
Will make the index where 5 is stored change every time you try to access it."

i know better than that lol. . . and i guess checking the "Do not allow replies to this post. " box doesn't mean anything >.<
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image