Cycle between minimized windows while preserving position - awesome-wm

So I have this keybind:
awful.key({modkey}, ";",
function()
if client.focus then
for c in awful.client.iterate(function(x) return x.minimized end) do
c.minimized = false
client.focus.minimized = true
client.focus = c
client.focus:raise()
break
end
end
end
The aim is to swap the focused client with the first minimized client, but not a simple cycle as I want it to be agnostic to how many un-minimized clients there are on screen. It works as intended, except that the positioning of the client on the screen isn't preserved. From what I can tell, if the newly un-minimized & focused client has a higer index than the next highest un-minimized client, then it will always end up positioned after it on the screen, and I have no idea how to get around this.

While it does nothing to clarify the issue I was running into, here's the workaround I came up with to accomplish the goal I had in mind:
if client.focus then
local c = client.focus
local nxt = nil
for x in awful.client.iterate(function(x) return x.minimized end) do
if nxt then nxt:swap(x) else nxt = x end
end
c.minimized = true
nxt.minimized = false
c:swap(nxt)
client.focus = nxt
end
I decided to do away with keeping the clients in their original order, and instead, any x of un-minimized clients will occupy positions 1-x in the clients table, while any minimized clients will form a queue along the remaining positions. Conveniently, spawning a new client gives it focus, makes it a master, and puts it in the first position, so the ordering is maintained.
It works ¯\_(ツ)_/¯

Related

AwesomeWM - open/close window callback

I have following idea:
I have a wallpaper in 2 version. One original and second a bit blurry. I want to change the wallpaper from original when first window/program opens on the screen. Once the last window/program is closed change the wallpaper back.
Also when I change between tags I want to check if any window/program is open or not and then adjust the wallpaper.
How can I do that?
P.S.
I use nitrogen to set wallpaper
AwesomeWM client created/removed callback
I found a way to set the wallpaper depending on the visible clients, but I have no idea how your Nitrogen calls look like. Just replace the naughty.notify lines with your Nitrogen invocations.
Somewhere in your rc.lua, you should find something like this:
awful.screen.connect_for_each_screen(function(s)
Add the following to that function
tag.connect_signal("property::selected", function(t)
if #s.clients > 0 then
naughty.notify({text = "set blurry wallpaper", timeout = 1})
else
naughty.notify({text = "set original wallpaper", timeout = 1})
end
end
)
table.getn is deprecated but this is the solution for older lua versions:
tag.connect_signal("property::selected", function(t)
if table.getn(s.clients) > 0 then
naughty.notify({text = "set blurry wallpaper", timeout = 1})
else
naughty.notify({text = "set original wallpaper", timeout = 1})
end
end
)

How to use recursive function in Lua to dynamically build a table without overwriting it each call

I have a need to crawl a set of data of indeterminate size and build a table key/value index of it. Since I don't know the dimensions in advance, seems I have to use a recursive function. My Lua skills are very new and superficial. I'm having difficulty understanding how to deal with returning a table from the function call.
Note this is for a Lua 5.1 script processor
API = function(tbl)
local table_api = {}
-- do stuff here with target data and add to table_api
table_api["key"] = value
-- then later there is a need to recurse deeper into target data
table_api["lower"] = API(var)
return table_api
end
result = API(source_data)
In most every other language I know there would be some way to make the recurse line table_api["lower"] = API(var) work but since Lua does table variables by reference, my return sub-table just keeps getting overwritten and my result is a tiny last bit of what it should be.
Just for background on my purpose: there's a commercial application I'm working with that has a weakly documented Lua scripting interface. It's running Lua 5.1 and the API is not well documented and frequently updated. As I understand it, everything is stored in _G, so I wanted to write something to reverse engineer the API. I have a working recursive function (not shown here) that enumerates all of _G. For that return value, it just builds up an annotated string and progressively builds on the string. That all works fine and is really useful, but it shows much more that the API interface; all the actual data elements are included, so I have to sift through like 30,000 records to determine an API set of about 500 terms. In order to determine the API, I am trying to use this sub-table return value recursive function being discussed in this question. The code I'm showing here is just a small distilled subset of the larger function.
I'll go ahead and include the full code here. I was hoping to incrementally build a large'ish table of each API level, any sublevels, and finally whatever keys used at the lowest level.
In the end, I was expecting to have a table that I could address like this:
result["api"]["label"]["api"]["sublabel"]["value"]["valuename"]
Full code:
tableAPIShow = function(tbl, table_track)
table_track = table_track or {}
local table_api = {}
if type(tbl) == 'table' then
-- Check if values are tables.
local parent_table_flag = true
for ind,val in pairs(tbl) do
if type(val) ~= 'table' then
parent_table_flag = false
break
end
end
-- If all children are table type, check each of them for subordinate commonality
local api_flag = false
if parent_table_flag == true then
local child_table = {}
local child_table_flag = false
api_flag = true
for ind,val in pairs(tbl) do
-- For each child table, store the names of the indexes.
for sub_ind,sub_val in pairs(val) do
if child_table_flag == false then -- First time though, create starting template view of typical child table.
child_table[sub_ind] = true -- Store the indexes as a template table.
elseif child_table[sub_ind] == nil then -- Otherwise, test this child table compared to the reference template.
api_flag = false
break
end
end
if api_flag == false then -- need to break out of nested loop
break
end
child_table_flag = true
end
end
if api_flag == true then
-- If everything gets to here, then this level is an API with matching child tables below.
for ind,val in pairs(tbl) do
if table_api["api"] == nil then
table_api["api"] = {}
end
table_api["api"][ind] = tableAPIShow(val, table_track)
end
else
-- This level is not an API level, determine how to process otherwise.
for ind,val in pairs(tbl) do
if type(val) == 'table' then
if table_track[val] ~= nil then -- Have we already recursed this table?
else
table_track[val] = true
if table_api["table"] == nil then
table_api["table"] = {}
end
table_api["table"][ind] = tableAPIShow(val, table_track)
end
else -- The children are not tables, they are values
if table_api["value"] == nil then
table_api["value"] = {}
end
table_api["value"][ind] = val
end
end
end
else
-- It's not a table, just return it.
-- Probably never use this portion because it's caught on upper level recurse and not called
return tbl
end
return table_api
end
And I was calling this function in the main script like this:
local str = tableAPIShow(_G)
I've got another function that recursively shows a table so I can look inside my results and see I only get a return value that contains only the values of the top-level of _G (I have it excluded built-in Lua functions/values because I'm only interested in the Application API):
{
[value] = table: 00000000F22CB700 {
[value][_VERSION] = Application/5.8.1 (x86_64; Windows NT 10.0.16299),
[value][tableAPIShow] = "function: 00000000F22C6DE0, defined in (121-231) C:\\Users\\user\\AppData\\Local\\Temp\\APP\\/~mis00002690 ",
[value][_FINAL_VERSION] = true,
[value][Path] = ./Scripts/Database/elements/,
[value][class] = "function: 00000000F1953C40, defined in (68-81) Scripts/Common/Class.lua ",
[value][db_path] = ./Scripts/Database/,
[value][merge_all_units] = "function: 00000000F20D20C8, defined in (2242-2250) Scripts/Database/db_merge.lua ",
}
You just need to localize the variable you store your table in and it will work as you expect:
local table_api = {}
(note that you are passing table variable that conflicts with the global table variable and is not currently used in the function.)
I am inclined to believe that your tableAPIShow function code is working correctly and the
another function that recursively shows a table
fails to serialize your table fully. Hence you don't see the deeper levels of the table returned by tableAPIShow().
I got your initial and current code (tableAPIShow) to work with my simple table serialize function: the whole _Global table is exported completely and formatted as you implemented it in your tableAPIShow().
Code for testing:
apiMassiveTable = {
api = {
get = {
profile = {"profileID", "format"},
name = {"profileID", "encoding"},
number = {"profileID", "binary"}
},
set = {
name = {"apikey", "profileID", "encoding", "newname"},
number = {"apikey", "profileID", "binary", "newnumber"}
},
retrieve = {}
},
metadata = {version="1.4.2", build="nightly"}
}
-- tableAPIShow implemenation here
table.serialize = dofile("serialize.lua")
print(table.serialize("myNameForAHugeTable", tableAPIShow(_G)))
PS: Whatever serialize function you're using, it should enquote strings like Application/5.8.1 (x86_64; Windows NT 10.0.16299) which it does not.
Like #Paul Kulchenko said, you need to learn to use locals (https://www.lua.org/pil/4.2.html). Global variable post-exist until a new lua_State is loaded (a new environment, could be a new process depending on what interpreter you are using). So a tip is to always use local variables for anything you don't want to leave the function or leave the compilation unit.
Think of tables like dictionaries: a word is attached to a definition. Thusly the definition is the data.
What I think you are trying to do is serialization of a table of data. However, that isn't really necessary. You can either shadow copy or deep copy the given table. A shadow copy is when you don't delve into the depths of tables found in the keys, etc. A deep copy is when you copy tables in keys of tables in keys of tables... etc.
local shallow_copy = function(tab)
local rep_tab = {}
for index, value in pairs(tab)do
rep_tab[index] = value
end
return rep_tab
end
-- because local variable is not defined or declared immediately on a 1-liner,
-- a declaration has to exist so that deep_copy can be used
-- lets metatable execute functions
local deep_copy
deep_copy = function(tab)
local rep_tab = {}
for index, value in pairs(tab)do
if(type(value) == "table")then
rep_tab[index] = deep_copy(value)
else
rep_tab[index] = value
end
end
return rep_tab
end
Deco's deepcopy.lua
https://gist.github.com/Deco/3985043
You can also index tables using periods:
local tab = {}
tab.abc = 123
tab["def"] = 456
print(tab.abc, tab["def"])
To serialize the entire _G, you would just filter out the junk you don't need and recurse each table encountered. Watch out for _G, package, and _ENV because if defined it will recurse back to the start.
-- use cap as a whitelist
enumerate_dir = function(dir, cap)
local base = {}
for i, v in pairs(dir) do
-- skip trouble
if(i ~= "_G" and i ~= "_ENV" and i ~= "package")then
if(type(v) == "table")then -- if we have a table
base[i] = enumerate_dir(v, cap)
else
for k, n in pairs(cap) do
if(type(v) == n)then -- if whitelisted
base[i] = tostring(v)
end
end
end
end
end
return base
end
-- only functions and tables from _G please
local enumeration = enumerate_dir(_G, {"function", "table"})
-- do some cool quips to get a basic tree system
prefix = ""
recursive_print = function(dir)
for i, v in pairs(dir)do
if(type(v) == "table")then
print(prefix, i, v)
prefix = prefix .. ">"
recursive_print(v)
else
print(prefix, i, v)
end
end
if(#prefix > 0)then
prefix = prefix:sub(1, #prefix-1)
end
end
recursive_print(test)

How to solve a tkinter memory leak?

I have a dynamic table with a fixed row number (like a FIFO Queue), which updates continuously through tkinter's after() function. Inside the table is a Button, which text is editable.
To make the Button's text editable I used the solution of BrenBarn and reference a loop variable into a function call at the command-attribute.
When the function update_content_items() is cycled, I found, that the memory usage is increasing MB by MB per second. I can confirm that after commenting out the lambda expression, the memory leak was gone. (as seen live running 'top' in the terminal)
It seems I have to use the lambda, otherwise the Button will have a wrong index and the user edits the wrong row, when I just used self.list_items[i], though the user clicked the right one.
Is there a way to solve the problem? How can the user click the right button and edit it while having the right index and getting rid of the leak?
The corresponding code:
def update_content_items(self):
"""
Continuously fills and updates the Table with rows and content.
The size of the table rows is initially fixed by an external value at config.ini
:return: nothing
"""
if len(self.list_items) > self.queueMaxlen:
self.queueMaxlen = len(self.list_items)
self.build_table()
try:
for i in range(len(self.list_items)):
item = self.list_items[i]
self.barcodeImgList[i].image = item.plateimage
orig_image = Image.open(io.BytesIO(item.plateimage))
ein_image = ImageTk.PhotoImage(orig_image)
self.barcodeImgList[i].configure(image=ein_image)
# keeps a reference, because somehow tkinter forgets it...??? Bug of my implementation???
self.barcodeImgList[i].image = ein_image
orig_image = None
ein_image = None
#FIXME Memory LEAK?
self.numberList[i].configure(text=item.number,
command=lambda K=i: self.edit_barcode(self.list_items[K]))
self.timestampList[i].configure(text=item.timestamp)
self.search_hitlist[i].config(bg='white', cursor="xterm")
self.search_hitlist[i].unbind("<Button-1>")
if item.queryresult is not None:
if item.queryresult.gesamtstatus != 'Gruen':
self.search_hitlist[i].insert(tk.END, item.queryresult.barcode +
'\n' + item.queryresult.permitlevel)
self.search_hitlist[i].configure(bg='red', cursor="hand2")
self.search_hitlist[i].bind("<Button-1>", item.url_callback)
else:
self.search_hitlist[i].configure(bg='green', cursor="xterm")
self.search_hitlist[i].configure(state=tk.DISABLED)
self.on_frame_configure(None)
self.canvas.after(10, self.update_content_items)
except IndexError as ie:
for number, thing in enumerate(self.list_items):
print(number, thing)
raise ie
def edit_barcode(self, item=None):
"""
Opens the number plate edit dialogue and updates the corresponding list item.
:param item: as Hit DAO
:return: nothing
"""
if item is not None:
new_item_number = EditBarcodeEntry(self.master.master, item)
if new_item_number.mynumber != 0:
item.number = new_item_number.mynumber
self.list_items.request_work(item, 'update')
self.list_items.edit_hititem_by_id(item)
self.parent.master.queryQueue.put(item)
else:
print("You shouldn't get here at all. Please see edit_barcode function.")
EDIT: It seems there is indeed a deeper memory leak (python itself). The images won't get garbage collected. Memory is slowly leaking in Python 3.x and I do use PIL. Also here: Image loading by file name memory leak is not properly fixed
What can I do, because I have to cycle through a list with records and update Labels with images? Is there a workaround? PhotoImage has no explicit close() function, and if I call del, the reference is gc'ed and no configuring of the Label possible.
an example of my proposed changes, with indentation fixed:
def update_content_items(self):
"""
Continuously fills and updates the Table with rows and content.
The size of the table rows is initially fixed by an external value at config.ini
:return: nothing
"""
if len(self.list_items) > self.queueMaxlen:
self.queueMaxlen = len(self.list_items)
self.build_table()
try:
for i in range(len(self.list_items)):
item = self.list_items[i]
self.barcodeImgList[i].image = item.plateimage
orig_image = Image.open(io.BytesIO(item.plateimage))
ein_image = ImageTk.PhotoImage(orig_image)
self.barcodeImgList[i].configure(image=ein_image)
# keeps a reference, because somehow tkinter forgets it...??? Bug of my implementation???
self.barcodeImgList[i].image = ein_image
orig_image = None
ein_image = None
self.numberList[i].configure(text=item.number) # removed lambda
self.numberList[i].bind("<Button-1>", self.edit_barcode_binding) # added binding
self.timestampList[i].configure(text=item.timestamp)
self.search_hitlist[i].config(bg='white', cursor="xterm")
self.search_hitlist[i].unbind("<Button-1>")
if item.queryresult is not None:
if item.queryresult.gesamtstatus != 'Gruen':
self.search_hitlist[i].insert(tk.END, item.queryresult.barcode +
'\n' + item.queryresult.permitlevel)
self.search_hitlist[i].configure(bg='red', cursor="hand2")
self.search_hitlist[i].bind("<Button-1>", item.url_callback)
else:
self.search_hitlist[i].configure(bg='green', cursor="xterm")
self.search_hitlist[i].configure(state=tk.DISABLED)
self.on_frame_configure(None)
self.canvas.after(10, self.update_content_items)
except IndexError as ie:
for number, thing in enumerate(self.list_items):
print(number, thing)
raise ie
def edit_barcode_binding(self, event): # new wrapper for binding
K = self.numberList.index(event.widget) # get index from list
self.edit_barcode(self.list_items[K]) # call the original function
def edit_barcode(self, item=None):
"""
Opens the number plate edit dialogue and updates the corresponding list item.
:param item: as Hit DAO
:return: nothing
"""
if item is not None:
new_item_number = EditBarcodeEntry(self.master.master, item)
if new_item_number.mynumber != 0:
item.number = new_item_number.mynumber
self.list_items.request_work(item, 'update')
self.list_items.edit_hititem_by_id(item)
self.parent.master.queryQueue.put(item)
else:
print("You shouldn't get here at all. Please see edit_barcode function.")

vcat StackOverflowError in julia

I have written a function to detect objects in arrays. In this case I use it to clear white objects, which touch the border of an image. But the problem is, that there is always a StackOverFlowError in vcat and it occurs at different lines (depends on image). Here is a minimum working example. It works fine with the second image, but not with the first:
using Images
function getImChars(I::Array)
charAr=copy(I);
indexMax=find(x->(x==1),charAr);
(h,w)=size(charAr);
# check border values
(r,c)=ind2sub(size(charAr),indexMax);
indexBorderR1=find(x->(x==1),r);
indexBorderR2=find(x->(x==h),r);
indexBorderR=[indexBorderR1;indexBorderR2];
indexBorderC1=find(x->(x==1),c);
indexBorderC2=find(x->(x==w),c);
indexBorderC=[indexBorderC1;indexBorderC2];
borderPixels=[999;999]; # initialize def value FIX
for bRc=1:length(indexBorderR)
borderPixels=[borderPixels [r[indexBorderR[bRc]];c[indexBorderR[bRc]]]];
end
for bCc=1:length(indexBorderC)
borderPixels=[borderPixels [r[indexBorderC[bCc]];c[indexBorderC[bCc]]]];
end
borderPixels=borderPixels[:,2:end];
(rbP,cbP)=size(borderPixels);
fcharAr=[];
for k=1:cbP
bP=[borderPixels[:,k];false;false;false;false];
locObj1=[];
locObj2=[];
locObj3=[];
locObj4=[];
locObj5=[];
locObj6=[];
if(charAr[bP[1],bP[2]]==0)
continue;
else
charAr[bP[1],bP[2]]=0;
end
recGetCharClearBorders(true,false,h,w,bP,locObj1,locObj2,locObj3,locObj4,locObj5,locObj6,charAr);
end
return charAr;
end
function recGetCharClearBorders(firstFlag::Bool,doNotAdd::Bool,h::Int,w::Int,hP::Array,locObj1::Array,locObj2::Array,locObj3::Array,locObj4::Array,locObj5::Array,locObj6::Array,imAr::Array)
leftPoint=[hP[1];hP[2]-1;0;0;0;0];
rightPoint=[hP[1];hP[2]+1;0;0;0;0];
topPoint=[hP[1]-1;hP[2];0;0;0;0];
bottomPoint=[hP[1]+1;hP[2];0;0;0;0];
# check if it is not out of bounds and relative directions
if(topPoint[1]!=0)
if(imAr[topPoint[1],topPoint[2]]==1)
hP[4]=1;
end
end
if(bottomPoint[1]!=(h+1))
if(imAr[bottomPoint[1],bottomPoint[2]]==1)
hP[6]=1;
end
end
if(leftPoint[2]!=0)
if(imAr[leftPoint[1],leftPoint[2]]==1)
hP[3]=1;
end
end
if(rightPoint[2]!=(w+1))
if(imAr[rightPoint[1],rightPoint[2]]==1)
hP[5]=1;
end
end
# add first elements
if(firstFlag)
locObj1=collect(hP[1]);
locObj2=collect(hP[2]);
locObj3=collect(hP[3]);
locObj4=collect(hP[4]);
locObj5=collect(hP[5]);
locObj6=collect(hP[6]);
firstFlag=false;
else
# if first element of locObj was deleted actual point should not get pushed to array
if(!doNotAdd)
push!(locObj1,hP[1]);
push!(locObj2,hP[2]);
push!(locObj3,hP[3]);
push!(locObj4,hP[4]);
push!(locObj5,hP[5]);
push!(locObj6,hP[6]);
imAr[hP[1],hP[2]]=0;
end
end
goL=false;
goT=false;
goR=false;
goB=false;
doNotAdd=false;
if(length(locObj1)!=0)
# always take and check first elements of locObj
hPfInLoc=[locObj1[1],locObj2[1],locObj3[1],locObj4[1],locObj5[1],locObj6[1]];
hPl=[hPfInLoc[1];hPfInLoc[2]-1;0;0;0;0];
hPt=[hPfInLoc[1]-1;hPfInLoc[2];0;0;0;0];
hPr=[hPfInLoc[1];hPfInLoc[2]+1;0;0;0;0];
hPb=[hPfInLoc[1]+1;hPfInLoc[2];0;0;0;0];
compL=false;
compT=false;
compR=false;
compB=false;
# check bounds and if array values have changed
if(hPt[1]!=0)
if(imAr[hPt[1],hPt[2]]!=0)
compT=true;
end
end
if(hPb[1]!=(h+1))
if(imAr[hPb[1],hPb[2]]!=0)
compB=true;
end
end
if(hPl[2]!=0)
if(imAr[hPl[1],hPl[2]]!=0)
compL=true;
end
end
if(hPr[2]!=(w+1))
if(imAr[hPr[1],hPr[2]]!=0)
compR=true;
end
end
# define directions and set defined direction false in locObj
if((locObj3[1]==1)& compL)
locObj3[1]=0;
goL=true;
elseif((locObj4[1]==1)& compT)
locObj4[1]=0;
goT=true;
elseif((locObj5[1]==1)& compR)
locObj5[1]=0;
goR=true;
elseif((locObj6[1]==1)& compB)
locObj6[1]=0;
goB=true;
else
if (length(locObj1)==1)
locObj=[];
else # if everything is zero delete first rows of arrays
deleteat!(locObj1,1);
deleteat!(locObj2,1);
deleteat!(locObj3,1);
deleteat!(locObj4,1);
deleteat!(locObj5,1);
deleteat!(locObj6,1);
doNotAdd=true;
return recGetCharClearBorders(firstFlag,doNotAdd,h,w,hP,locObj1,locObj2,locObj3,locObj4,locObj5,locObj6,imAr);
end
end
end
#execute choosen direction
if(goL)
return recGetCharClearBorders(firstFlag,doNotAdd,h,w,hPl,locObj1,locObj2,locObj3,locObj4,locObj5,locObj6,imAr);
end
if(goT)
return recGetCharClearBorders(firstFlag,doNotAdd,h,w,hPt,locObj1,locObj2,locObj3,locObj4,locObj5,locObj6,imAr);
end
if(goR)
return recGetCharClearBorders(firstFlag,doNotAdd,h,w,hPr,locObj1,locObj2,locObj3,locObj4,locObj5,locObj6,imAr);
end
if(goB)
return recGetCharClearBorders(firstFlag,doNotAdd,h,w,hPb,locObj1,locObj2,locObj3,locObj4,locObj5,locObj6,imAr);
end
end
# execute test procedure
Im=Images.load("Test.png");
Im=data(Im);
imAr=map(Float64,Im);
resIm=getImChars(imAr);
save("Imout.png",resIm);
You only have to change the name of the image in my code. Just tell me, if you need some more information. Thanks a lot.
Cheers, clax
Unlike many functional programming language, Julia doesn't optimize for tail recursion (reusing the stack frame when a function ends by a call). So in your case it is very likely that your recursive function hits the max stack depths depending on your OS's thread stack size.
Unfortunately, the Julia core team decide not to prioritise Tail call optimization as they don't deem such feature a must. Indeed people can often refactor their code into loops.
Afaik, tail call optimization is pretty difficult and many other programming languages featuring functional programming paradigm also lack it.
Took a few minutes needed to run and fixup the tail recursion. Is the following replacement for recGetCharClearBorders performing correctly:
function recGetCharClearBorders(firstFlag::Bool,doNotAdd::Bool,h::Int,w::Int,hP::Array,locObj1::Array,locObj2::Array,locObj3::Array,locObj4::Array,locObj5::Array,locObj6::Array,imAr::Array,recdepth,recloc)
while true
leftPoint=[hP[1];hP[2]-1;0;0;0;0];
rightPoint=[hP[1];hP[2]+1;0;0;0;0];
topPoint=[hP[1]-1;hP[2];0;0;0;0];
bottomPoint=[hP[1]+1;hP[2];0;0;0;0];
# check if it is not out of bounds and relative directions
if(topPoint[1]!=0)
if(imAr[topPoint[1],topPoint[2]]==1)
hP[4]=1;
end
end
if(bottomPoint[1]!=(h+1))
if(imAr[bottomPoint[1],bottomPoint[2]]==1)
hP[6]=1;
end
end
if(leftPoint[2]!=0)
if(imAr[leftPoint[1],leftPoint[2]]==1)
hP[3]=1;
end
end
if(rightPoint[2]!=(w+1))
if(imAr[rightPoint[1],rightPoint[2]]==1)
hP[5]=1;
end
end
# add first elements
if(firstFlag)
locObj1=collect(hP[1]);
locObj2=collect(hP[2]);
locObj3=collect(hP[3]);
locObj4=collect(hP[4]);
locObj5=collect(hP[5]);
locObj6=collect(hP[6]);
firstFlag=false;
else
# if first element of locObj was deleted actual point should not get pushed to array
if(!doNotAdd)
push!(locObj1,hP[1]);
push!(locObj2,hP[2]);
push!(locObj3,hP[3]);
push!(locObj4,hP[4]);
push!(locObj5,hP[5]);
push!(locObj6,hP[6]);
imAr[hP[1],hP[2]]=0;
end
end
goL=false;
goT=false;
goR=false;
goB=false;
doNotAdd=false;
if(length(locObj1)!=0)
# always take and check first elements of locObj
hPfInLoc=[locObj1[1],locObj2[1],locObj3[1],locObj4[1],locObj5[1],locObj6[1]];
hPl=[hPfInLoc[1];hPfInLoc[2]-1;0;0;0;0];
hPt=[hPfInLoc[1]-1;hPfInLoc[2];0;0;0;0];
hPr=[hPfInLoc[1];hPfInLoc[2]+1;0;0;0;0];
hPb=[hPfInLoc[1]+1;hPfInLoc[2];0;0;0;0];
compL=false;
compT=false;
compR=false;
compB=false;
# check bounds and if array values have changed
if(hPt[1]!=0)
if(imAr[hPt[1],hPt[2]]!=0)
compT=true;
end
end
if(hPb[1]!=(h+1))
if(imAr[hPb[1],hPb[2]]!=0)
compB=true;
end
end
if(hPl[2]!=0)
if(imAr[hPl[1],hPl[2]]!=0)
compL=true;
end
end
if(hPr[2]!=(w+1))
if(imAr[hPr[1],hPr[2]]!=0)
compR=true;
end
end
# define directions and set defined direction false in locObj
if((locObj3[1]==1)& compL)
locObj3[1]=0;
goL=true;
elseif((locObj4[1]==1)& compT)
locObj4[1]=0;
goT=true;
elseif((locObj5[1]==1)& compR)
locObj5[1]=0;
goR=true;
elseif((locObj6[1]==1)& compB)
locObj6[1]=0;
goB=true;
else
if (length(locObj1)==1)
locObj=[];
else # if everything is zero delete first rows of arrays
deleteat!(locObj1,1);
deleteat!(locObj2,1);
deleteat!(locObj3,1);
deleteat!(locObj4,1);
deleteat!(locObj5,1);
deleteat!(locObj6,1);
doNotAdd=true;
continue
end
end
end
#execute choosen direction
if(goL)
hP = hPl
continue
end
if(goT)
hP = hPt
continue
end
if(goR)
hP = hPr
continue
end
if(goB)
hP = hPb
continue
end
break
end
end
Somehow the output image turned on its side in my run.

How to tell if an object has been touch by the player in gml code

Hey am going to give an example of what am trying to do imagine that i have 5 circle sprites and in my gml code i want to do something like this if cirlce_1 was touch then you can touch circle_2 and if circle_2 was touch then you can touch cirlce_3. Please who can help me with this, willing to give a reward via paypal.
Touch events in Game Maker are treated as mouse events. If you want the circles to only allow the player to touch them in order, you can assign each one to have a number and make them all the same object. Take a look at this:
Script to create circles
counter = 0;
lastball = 0;
for(i = 0; i < 10; i++){//Make that third part "i += 1" if using a version before Studio
c = instance_create(floor(random(room_width)), floor(random(room_height)), objCircle);
lastball++;
c.myNum = lastball;
c.radius = 16;//Or whatever radius you want
};
The for statement here automatically generates circles around the room, but if you want manual control, try this:
newCircle()
c = instance_create(argument0, argument1, objCircle);
c.myNum = lastball;
c.radius = 16;
lastball++;
This will create a new circle wherever you want and will automatically increment lastball as well every time it's called. For instance, you could say newCircle(16, 27);.
In the step code for objCircle
if(mouse_check_button_pressed(mb_left) && point_distance(x, y, mouse_x, mouse_y) < radius && counter == myNum){
counter++;//Or counter += 1 in versions before Studio
//Insert whatever circles do when clicked here
};
The circles can be made to do anything when clicked. Since they're all the same object, perhaps you could use a switch statement so each one does something different depending on its number.
Let me know if there's anything else I can help with.

Resources