Half My Code Disappears

So when I’m testing one of my projects, it will sometimes remove just about all the code in the project after I go back into the editor and the only way to get it back is to close the app completely.

@Creator27 does this happen consistently? Are you able to reproduce it?

If so, I would really appreciate if you could share more about this bug

@Simeon, yes this does happen consistently and yes the code does reappear when I like close the app and reopen it. For example, I might be deleting some unwanted code in my project or going back into the editor to change something in my code when suddenly, the code will start to flicker out of sight, and just like magic, it’s suddenly gone.

@Creator27 does this happen on all projects? Or just some specific ones? Curious if it’s to do with a particular size of screen and section of text that happens to wrap or render incorrectly in that exact configuration

If you have an example / let me know what device you are using I’ll be happy to try reproduce

@Simeon, I would say that it doesn’t only because I’ve seen it happen only on a couple of my projects. I am using an iPad 8th Generation and here’s one of the projects that I’m working on (it’s not quite finished and sorry about the mess):

-- Odd
--assert(OrbitViewer)
function setup()
    -- Create a new craft scene
    scene = craft.scene()
    scene.physics.gravity = vec3(0, -1, 0)
    scene.ambientColor = color(74)
    scene.sky.material.sky = color(255, 72, 0)
    scene.sky.material.horizon = color(224, 147, 143) 
    cameraSettings = scene.camera:get(craft.camera)
    cameraSettings.nearPlane = 0.001
    viewer.mode = FULLSCREEN
    rotation = -15.85
    rotation2 = 180
    rotation3 = -2.15
    rotation4 = -526
    lavaRot = 0
    coinRot = 0
    dead = false
    emActive = false
    battleMode = false
    playSound = false
    playSound2 = false
    playSound3 = false
    playSound4 = false
    playSound5 = false
    playSound6 = false
    playSound7 = false
    playSound8 = false
    playSound9 = false
    startCalc = false
    playHitSound = false
    startAnim = false
    ultraMove = false
    pui1 = false
    pui2 = false
    finalMove = false
    cC = false
    gp = false
    elim1 = 0
    elim2 = 0
    pu1 = {x = 0, y = 0}
    pu2 = {x = 0, y = 0}
    k1 = {x = 0, y = 0}
    k2 = {x = 0, y = 0}
    lavaS = {x = 0.12, y = 0.12, z = 0.12}
    goA = {x = 255}
    eP = {x = 0, y = 0, z = 0}
    tS = {x = 100}
    ttS = {x = 0.2, y = 0.1}
    hS = {x = 30, y = 30}
    lS = {x = 0.045, y = 0.045, z = 0.045}
    sY = {y = -0.093}
    s = 10  
    pH = 2
    eH = 2
    rC = 1
    
    -- Create a new entity
    earth = scene:entity()
    eab = earth:add(craft.rigidbody, STATIC)
    earth.model = craft.model(asset.builtin.Primitives.Sphere)
    earth:add(craft.shape.model, earth.model)
    earth.position = vec3(0, 0, 0)
    earth.eulerAngles = vec3(0, 0, 0)
    earth.scale = vec3(1, 1, 1) / 8
    earth.material = craft.material(asset.builtin.Materials.Standard)
    earth.material.map = readImage(asset.builtin.Blocks.Grass_Top)
    earth.material.offsetRepeat = vec4(12.0, 12.0, 10.0, 10.0)
    eab.restitution = 10
    
    player = scene:entity()
    body = player:add(craft.rigidbody, STATIC)
    player.model = craft.model(asset.builtin.Primitives.Sphere)
    player:add(craft.shape.model, player.model)
    player.position = vec3(0, 0, -0.4)
    player.eulerAngles = vec3(0, 0, 0)
    player.scale = vec3(0.1, 0.1, 0.1) / 8 
    player.material = craft.material(asset.builtin.Materials.Standard)
    player.material.diffuse = color(235, 76, 68)
    body.restitution = 1
    
    enemy = scene:entity()
    eb = enemy:add(craft.rigidbody, STATIC)
    enemy.model = craft.model(asset.builtin.Primitives.Sphere)
    enemy:add(craft.shape.model, enemy.model)
    enemy.position = vec3(0, 0, -0.4)
    enemy.eulerAngles = vec3(0, 0, 0)
    enemy.scale = vec3(0.1, 0.1, 0.1) / 8
    enemy.material = craft.material(asset.builtin.Materials.Standard)
    enemy.material.diffuse = color(30, 131, 55)
    eb.restitution = 1
    
    sea = scene:entity()
    sea.model = craft.model(asset.documents.Dropbox.Donut)
    sea.position = vec3(0, sY.y, -0.06)
    sea.rotation = quat.eulerAngles(0, 0, 0)
    sea.scale = vec3(lS.x, lS.y, lS.z) / 8
    sea.material = craft.material(asset.builtin.Materials.Standard)
    sea.material.map = readImage(asset.builtin.Blocks.Lava)
    sea.material.offsetRepeat = vec4(6.0, 6.0, 5.0, 5.0)
    
    sun = scene:entity()
    sun.position = vec3(-11, 8, 20)
    sun.eulerAngles = vec3(0, 0, 0)
    rays = sun:add(craft.light, DIRECTIONAL)
    rays.angle = 60
    rays.intensity = 1.2
    
    img=image(320,160)
    setContext(img)
    background(0,0,0,0)
    fontSize(20)
    sprite(asset.Heart_Count,160,80,hS.x,hS.y)
    setContext()
    
    img2=image(320,160)
    setContext(img2)
    background(0,0,0,0)
    fontSize(20)
    sprite(asset.Heart_Count_Half,160,80,30,30)
    setContext()
    
    img3=image(320,160)
    setContext(img3)
    background(0,0,0,0)
    fontSize(20)
    sprite(asset.Heart_Count_Empty,160,80,30,30)
    setContext()
    
    img4=image(320,160)
    setContext(img4)
    background(0,0,0,0)
    fontSize(20)
    sprite(asset.Heart_Count,160,80,30,30)
    setContext()
    
    img5=image(320,160)
    setContext(img5)
    background(0,0,0,0)
    fontSize(20)
    sprite(asset.Heart_Count_Half,160,80,30,30)
    setContext()
    
    img6=image(320,160)
    setContext(img6)
    background(0,0,0,0)
    fontSize(20)
    sprite(asset.Heart_Count_Empty,160,80,30,30)
    setContext()
    
    img7=image(320,160)
    setContext(img7)
    background(0,0,0,0)
    fontSize(20)
    sprite(asset.Kill_Count,160,80,30,30)
    fill(0)
    font("Futura-Medium")
    text(elim1,175,65)
    setContext()
    
    img8=image(320,160)
    setContext(img8)
    background(0,0,0,0)
    fontSize(20)
    sprite(asset.Kill_Count_Enemy,160,80,30,30)
    fill(0)
    font("Futura-Medium")
    text(elim2,140,65)
    setContext()
    
    parameter.watch("player.y")
    parameter.watch("player.x")
    parameter.watch("rotation2")
    parameter.watch("rotation")
    parameter.watch("rotation3")
    parameter.watch("rotation4")
    parameter.watch("scene.camera.rotation.x")
    parameter.watch("scene.camera.rotation.y")
    
    scene.camera.z = -.5
    createCube()
    shockwave()
    buildPineTree(vec3(0.05, 0.05, -0.105), vec3(-60, -40, 0))
    buildPineTree(vec3(-0.05, -0.05, -0.105), vec3(-120, 40, 0))
    makeCoin()
    makeGun()
    
    goUpOne = tween(1, coin, {position = vec3(0, -0.023, -0.15)}, tween.easing.cubicInOut)
    goUpTwo = tween(1, coin, {position = vec3(0, -0.003, -0.15)}, {easing = tween.easing.cubicInOut, loop = tween.loop.pingpong})
    tween.sequence(goUpOne, goUpTwo)
    
    if gp == true then
        
    end
 
end
function makeSpike(p, e)
    spike = scene:entity()
    sb = spike:add(craft.rigidbody, STATIC)
    spike.model = craft.model(asset.builtin.CastleKit.towerTopRoof_obj)
    spike:add(craft.shape.model, spike.model)
    spike.position = p
    spike.rotation = e
    spike.scale = vec3(0.01, 0.01, 0.01) / 8
end

function makeLava(p, e)
    lava = scene:entity()
    lb = lava:add(craft.rigidbody, STATIC)
    lava.model = craft.model(asset.builtin.Primitives.Sphere)
    lava:add(craft.shape.model, lava.model)
    lava.position = p
    lava.rotation = e
    lava.scale = vec3(0.12, 0.12, 0.12) / 8
    lava.material = craft.material(asset.builtin.Materials.Standard)
    lava.material.map = readImage(asset.builtin.Blocks.Lava)
end

function shoot(p, e)
    bullet = scene:entity()
    bb = bullet:add(craft.rigidbody, DYNAMIC)
    bullet.model = craft.model(asset.builtin.Primitives.Sphere)
    bullet.position = p
    bullet.rotation = e
    bullet.scale = vec3(0.05, 0.05, 0.05) / 8
    bullet.material = craft.material(asset.builtin.Materials.Standard)
    bullet.material.diffuse = color(255, 171, 0)
    bullet.centreOfMass = 10
    bb.restitution = 1
end

function shockwave()
    wave = scene:entity()
    wave.model = craft.model(asset.documents.Dropbox.Shockwave)
    wave.position = vec3(0, 0, 0)
    wave.eulerAngles = vec3(0, 0, 0)
    wave.scale = vec3(1, 1, 1) / 8
    wave.material = craft.material(asset.builtin.Materials.Skybox)
    
end

function createCube(x, y, z)
    t=scene:entity()
    t.position=vec3(0,0,0)
    t.model = craft.model.plane(vec2(ttS.x,ttS.y))
    t.material = craft.material(asset.builtin.Materials.Standard)
    t.material.blendMode = NORMAL  
    t.material.map = img
    
    e=scene:entity()
    e.position=vec3(0,0,0)
    e.model = craft.model.plane(vec2(0.2,0.1))
    e.material = craft.material(asset.builtin.Materials.Standard)
    e.material.blendMode = NORMAL  
    e.material.map = img4
    
    b=scene:entity()
    b.position=vec3(0,0,0)
    b.model = craft.model.plane(vec2(0.4,0.2))
    b.material = craft.material(asset.builtin.Materials.Standard)
    b.material.blendMode = NORMAL  
    b.material.map = img7
    
    c=scene:entity()
    c.position=vec3(0,0,0)
    c.model = craft.model.plane(vec2(0.4,0.2))
    c.material = craft.material(asset.builtin.Materials.Standard)
    c.material.blendMode = NORMAL  
    c.material.map = img8
end

function homeMissiles()
    -- Create dangers to the players
    home = scene:entity()
    hb = home:add(craft.rigidbody, DYNAMIC)
    home.model = craft.model(asset.builtin.Primitives.Sphere)
    home.position = p
    home.rotation = e
    home.scale = vec3(0.05, 0.05, 0.05) / 8
    home.material = craft.material(asset.builtin.Materials.Standard)
    home.material.diffuse = color(0)
    home.centreOfMass = 10
    hb.restitution = 1
end

function buildPineTree(p, e)
    pine = scene:entity()
    pb = pine:add(craft.rigidbody, STATIC)
    pine.model = craft.model(asset.documents.Dropbox.treePine_obj)
    pine:add(craft.shape.model, pine.model)
    pine.position = p
    pine.eulerAngles = e
    pine.scale = vec3(0.2, 0.2, 0.2) / 8
end

function makeCoin()
    coin = scene:entity()
    cb = coin:add(craft.rigidbody, STATIC)
    coin.model = craft.model(asset.documents.Dropbox.coinGold_obj)
    coin:add(craft.shape.model, coin.model)
    coin.position = vec3(0, -0.013, -0.15)
    coin.eulerAngles = vec3(0, 0, 0)
    coin.scale = vec3(0.5, 0.5, 0.5) / 8
end

function makeGun()
    gun = scene:entity()
    gun.model = craft.model(asset.documents.Dropbox.machinegun_obj)
    gun.position = vec3(player.x, player.y, player.z)
    gun.eulerAngles = vec3(0, 0, 0)
    gun.scale = vec3(1.5, 1.5, 1.5) / 8
end

function powShoot()
    powbullet = scene:entity()
    powbullet.model = craft.model(asset.builtin.Primitives.Sphere)
    powbullet.position = vec3(gun.position.x, gun.position.y, gun.position.z)
    powbullet.scale = vec3(1, 1, 1) / 8
end

function touched(touch)  
    if touch.tapCount == 3 and gp == false then
        tween(0.5, scene.camera, {y = scene.camera.y + 0.0045, z = gun.position.z}, tween.easing.cubicInOut)
        gp = true
    end
    
    if touch.state == BEGAN then
        if scene.camera.x == gun.position.z then
            
        end
        end
        if touch.x < WIDTH/2 then
        if scene.camera.z ~= gun.position.z then
        if touch.tapCount == 2 and touch.state == BEGAN then
            sound(SOUND_SHOOT, 24414)
            shoot(vec3(player.x, player.y, player.z), quat.eulerAngles(rotation2, rotation, 0))
            bb:applyForce(vec3(enemy.position.x + rotation, enemy.position.y - -rotation - 0.05, enemy.rotation.z + 0.4 + player.rotation.z * 2))
            startCalc = true
        end   
       end 
        if battleMode == true then
        if touch.state == MOVING then
            if pui1 == false then
                tween(20, pu1, {x = 150, y = 150}, tween.easing.cubicInOut)
            end
            pui1 = true
        end
        end
      
    if touch.state == MOVING and touch.deltaX > 0 then
        rotation = rotation + 2
    end
    
    if touch.state == MOVING and touch.deltaX < 0 then
        rotation = rotation - 2
    end
    
    if touch.state == MOVING and touch.deltaY > 0 then
        rotation2 = rotation2 + 2
    end
    
    if touch.state == MOVING and touch.deltaY < 0 then
        rotation2 = rotation2 - 2
        end
    end
    if touch.x > WIDTH/2 then
        if touch.state == MOVING and touch.deltaX > 0 then
            rotation3 = rotation3 - 2
        end
        
        if touch.state == MOVING and touch.deltaX < 0 then
            rotation3 = rotation3 + 2
        end
        
        if touch.state == MOVING and touch.deltaY > 0 then
            rotation4 = rotation4 - 2
        end
        
        if touch.state == MOVING and touch.deltaY < 0 then
            rotation4 = rotation4 + 2
        end
    end
end

function update(dt)
    -- Update the scene (physics, transforms etc)
    scene:update(dt)
    if gp == false then
        player.rotation = quat.eulerAngles(rotation2, rotation, 0)    
        player.position = player.forward * 0.068 * 2.0
    end
    
    gun.rotation = quat.eulerAngles(0, -rotation, 0)    
    
    if scene.camera.z == gun.position.z then
        scene.camera.rotation = quat.eulerAngles(scene.camera.y, -rotation, 0) 
        if battleMode == true then
           
        end
    end
    
    enemy.rotation = quat.eulerAngles(rotation3, rotation4, 0) 
    enemy.position = enemy.forward * 0.068 * 2.0
    
    coinRot = coinRot + 2
    coin.eulerAngles = vec3(0, coinRot, 0)
end
-- Called automatically by codea 
function draw()
    update(DeltaTime)
    
    -- Draw the scene
    scene:draw() 
    
    if battleMode == true then
        t.rotation=quat.eulerAngles(-90,scene.camera.eulerAngles.y,0)
        t.position=vec3(player.x, player.y + 0.03, player.z)
        e.rotation=quat.eulerAngles(-90,scene.camera.eulerAngles.y,0)
        e.position=vec3(enemy.x, enemy.y + 0.03, enemy.z)
        b.rotation=quat.eulerAngles(-90,scene.camera.eulerAngles.y,0)
        b.position=vec3(player.x + 0.02, player.y - 0.035, player.z)
        c.rotation=quat.eulerAngles(-90,scene.camera.eulerAngles.y,0)
        c.position=vec3(enemy.x - 0.02, enemy.y - 0.035, enemy.z)
        ellipse(WIDTH/2 - 350, HEIGHT/2 - 300, pu1.x, pu1.y)
        ellipse(WIDTH/2 + 350, HEIGHT/2 - 300, pu2.x, pu2.y)
    end
    
    if battleMode == true then
        if scene.camera.z == gun.position.z then
            ray = scene.physics:raycast(vec3(gun.position.x, gun.position.y, gun.position.z), vec3(0, 0, 50) * 100)
            l = scene.debug:line(vec3(gun.position.x, gun.position.y, gun.position.z), vec3(-rotation, 0, 50), color(255))
        end
    end
    
    if battleMode == false then
        font("Futura-Medium")
        fontSize(tS.x)
        text(s, WIDTH/2, HEIGHT/2 + 320)
        nOS = 1
        if ElapsedTime > 1 then
            s = 9
            if playSound == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            
            playSound = true
        end
        if ElapsedTime > 2 then
            s = 8
            if playSound2 == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            playSound2 = true
        end
        if ElapsedTime > 3 then
            s = 7
            if playSound3 == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            playSound3 = true
        end
        if ElapsedTime > 4 then
            s = 6
            if playSound4 == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            playSound4 = true
        end
        if ElapsedTime > 5 then
            s = 5
            if playSound5 == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            playSound5 = true
        end
        if ElapsedTime > 6 then
            s = 4
            if playSound6 == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            playSound6 = true
        end
        if ElapsedTime > 7 then
            s = 3
            if playSound7 == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            playSound7 = true
        end
        if ElapsedTime > 8 then
            s = 2 
            if playSound8 == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            playSound8 = true
        end
        if ElapsedTime > 9 then
            s = 1
            if playSound9 == false then
                sound(SOUND_PICKUP, 7846)
                tS.x = 210
                tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            end
            playSound9 = true
        end
        if ElapsedTime > 10 then
            s = 0
            sound(SOUND_POWERUP, 7866) 
            tS.x = 210
            tween(0.25, tS, {x = 100}, tween.easing.cubicInOut)
            battleMode = true
        end
    else
        if ElapsedTime < 11 then
            fontSize(tS.x)
            text("BATTLE!", WIDTH/2, HEIGHT/2 + 320)
        end
        calc()
        calcCoin()
    end
    
    if startCalc == true then
        calcBullet()
    end
    
    if emActive == true then
        fill(goA.x)
        rect(-1, -1, WIDTH/0.1, WIDTH/0.1)
        tween(1, goA, {x = 0})
    end
end

function calc()
    if battleMode == true then
    if dead == false then
    x = math.abs(player.position.x - enemy.position.x)
    y = math.abs(player.position.y - enemy.position.y)
    z = math.abs(player.position.z - enemy.position.z)
    if x<.02 and y<.02 and z<.02 then
        if finalMove == true then
          print("hit")
          playHitSound = false
          if playHitSound == false then
              sound(SOUND_HIT, 9683)
              pH = 1
              if pH == 1 then
                  t.material.map = img2
              end
                    
              if pH == 0 then
                  t.material.map = img3
              end
        end
          playHitSound = true
          if pH == 0 then
          dead = true
          if dead == true then
              zoomOne = tween(0.25, cameraSettings, {fieldOfView = 35}, tween.easing.cubicIn)
              zoomTwo = tween(0.25, cameraSettings, {fieldOfView = 45}, tween.easing.cubicOut)
              tween.sequence(zoomOne, zoomTwo)
              tween(1.5, player, {scale = vec3(0, 0, 0)}, tween.easing.cubicIn, function() player:destroy() sound(SOUND_EXPLODE, 26760) emActive = true if ElapsedTime > 2 then sound(SOUND_RANDOM, 43522) end end)
            end
            end   
            end
            end
                
            x2 = math.abs(enemy.position.x - player.position.x)
            y2 = math.abs(enemy.position.y - player.position.y)
            z2 = math.abs(enemy.position.z - player.position.z)
            if x2<.02 and y2<.02 and z2<.02 then
                if finalMove == true then
                print("hit")
                    playHitSound = false
                    if playHitSound == false then
                        sound(SOUND_HIT, 9683)
                        eH = 1
                        if eH == 1 then
                            e.material.map = img2
                        end
                    end
                playHitSound = true
                if eH == 0 then
                dead = true
                if dead == true then
                    zoomOne = tween(0.25, cameraSettings, {fieldOfView = 35}, tween.easing.cubicIn)
                    zoomTwo = tween(0.25, cameraSettings, {fieldOfView = 45}, tween.easing.cubicOut)
                    tween.sequence(zoomOne, zoomTwo)
                    tween(1.5, enemy, {scale = vec3(0, 0, 0)}, tween.easing.cubicIn, function() enemy:destroy() sound(SOUND_EXPLODE, 26760) emActive = true end)
                end
            end   
            end
        end
        end
        end
    end

function calcBullet()
    if battleMode == true then
    if dead == false then
    x2 = math.abs(enemy.position.x - bullet.position.x)
    y2 = math.abs(enemy.position.y - bullet.position.y)
    z2 = math.abs(enemy.position.z - bullet.position.z)
    if x2<.02 and y2<.02 and z2<.02 then
        print("hit")
        sound(SOUND_HIT, 9683)
        bullet:destroy()
        dead = true
        if dead == true then
            zoomOne = tween(0.25, cameraSettings, {fieldOfView = 35}, tween.easing.cubicIn)
            zoomTwo = tween(0.25, cameraSettings, {fieldOfView = 45}, tween.easing.cubicOut)
            tween.sequence(zoomOne, zoomTwo)
            tween(1.5, enemy, {scale = vec3(0, 0, 0)}, tween.easing.cubicIn, function() enemy:destroy() sound(SOUND_EXPLODE, 26760) emActive = true end)
        end
        end
        end
    end
end

function calcCoin()
    if battleMode == true then
    x = math.abs(player.position.x - coin.position.x)
    y = math.abs(player.position.y - coin.position.y)
    z = math.abs(player.position.z - coin.position.z)
    if x<.014 and y<0014 and z<0014 then
            if cC == false then
                coin:destroy()
                sound(SOUND_PICKUP, 32621)
                cC = true
            end   
        end
        
        x2 = math.abs(enemy.position.x - coin.position.x)
        y2 = math.abs(enemy.position.y - coin.position.y)
        z2 = math.abs(enemy.position.z - coin.position.z)
        if x2<.014 and y2<0014 and z2<0014 then
            if cC == false then
                coin:destroy()
                sound(SOUND_PICKUP, 32621)
                cC = true
            end   
        end
    end
end

@Simeon, you might also need to change some of the models as they’re from my Dropbox folder.