DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
Now, I was doing homework in Flash, and I had come up with a great idea, Instance Names. An Instance Name is a hidden name of an object, that has great possibilities. Now, it would be of help to scripters to find all of a certain item. Now, in ActionScript (In which this idea is borrowed from), this would be the syntax to add an event to all things with the Instance Name of "kole".
kole.addEventListener(MouseEvent.CLICK, clickFunction)
function clickFunction(event:MouseEvent):void {
trace("Click")
}
Now, this will go into the Workspace, and grab all the things with the Instance Name of kole. Now, this would how it would work in Lua:
local Test = InstanceName("kole", workspace.Model)
Test.Touched:connect(function()
print("Touched")
end)
Now, this update would require a new function, called InstanceName(). InstanceName will take 2 arguments, the name it will look for, and the second is what it should look inside. The second can be anything, even game, it just has the be an object that is a descendant of game, and it will look inside that object for the InstanceNames. InstanceName returns all the items with the Instance Name of the first argument that was inside the the second argument. If it doesn't find any, it returns nil, so it would be good check if the variable of InstanceName() isn't nil before you use it.
Now, some of you who are thinking, "Why, this is useless, what's the point of it?" Well, it would help by not having to make so much scripts/listeners to lots of brick that do the same function. It also helps to find all of a certain item so you can manipulate only certain ones, it can even help find viruses that come in some free-models! It would greatly reduce the pain-staking work of making tons of listeners for bricks that do the same function. But, you might say that you can copy and paste, but takes up memory space for more scripts. It also helps for people who make things that require more than one object to be touched, causing a chain-reaction. Like, Chair Racing, or Call of Duty: Black Ops, or Deathrun. Now, how would you tell which Instance Name had triggered the event? Well, we can use another new update called case.
local Test = InstanceName("kole", workspace.Model)
Test.Touched:connect(function()
case (target, Test)
print(target.Name)
if target.Name == "Name" then
print("Found!")
end
end
end
The new item, called case, would go between all the items in the second argument, which is the parts that InstanceName returned, the first is what it will be called inside the case. Now, the first argument is which one triggered the event. You can use if's to check if it the part you want or something.
Now, what if you want all the bricks to the same function? Just do the example 2 above.
What about changing Instance Names? You can do it 3 ways:
Going into the item's Properties and change the InstanceName.
Using the command bar.
Using a script.
Now, changing an InstanceName with a script/command bar will look something like this:
workspace.Model.Part1.InstanceName = "kole"
Before you post "OH THS IZ COMPLETLE USELES OH STUPID IDEA YOU N00B!", think about how useful it would be to scripters and ROBLOX. It would help ROBLOX by not having to pay so much money for extra memory space of more scripts. It would be extremely useful, and already works great with ActionScript, and it would definitely help in ROBLOX.
If you are not a scripter, please try and NOT to post things like above. For the scripters out there, this would greatly help so much things, I can't even name them.
If you don't understand it, ask me to dumb it down for you.
NO TL;DR!
All hail the Power of DX! |
|
|
> Now, it would be of help to scripters to find all of a certain item
Can't we find things using normal names fine? |
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
I know, but you would have to use a bunch of for loops, and :GetChildren(). This speeds up the process.
All hail the Power of DX! |
|
|
> This speeds up the process.
The InstanceName() function would have to use for loops and :GetChildren() to find the items anyway. |
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
I know, but it could find all the items with a certain InstanceName in game with 1 line of code.
local Found = InstanceName("FindMe!", game)
All hail the Power of DX! |
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
Bump.
All hail the Power of DX! |
|
KachanceJoin Date: 2009-01-21 Post Count: 3231 |
Hmm... Support I suppose.
Not completely sure what I'd use this for though. |
|
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
Yay, some supports! |
|
VarpJoin Date: 2009-11-18 Post Count: 5333 |
This can already be accomplished, if I understand correctly (it's a bit weird, especially the "target" thing; I feel that that should probably just be a parameter somewhere).
What I'd do would be:
function childrenWithPredicate(obj,pred,recursive,t)
t = t or {}
for i,v in pairs(obj:GetChildren())do
if(pred(v))then
table.insert(t,v)
end
if(recursive)then
childrenWith(v,pred,true,t)
end
end
return t
end
function childrenWithProperty(obj,prop,recursive)
return childrenWithPredicate(obj,function(v) return v:FindFirstChild(prop) end,recursive)
end
In this code, two functions are defined which should be adequate to do, really anything:
childrenWithProperty(obj, prop, [recursive])
This function sorts through all the children of obj and returns all children that have an object named prop as their child. If recursive is true, it will go through not just children, but all descendants. So, if you put a Model (or some other dummy object - only the name matters) named "kole" in every object you want to function the same way, you can find all of them through this function. For example:
for i,target in pairs(childrenWithProperty("kole",workspace,true))do
target.Touched:connect(function()
print(target.Name .. " was touched")
end)
end
Would find every object that has a child named "kole" and connect a function to all of them.
childrenWithPredicate(obj,pred,[recursive])
Similar to childrenWithProperty. This function accepts an object, obj, and a function, pred. If pred(o) is true, then o is included in the returned list.
(I didn't actually test the code so... it might not work) |
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
But, would your rather do that or do it all in one line? |
|
VarpJoin Date: 2009-11-18 Post Count: 5333 |
"But, would your rather do that or do it all in one line?"
Mine. It's generalized, doesn't involve introducing new constructs ("case" cannot be expressed in plain Lua, even from the C side), and can be used at this moment - this suggestion needs an admin. Also, I would argue that my script actually does do it all in one line - the length of the function can be disregarded, since you only have to write that once, but you can use it forever. Yeah, you have to use an extra for loop, but that's just an extra "end" you have to write, and you could do away with that by modifying the function.
I'll admit that my last script was not optimal. This one basically is:
function toObjectsWithProp(obj,prop,f)
for i,v in pairs(obj:GetChildren())do
if(v:FindFirstChild(prop))then f(v) end
if(recursive) toObjectsWithProp(v,prop,true,f)
end
end
toObjectsWithProp(game,"kole",function(target)
target.Touched:connect(function() print("Hi") end)
end)
Behold! The application of this function AND its side effects takes just 3 lines, and the function itself is just 6 lines! Surely, you cannot object to using something this simple?
Also, there is an error in my previous script. The line:
childrenWith(v,pred,true,t)
should be
childrenWithPredicate(v,pred,true,t)
|
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
But, this suggestion would be very helpful, and wouldn't take as much memory than your would. Even though yours seems legit, is it a no or yes support? |
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
Bumpy. |
|
|
> and wouldn't take as much memory than your would.
Actually, it would take less. They would have to add this to EVERY class in Roblox, meaning everything your game consists of has two or three more methods in them, vs a table which is just a table. Just because you see less words and work, doesn't mean it's more efficient. So unless your game is just a single brick, his method would take less memory. |
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
Yet still, support or no support?
All hail the Power of DX! |
|
chicka123Join Date: 2008-07-05 Post Count: 25356 |
Support, but I don't know how case works.. but im just a beginner-normal Lua scripter. |
|
DXPowerJoin Date: 2008-10-21 Post Count: 2866 |
Case, is again, borrowed from ActionScript 3.0. And, it will help with the finding out of which one set the event with the new InstanceName() in this suggestion.
All hail the Power of DX! |
|