New Objects Via Script
Sora574
07 Mar 2013, 05:53Ok, here's the problem.
I can't for the life of me figure out how to get this to work:
Basically, what this is trying to do is create a new object with the alias of an object, say 'spoon', with a prefix, '_container' (spoon_container) and then set the attribute 'container' to the new object, spoon_container.
However, this doesn't work, because there's no possible way of retrieving the new object's name in a script. Unless I'm just not seeing it?...
Oh, and I also tried this:
This just popped up with an error reading 'Error running script: Cannot change name of element when not in Edit mode'...
I deem this error pointless.
I mean, I just don't understand the purpose of this. If I want to change the name instead of the alias (because using an alias would make it impossible as well) then why should I be denied that right?
Of course, I'm just rambling, though.
I can't for the life of me figure out how to get this to work:
create (object.alias + "_container")
container = object.alias + "_container"
Basically, what this is trying to do is create a new object with the alias of an object, say 'spoon', with a prefix, '_container' (spoon_container) and then set the attribute 'container' to the new object, spoon_container.
However, this doesn't work, because there's no possible way of retrieving the new object's name in a script. Unless I'm just not seeing it?...
Oh, and I also tried this:
create ("_container")
container = _container
_container.name = object.alias + _container.name
This just popped up with an error reading 'Error running script: Cannot change name of element when not in Edit mode'...
I deem this error pointless.
I mean, I just don't understand the purpose of this. If I want to change the name instead of the alias (because using an alias would make it impossible as well) then why should I be denied that right?
Of course, I'm just rambling, though.

Pertex
07 Mar 2013, 07:42object names must be unique but you could try to set the name of an new object equals to the name of an existing object via script. Quest can't recognize that and and your game would crash. So this error message is the easiest way to prevent you from doing something bad 

Sora574
07 Mar 2013, 07:56Pertex wrote:object names must be unique but you could try to set the name of an new object equals to the name of an existing object via script. Quest can't recognize that and and your game would crash. So this error message is the easiest way to prevent you from doing something bad
Well although that makes sense... I think the editor's thing that makes a unique name should be implemented into the create () script. Eh, thanks anyway.
What about the first half of my question?
Alex
07 Mar 2013, 09:05You can use the GetObject function to return an object named by a string.
levicki
07 Mar 2013, 14:19What you want to do is actually very simple:
This will create an object and assign it (as an object!) to the object it contains. if for some reason you would like to assign it as a string instead, then you can just omit GetObject() function call.
I have attached a demo game with a function CreateContainer() which does this with any object passed in as a parameter. There is an option Enchant which demonstrates how to call the function. If you Enchant Wooden Spoon you can confirm in debugger window that a "spoon_container" has been created and assigned to "spoon.container".
create (obj.name + "_container")
obj.container = GetObject(obj.name + "_container")
This will create an object and assign it (as an object!) to the object it contains. if for some reason you would like to assign it as a string instead, then you can just omit GetObject() function call.
I have attached a demo game with a function CreateContainer() which does this with any object passed in as a parameter. There is an option Enchant which demonstrates how to call the function. If you Enchant Wooden Spoon you can confirm in debugger window that a "spoon_container" has been created and assigned to "spoon.container".
Sora574
07 Mar 2013, 21:18Wow, how did I miss that?
Thanks to both of you.
By the way, basically what I'm doing is making a simple way to 'stack' objects, so it's not a literal 'container', it's more like just a null object.
Thanks to both of you.
By the way, basically what I'm doing is making a simple way to 'stack' objects, so it's not a literal 'container', it's more like just a null object.
levicki
07 Mar 2013, 23:10Sora574 wrote:Wow, how did I miss that?
Thanks to both of you.
By the way, basically what I'm doing is making a simple way to 'stack' objects, so it's not a literal 'container', it's more like just a null object.
My own very simple method for stacking:
// create object and stack it
obj = GetObject("apple")
if (obj = null) {
create ("apple")
obj = GetObject("apple")
}
else {
CloneObject(obj)
}
StackObject(obj)
// function for creating stack for every type of object
<function name="StackObject" parameters="obj">
stack = GetObject(obj.name + "_stack")
if (stack = null) {
create (obj.name + "_stack")
stack = GetObject(obj.name + "_stack")
stack.container = true
stack.count = 1
stack.parent = player
obj.parent = stack
}
else {
obj.parent = stack
stack.count = stack.count + 1
}
stack.alias = obj.name + " (" + stack.count + ")"
</function>
Check out the demo game I attached, hopefully you can figure out the rest.
Sora574
07 Mar 2013, 23:33I think you misunderstood me.
I already have all the code I need for stacking, I just needed help with making a new object.
Thanks anyway though
I already have all the code I need for stacking, I just needed help with making a new object.
Thanks anyway though

jaynabonne
08 Mar 2013, 12:25Attached is some code I use for object management. There are methods for allocating, cloning and eventually freeing objects. Maybe it will help. 
