1
\$\begingroup\$
Class entity() 
{ // there is obviously more code but i dont think if adding them will help 
    val bodyInTheWorld: Body? = null // nulls mean that it will be initialised later, obviously
}

I need to set it to null because I need to do a

createThis.bodyInTheWorld = world!!.createBody(createThis.body)

in main.kt. And that I don't have access to the world object of the main class inside my entity.kt class

class main : ApplicationAdapter() {
    private var batch: SpriteBatch? = null
    private var image: Texture? = null
    private var player: entity? = null
    private var camera: OrthographicCamera? = null
    private var world: World? = null
    private val entities = mutableListOf<entity>()

    fun createObject(createThis: entity) // will create reference or copy issues probably...
    {
        if(!entities.contains(createThis))
                entities.add(createThis)
        createThis.bodyInTheWorld = world!!.createBody(createThis.body)
        var temporaryShape = PolygonShape()
        temporaryShape.setAsBox(50f, 1f)
        createThis.bodyInTheWorld.createFixture(temporaryShape, 1f)
    }

I was about to write more code but I couldn't fix this damn thing: if I set it to var the last line will tell me that it requires an immutable argument.

Smart cast to 'Body!' is impossible, because 'createThis.bodyInTheWorld' is a mutable property that could have been changed by this time

And if I set it to val, the code doesn't let me do the main thing I want, which is to make that body exist in the world.

        createThis.bodyInTheWorld = world!!.createBody(createThis.body)

This time the problem is, by syntax and complexity, more reasonable at least, it tells me that I can only define bodyInTheWorld variable only once and that I already did that by setting it to null in entity.kt

I need help on this. This is the guide I used to implement box2d btw

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Well, so i added a double exclamation mark to the end of the createThis.bodyInTheWorld createThis.bodyInTheWorld!!.createFixture(temporaryShape, 1f) and everything suddenly started to work, i still have no idea how it works at all, anyone care to explain?

\$\endgroup\$
1
  • \$\begingroup\$ i guess mutability has something to do with nulls and this somehow fixes it \$\endgroup\$ Dec 6, 2021 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .