I want to go to the next screen after finishing a time consuming calculation. Therefore, I wnat to define at the end of a function the screen that has to be shown. However, when I try the following:
def gotoscreen1(self):
print self.manager (prints None)
self.manager.current = 'screen2'
I have the error = AttributeError: 'NoneType' object has no attribute 'current'
When I try:
def on_enter(self):
print self.manager (prints <__main__.ScreenManagement object at 0x1227C688>)
self.manager.current = 'screen2'
I can jump imediatly to screen 2, but that is not what I want ...
Many thanks in advance for your help!
After a hort night sleep (thank you son) I woke up and knew the solution: I needed to pass the correct arguments when calling the function in the clock schedule
Clock.schedule_interval(partial(self.gotoscreen1, self), 0.5)
Related
#some_instance_var = Concurrent::Hash.new
(0...some.length).each do |idx|
fetch_requests[idx] = Concurrent::Promise.execute do
response = HTTP.get(EXTDATA_URL)
if response.status.success?
... # update #some_instance_var
end
# We're going to disregard GET failures here.
puts "I'm here"
end
end
Concurrent::Promise.all?(fetch_requests).execute.wait # let threads finish gathering all of the unique posts first
puts "how am i out already"
When I run this, the bottom line prints first, so it's not doing what I want of waiting for all the threads in the array to finish its work first, hence I keep getting an empty #some_instance_var to work with below this code. What am I writing wrong?
Never mind, I fixed this. That setup is correct, I just had to use the splat operator * for my fetch_requests array inside the all?().
Concurrent::Promise.all?(*fetch_requests).execute.wait
I guess it wanted multiple args instead of one array.
I'm trying to debug with Pry. Most of the times, whenever I use next command to jump over the next line, pry goes to what it seems another frame. For example, watch my code:
Item.where(dst: "book").each do |book|
ott = book.ott
est = extract_es_title(ott) if ott != nil
web_item = Item.find_by(su: GR_BASE_URL+book.id)
binding.pry
end
class Item
include Mongoid::Document
include Mongoid::Timestamps
field :su, as: :source_url
field :ott, as: :other_titles
field :dst, as: :details_structure
end
If I run the code, it stops at the correct breakpoint. However, if I run next once in Pry console, it goes what it seems deeper in the stack instead of jumping to the next line. Here is the code that Pry shows mw once I run next:
645: def yield_document(document, &block)
646: doc = document.respond_to?(:_id) ?
647: document : Factory.from_db(klass, document, criteria.options[:fields])
648: yield(doc)
=> 649: documents.push(doc) if cacheable?
650: end
Why is this behavior? How can I just jump to the next method line?
Thanks in advance.
Use breakpoints:
break 4 then hit CTRL-D or type continue and it should take you to line 4. Generally "next line" is considered the next line of execution, not the next line of your current file.
From docs: https://github.com/nixme/pry-debugger
If you want to go to the next iteration of the each block. You can just move the binding.pry to the beginning of the loop, and run continue each time.
Item.where(dst: "book").each do |book|
binding.pry
ott = book.ott
est = extract_es_title(ott) if ott != nil
web_item = Item.find_by(su: GR_BASE_URL+book.id)
end
ok, Im almost completely new to lua and computercraft but I have alot of creativity. I'm trying to write code that will reprint a variable every second. here is what I have so far:
display = "Loading..."
While true do
sleep(1)
term.clear()
term.setCursorPos(1,1)
print (display)
end
sleep(3)
display = "hello"
I want to use it to render a 2d game, the "display" variable would change often and thus why i want it to be updated every second.
It does indeed refresh every second when I run the code but for some reason I cant seem to get the "display" variable to change after 3 seconds to test it.
What am I doing wrong?
while true is an infinite loop. The script never reaches sleep(3).
Perhaps you want to replace while true with for i=1,3.
I am not experienced in Lua, but this might be a solution: answer on SO
In the UI thread, run:
while ((status=lua_resume(L_coroutine, 0)) == LUA_YIELD) {
semaphore_wait(); /* whatever the appropriate C# call is */
}
"Wait for response" should look something like:
while not results[my_result] do
coroutine.yield()
end
The "incoming message" function should look like the following in Lua:
results[cur_result]=parsed_message
I am attempting to build my first game using love2D, I have hit a problem.
The game is a bubble popping game, I want to assign a bubble to each letter on the keyboard so that when a letter is pressed, the bubble will pop.
I have an external file called "bubble.lua" which I have tried to make an object "bubble" with.
to do that I have created a table "bubble" in the bubble.lua which contains functions and variables. Now, this file works when called from main.lua using just one bubble, however I am going to need 26 bubbles so I thought it would be best to store each bubble in another table. For the purpose of trying this I just stored one bubble using 1 as the key.This is where I have problems.
require "bubble"
local bubbles = {}
function love.load()
bubbles[1] = bubble.load(100, 100)
end
function love.draw()
for bubble in bubbles do
bubble.draw()
end
end
function love.keypressed(key)
bubbles[key].bubble.pop()
end
Firstly, I know that the for loop in love.draw() does not work, and the line "bubble[key].bubble.pop" seems to return nil as well
The for loop I can probably find the solution myself online, my main problem is the "bubble[key].bubble.pop()" line, I cannot work out what's wrong or how to fix it.
Can anybody help me?
You may want to look at this as well:
bubble.lua
bubble = {}
function bubble.load(posX, posY)
bubble.x = posX
bubble.y = posY
bubble.popped = false
end
function bubble.draw()
if not bubble.popped then
love.graphics.rectangle("line", bubble.x, bubble.y, 37, 37)
else
love.graphics.rectangle("line", bubble.x, bubble.y, 37, 100)
end
end
function bubble.pop()
bubble.popped = true
end
Edit:
Following the advice of the answer below I now have the following error when I press "a":
main.lua:14: attempt to index a nil value
the updated code is below
main.lua
require "bubble"
local bubbles = {}
function love.load()
bubbles["a"] = bubble.load(100, 100)
end
function love.draw()
for key, bubble in pairs(bubbles) do
bubble.draw()
end
end
function love.keypressed(key)
bubbles[key].pop()
end
any thoughts?
There are several issues with this code. First, you index by number when you initialize bubbles (bubbles[1]), but access them using the key as the index (bubbles[key]), which is NOT a number. You need to settle on one mechanism to index the bubbles. Let's say you picked using key as the index (instead of the number).
This loop:
for bubble in bubbles do
bubble.draw()
end
should be written as:
for key, bubble in pairs(bubbles) do
bubble.draw()
end
and instead of bubbles[key].bubble.pop() you can simply do bubbles[key].pop() as bubbles[key] already returns the bubble you can pop.
To initialize, instead of bubbles[1] you need to do bubbles['a'] (or whatever other value is used by key in love.keypressed(key)).
I'm trying to create a while true do loop, that reacts to clicks, using os.pullEvent, and also updates a monitor.
Problem being, it only updates the screen when I press one of the on screen buttons, and I've found out that's because pullEvent stops the script, until an event is fired.
Is it possible to make it so pullEvent doesn't stop me updating the monitor?
function getClick()
event,side,x,y = os.pullEvent("monitor_touch")
button.checkxy(x,y)
end
local tmp = 0;
while true do
button.label(2, 2, "Test "..tmp)
button.screen()
tmp++
getClick()
end
You can easily use the parallel api to run both codes essentially at the same time. How it works is it runs them in sequence until it hits something that uses os.pullEvent and then swaps over and does the other side, and if both stop at something that does os.pullEvent then it keeps swapping between until one yields and continues from there.
local function getClick()
local event,side,x,y = os.pullEvent("monitor_touch")
buttoncheckxy(x,y)
end
local tmp = 0
local function makeButtons()
while true do
button.label(2,2,"Test "..tmp)
button.screen()
tmp++
sleep(0)
end
end
parallel.waitForAny(getClick,makeButtons)
Now if you notice, first thing, I've made your while loop into a function and added a sleep inside it, so that it yields and allows the program to swap. At the end you see parallel.waitForAny() which runs the two functions that are specified and when one of them finishes, which in this case whenever you click on a button, then it ends. Notice however inside the arguments that I'm not calling the functions, I'm just passing them.
I don't have computercraft handy right now or look up the functions but i know that you can use the function os.startTimer(t) that will cause an event in t seconds (I think it is seconds)
usage:
update_rate = 1
local _timer = os.startTimer(update_rate)
while true do
local event = os.pullEvent()
if event == _timer then
--updte_screen()
_timer = os.startTimer(update_rate)
elseif event == --some oter events you want to take action for
--action()
end
end
note: the code is not tested and I didn't use computercraft in quite a while so pleas correct me if i did a mistake.