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

local variables #2

Open
lispm opened this issue Nov 1, 2019 · 1 comment
Open

local variables #2

lispm opened this issue Nov 1, 2019 · 1 comment

Comments

@lispm
Copy link

lispm commented Nov 1, 2019

small improvement to your code: in Common Lisp setting a variable is a different operation from defining it.

(defun check-collision-all (item)
  (setf collides nil)
  (loop
    :for elem :in (nth (- *game-state* 2) *levels*)
    :when (check-collision item elem)
    :do (setf collides t))
  collides)

You have that pattern in your code elsewhere, too. load-level and check-collision for example.

SETF does not define a variable, but sets it only. To define a function local variable one has two options: let and (not that often seen) &aux:

(let ((collides nil))
  (loop ... (setf collides t) ...)
  collides)

or

(defun check-collision-all (item &aux (collides nil))
   (loop ... (setf collides t) ...)
   collides)

Otherwise collides possibly would be a global and undefined variable. A Common Lisp compiler will typically warn about that.

@srcnalt
Copy link
Owner

srcnalt commented Nov 1, 2019

Thank you for the info. Indeed I was getting quite many warning messages however did not really know the cause. Soon I will refactor with this info.

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

2 participants