Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2 Lambda timer scope issues #3233

Open
unknao opened this issue Dec 28, 2024 · 3 comments
Open

E2 Lambda timer scope issues #3233

unknao opened this issue Dec 28, 2024 · 3 comments

Comments

@unknao
Copy link
Contributor

unknao commented Dec 28, 2024

@name  
@strict
@trigger none

propSpawnUndo(0)
let E1 = propSpawn("models/props_junk/CinderBlock01a.mdl", entity():toWorld(vec(30, 0, 30)), ang(), 1)
let E2 = propSpawn("models/props_junk/CinderBlock01a.mdl", entity():toWorld(vec(0, 0, 30)), ang(), 1)
let E3 = propSpawn("models/props_junk/CinderBlock01a.mdl", entity():toWorld(vec(-30, 0, 30)), ang(), 1)
const A = array(E1, E2, E3)

foreach(_:number, Ent:entity = A){
    local E = Ent
    print("test_initial_" + E:id())
    timer("test_result" + E:id(), 1, 1, function(){
        print("test_result" + E:id())
        E:setPos(E:pos() + vec(0, 0, 10))
    })
}
printTable(getTimers())

This E2 will error because it tries to move the E3 entity 3 times instead of moving all of them once

@unknao
Copy link
Contributor Author

unknao commented Dec 28, 2024

@strict

for(I = 1, 9){
    timer(1, function(){
        print(I)
    })
}

Actually this is a better example, as this prints 9 9 times

@Denneisk
Copy link
Member

Loops share the same scope for their bodies instead of creating a new scope each iteration. Changing this would be a performance decrease on all loop performance. You can mitigate this issue by creating a new scope manually such as with if(1) { ... }, or by using a function.

for(I = 1, 9) {
    if(1) {
        let X = I
        timer(1, function() {
            print(X)
        })
    }
}

@Vurv78
Copy link
Contributor

Vurv78 commented Dec 30, 2024

I feel like people will definitely encounter this case so a scope being created per iteration might be needed. Potential half measure is if this only done as long as a function was created inside.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants