parent and child entity active states bug?

@dave1707 thanks, that is what i am doing. Although i would just do sphere3.active=true rather than sphere2.children[1].active=true.

Still trying to understand my problems-but part of it is that if the active state of a parent is false and then a child is created, the child will be displayed even though the parent is false and the activeInHierarchy of the child is false. I think there is a bug here.

@piinthesky The table is used because there are more than one child for the parent and you need to set the table value for a specific child. In my code above, the parent is a sphere just above or just to the left of another sphere. If you change the state of a sphere to the right or below another sphere, that state is remembered when the parent state is changed. Maybe I’ll try writing another example that shows a better example of parent/children.

@piinthesky Here’s another parent/child example. The robot is the parent to the shield. The shield is the parent to the blue and white flags. You can turn the shield and flags on/off with the boolean sliders. The state of the flags are remembered when the state of the shield is turned on or off. You can rotate the robot or shield with the sliders.

displayMode(STANDARD)

function setup()
    parameter.number("Rotate", 0, 360, 208)
    parameter.number("shy",-180,180,-30)
    parameter.number("shz",-180,180,30)

    scene = craft.scene()
    
    ground = scene:entity()
    ground.model = craft.model.cube(vec3(10,.2,10))
    ground.material = craft.material("Materials:Specular")
    ground.material.map = readImage("Blocks:Dirt")
    ground.material.specular = color(0, 0, 0, 255)
    ground.material.offsetRepeat = vec4(0,0,1,1)
    ground.y = -1

    robot = scene:entity()
    robot.model = craft.model("Blocky Characters:Robot")
    robot.y = -1
    robot.z = 0
    robot.scale = vec3(1,1,1) / 8

    shield = scene:entity()
    shield.model = craft.model("CastleKit:shieldRed")
    shield.x = -3
    shield.y = 7
    shield.z = 4
    shield.scale = vec3(2.5,2.5,.1)
    
    flag1 = scene:entity()
    flag1.model = craft.model("CastleKit:flagBlueWide")
    flag1.x = 0
    flag1.y = 0
    flag1.z = 0
    flag1.scale = vec3(.5,.5,.5)
    
    flag2 = scene:entity()
    flag2.model = craft.model("CastleKit:flagWhiteWide")
    flag2.x = 0
    flag2.y = 0
    flag2.z = 0
    flag2.scale = vec3(.5,.5,.5)
    
    sword = scene:entity()
    sword.model = craft.model("CastleKit:sword")
    sword.x = 3.2
    sword.y = 6
    sword.z = 1
    sword.scale = vec3(1,1,1)*1.6

    scene.camera.z = -6
    
    shield.parent=robot
    flag1.parent=shield
    flag2.parent=shield
    sword.parent=robot
    
    parameter.boolean("sh",true,   function() robot.children[1].active=sh end)    
    parameter.boolean("blue",true, function() shield.children[1].active=blue end)    
    parameter.boolean("white",true,function() shield.children[2].active=white end)
end

function update(dt)
    robot.eulerAngles=vec3(0,Rotate,0)
    sword.eulerAngles=vec3(45,0,0)
    shield.eulerAngles=vec3(180,shy,shz)
    flag1.eulerAngles=vec3(180,-90,45)
    flag2.eulerAngles=vec3(120,-90,45)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()	   
end

@dave1707, thats a nice example and illustrates how the child inherits the transforms (rotation, displacement, scaling) from the parent.

I fixed all the bugs in my code-so now a happy camper.

@John, new parent/child code works great. The only small bug i found is that if the parent.active=false is set before the child is created then the child will be active.

@piinthesky Maybe when to create a child, you can check the status of the parent and set the child the same as the parent.

@piinthesky Hey, sorry I didn’t post that example code, I’ve been getting ready for an overseas trip. I’ll post it by tomorrow. Thanks for finding that bug, I didn’t consider that particular edge case. It shouldn’t be too hard to fix though.

@dave1707 the child should probably keep it’s own internal active state but the emergent active state will be based on the chain of parents up to the root when the parent is set.

Here’s the code from the sample I showed on twitter:

-- Active Test

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    scene = craft.scene()
    scene.camera.z = -20
    
    objs = {}
    createObjects(objs, vec3(5,0,0), nil, 5)
end

-- This function gets called once every frame
function draw()
    
    for k,v in pairs(objs) do 
        v.y = math.sin(v.x + ElapsedTime)
    end
    
    scene:update(DeltaTime)
    scene:draw() 
end

function createObjects(objs, pos, parent, count)
    local e = scene:entity()
    e.model = craft.model("Primitives:Monkey")
    e.model:getMaterial(0).diffuse = color(math.random(100,200), math.random(100,200), math.random(100,200), 255)
    e.position = pos
    if parent == nil then e.rotation = quat.eulerAngles(0,180,0) end
    e.parent = parent
    table.insert(objs, e)
    
    parameter.boolean("Entity "..(5-count+1).." Active", true, function(b)
        e.active = b
        e.active = not b
        e.active = b
    end)
    
    parameter.watch("objs["..#objs.."].activeInHierarchy")
    parameter.watch("objs["..#objs.."].active")    
    
    if count > 0 then 
        createObjects(objs, vec3(3,0,0), e, count-1)
    end
end