How I can set global variable inside function lua - iphone

webView = nil
func()
function func()
webView = native.newWebView( 0, 0, display.contentWidth, display.contentHeight )
webView:request( "https://api.twitter.com/oauth/authenticate?oauth_token="..twitter_request_token )
webView:addEventListener( "urlRequest", webListener )
end
print(webView) -- webView = nil
How I can get webView outside func, which I change inside func. Help ne, please

There is (almost) nothing wrong with your example. You need to make sure to call func() after you define it, but other than that it should work. For example, this outputs something for me:
webView = nil
function func()
webView = "something"
end
func()
print(webView)
Yes, you can use explicit reference _G.webView inside the function, but there is no need to do that in your case.
If you still have an issue, it's probably because your func() is not executed when you think it is. Try to print the value of webView after it's assigned inside the function.

You can just append _G in front of your variable in will work as global variable.
Everywhere you have to use your variable with _G.
For Ex:
_G.yourVaribalename=display.newText();

You dont' need to declare variables, they are automatically global unless specified as local. And you can't call func() before func is defined. So try this:
function func()
webView = native.newWebView( 0, 0, display.contentWidth, display.contentHeight )
webView:request( "https://api.twitter.com/oauth/authenticate?oauth_token="..twitter_request_token )
webView:addEventListener( "urlRequest", webListener )
end
func()
print(webView) -- webView will not be nil
In the above, webView is global so it will exist globally as soon as created within func(), and will continue to exist after func() returns.

Related

Call a PostgreSQL function and get result back with no loop

I have a simple rust program that interacts with a PostgreSQL database.
The actual code is:
for row in &db_client.query("select magic_value from priv.magic_value();", &[]).unwrap()
{
magic_value = row.get("magic_value");
println!("Magic value is = {}", magic_value);
}
And.. it works. But I don't like it: I know this function will return one and only one value.
From the example I found, for example here: https://docs.rs/postgres/latest/postgres/index.html
and here: https://tms-dev-blog.com/postgresql-database-with-rust-how-to/
You always have a recordset to loop on.
Which is the clean way to call a function without looping?
query returns a Result<Vec<Row>, _>. You are already unwrapping the Vec, so you can just use it directly instead of looping. By turning the Vec into an owning iterator yourself, you can even easily obtain a Row instead of a &Row.
magic_value = db_client.query("select magic_value from priv.magic_value();", &[])
.unwrap() // -> Vec<Row>
.into_iter() // -> impl Iterator<Item=Row>
.next() // -> Option<Row>
.unwrap() // -> Row
.get("magic_value");

How do I assign a variable to a thing in roblox?

There is a variable called f, And this code is supposed to play random audio whenever it's clicked
local clickDetector = script.Parent.ClickDetector
f = math.random(1,3)
function onMouseClick()
script.Parent.f.TimePosition = 0
script.Parent.f.Playing = true
end
clickDetector.MouseClick:connect(onMouseClick)
How do I make it so it will thing it's not inside
assuming the parent exist, try
script.Parent[f]

How to use functions from another class in main.lua?

I am trying to use functions of another class in my main.lua. I wrote some code according to my research but it's not working properly. Can you help me out? Thanks.
fish.lua code:
function class()
local cls = {}
cls.__index = cls
return setmetatable(cls, {__call = function (c, ...)
instance = setmetatable({}, cls)
if cls.__init then
cls.__init(instance, ...)
end
return instance
end})
end
Fish= class()
function Fish:listen(event)
if phase =="began" then
print("hi")
end
end
function Fish:__init(image)
self.image=display.newImage(image,30,30)
self.image: addEventListener("touch",self.listen)
end
main.lua code:
require "fish"
originalImage="fish.small.red.png"
differentImage="fish.small.blue.png"
local fishImage=Fish(originalImage)
It displays the image but does not work(prints "hi" ) when it is touched.
A couple of problems:
Change function Fish:listen(event) to function Fish.listen(event)
and if phase =="began" then should be if event.phase =="began" then

Is main form an fsMDIForm

I need a way to figure out if my application's mainform is an fsMDIForm.
How can this be done?
You can test Application.MainForm.FormStyle.
Assuming you want to know for the main form of another process (other then the calling application), then if you have the handle of that main form, use:
MDIActive := FindWindowEx(MainFormHandle, 0, PAnsiChar('MDICLIENT'), nil) <> 0;
I ended up with a function
function GetMovementArea: TRect;
var
MovementRect: TRect;
begin
if Application.MainForm.FormStyle = fsMDIForm then
Windows.GetWindowRect(Application.MainForm.ClientHandle, MovementRect)
else
SystemParametersInfo(SPI_GETWORKAREA, 0, #MovementRect, 0);
Result := MovementRect;
end;
This return a TRect in which my form may move around

AMFPHP overiding default function arguments?

I've got this odd problem.
If I make a call on that function through amfphp service browser and give it a valid ID and leave the $num_images field blank amfphp will actually pass a blank string as the argument.
// if i call this function width just an ID
function getWorkers($id, $num_images = 100) {
...
// num_images will be set as ''
}
I can easily override using a check:
function getWorkers($id, $num_images = 100) {
if($num_images=='') $num_images = 100;
...
// num_images will now be really set as 100
}
Anyone experiencing the same with amfphp?
That's odd, I never got that from AMFPHP. If you don't have the latest version try updating your installation of AMFPHP. Also make sure Flash doesn't somehow pass an empty variable as the second variable.
(Copied from the comment.)