1

How can I assign variable to another variable name? for example, I would like to achieve the following

        function ballPosition(ballType)
            balls[ball].ballType.x  = balls[ball].x
            balls[ball].ballType.y  = balls[ball].y
        end

    if balls[ball].type == "fireBall" then
        ballPosition(fireBall)
    elseif balls[ball].type == "powerLessBall" then
        ballPosition(powerLessBall)
    end

at the moment i have:

        if balls[ball].type == "fireBall" then
            balls[ball].fireBall.x  = balls[ball].x
            balls[ball].fireBall.y  = balls[ball].y
        elseif balls[ball].type == "powerLessBall" then
            balls[ball].powerLessBall.x = balls[ball].x
            balls[ball].powerLessBall.y     = balls[ball].y
        end

1 Answer 1

2

balls[ball][balls[ball].type].x = balls[ball].x

or more clearly

balltype = balls[ball].type
balls[ball][balltype].x = balls[ball].x
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.