Drawing an image loaded from other files (love2d) - graphics

So I have a "load_assets.lua" file inside my game folder along with the "main.lua". Inside the "load_assets.lua" file I have a "love.graphics.newImage(image)"
and in the "main.lua" file I have the "love.graphics.draw(image)".
I've tried to create this function inside the "main.lua" file:
function love.draw(i)
love.graphics.draw(i)
end
and this function inside the "load_assets.lua" file:
image = love.graphics.newImage(image)
lovedraw(image)
but it still doesn't seem to work.
Of course, the code is a little more complex than this, but similar:
--in "main.lua" the function is actually:
function title(lvl0)
love.graphics.draw(lvl0)
end
--and in "load_assets" it's:
function love.load()
lvl0 = love.graphics.newImage("lvl0.png")
end
title(lvl0)
When I run the code I get this error:
Error
title.lua:3: bad argument #1 to 'draw' (Drawable expected, got nil)
Traceback
[C]: in function 'draw'
title.lua:3: in function 'title'
main.lua:16: in main chunk
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

I don't know the specifics of your code, but the following worked for me:
load_assets.lua
function love.load()
lv10 = love.graphics.newImage("image.png")
end
main.lua
require("load_assets")
function title(lv10)
love.graphics.draw(lv10)
end
function love.draw()
title(lv10)
end
Love2d has several built in functions that you can override to create your program. Ideally, all your function calls happen from within these programs.
So, in load_assets.lua I override the love.load() function to create lv10. By default love.load() is called exactly once, at the start of the program.
In main.lua I define the title() function, then override love.draw() to call title(). By default, love.draw() is called every update cycle of the love engine (every frame).

Related

why does my depth first search continue to work even after finding the target?

I am building a depth first search function which is used on an undirected adjacency list. This function is meant to put all paths that successfully find the target node into the viable functions list and the non-viable paths into culdesac.
adjacencylist={1:[2,3],2:[1,4],3:[1,5],4:[2],5:[3]}
visited=[]
culdesacs=[]
viablepaths=[]
def dfs(graph,node,target,path,output):
if node in visited:
return
visited.append(node)
print(path)
if node==target:
output.append(path)
viablepaths.append(output)
return print('hello')
for nx in graph[node]:
dfs(graph,nx,5,path+[nx],output)
for element in path:
if element not in culdesacs:
culdesacs.append(element)
print(path)
print(culdesacs)
return print('oops')
dfs(adjacencylist,1,5,[1],[])
What I don't understand is why my function continues to work even after successfully hitting the target and even triggering the base case of my recursive program. For example in the function call dfs(adjacencylist,1,5,[1],[]), my starting node is 1 and my target node is 5. My function successfully found the path [1,3,5] and returned "hello". But then it continues to print oops and printing the paths [1,3] and [1].
Ideally, once it found the path[1,3,5], i would just like it to end the function altogether
How can I fix this
The problem is the following: When you're in
for nx in graph[node]:
dfs(graph,nx,5,path+[nx],output)
you start the recursive call that finds the target.
However, you do not return e.g. true in
if node==target:
output.append(path)
viablepaths.append(output)
return print('hello')
an do not check the return value.
I would recommend reading up on the beahvior of the call stack. Just because you end the recursive call that finds the value, the others are not cancelled

Python Function and closure

def html_tag(tag):
def wrap_text(msg):
print(f'{tag} {msg} {tag}')
return wrap_text
html_tag('Hi')
print(html_tag)
check_h1 = html_tag('Hi')
print(check_h1)
CONSOLE RETURNS
<function html_tag at 0x108492b00>
<function html_tag.<locals>.wrap_text at 0x1085b0280>
SO I KNOW THAT FIRST LINE OF CONSOLE FUNCTION WAS STORED IN COMPUTER'S MEMORY, BUT WHAT HAPPENED TO THAT 'Hi' ARGUMENT IN html_tag FUNCTION.
ALSO IN 2ND LINE OF CONSOLE I GUESS 'Hi' WAS STORED AS ARGUMENT OF tag PARAMETER, BUT WHY THAT DID NOT OCCUR WITH html_tag('Hi')?
The difference is that once you are referencing the wrap_text function and once the html_tag function.
With the use of
html_tag('Hi')
you do call the function and receive the wrap_text functon, you do however never store the returend result. if you change
html_tag('Hi')
print(html_tag)
to
my_html_tag = html_tag('Hi')
print(my_html_tag)
you would get the expected result.
When you simply call
print(html_tag)
you tell python to print the html_tag function, which is not influenced by you calling it earlier via
html_tag('Hi')

Why is function changing list values in python

i tried calling .copy on it and then passing it in the function. that didn't work.
when i tried coppying in the function itself it still changed the original list.
the function is in another file
main.py
win_condition.check_r_win(board)
win_condition.py
def check_r_win(board):
board = _board.copy()
for col in board:
while(len(col) <= ROWS):
col.append("-")
I don't really get what you are trying here. Python's list is a mutable object type. If you pass a object reference of a list to a function and change the list within this function, it also gets changed outside of the function scope.

Elixir - Define functions in module by metaprogramming

I have three identical methods in my module, that do (almost) exactly the same thing. Instead of repeating the function definitions, I am trying to define them once to keep code minimal and same for all of them.
So far I've tried using Code.eval_string:
defmodule MyModule do
Enum.each ~w(method1 method2 method3), fn method ->
#method method
Code.eval_string """
def #{#method}(map) when is_map(map) do
do_map_stuff(:#{#method}, map)
end
def #{#method}(arg) do
do_other_stuff(:#{#method}, arg)
end
"""
end
## Other Methods
end
but this throws ArgumentError:
Compiling 1 file (.ex)
** (ArgumentError) cannot invoke def/2 outside module
(elixir) lib/kernel.ex:4297: Kernel.assert_module_scope/3
(elixir) lib/kernel.ex:3299: Kernel.define/4
(elixir) expanding macro: Kernel.def/2
nofile:1: (file)
I think quote/unquote might be the way to go, but I'm not exactly sure how to do this using them (I've already read the Meta Guide on the Elixir website).
Something like this?
defmodule MyModule do
def do_map_stuff(method, arg) do
IO.inspect([method, arg])
end
Enum.each [:method1, :method2, :method3], fn method ->
def unquote(method)(map) when is_map(map) do
do_map_stuff(unquote(method), map)
end
def unquote(method)(arg) do
do_map_stuff(unquote(method), arg)
end
end
end

Calling a function whos name is stored in a string

I have a function e.g.
function run()
print("hi")
end
I have a string called message which contains the word "run":
activity = message
print(activity) --returns run
However I am unable to use the string activity to run the function run(). I have tried:
func = loadstring(activity.."()")
func() -- I get an error on line 1 saying: attempt to call nil
Well, the function run is stored in _G so the simple answer:
_G[message]()
function run() print("hi") end
activity = "run"
loadstring(activity.."()")()
This works fine as long as loadstring is running in the same environment as run. In other words, the loadstring line is equivalent to _G[activity](). If the global run was created in some other environment, that'll evaluate to nil.
Given that Lua functions are first class values, just like strings, if at all possible it would be better to do this:
function run() print("hi") end
activity = run
activity()
Though if this is in a context where you're getting a string value from config file or something, that wouldn't work.

Resources