Gideros how to pass a function to Timer.delayCall? - timer

First, it is possible to do like this:
local delay = 1000
local timer = Timer.delayedCall(delay,
function(data)
print(data)
end,
20)
But, it seems not possible to do like this:
function Game:DoSomething(data)
print(data)
end
local timer = Timer.delayedCall(delay,
self.DoSomething,
20)
In other words, I would like to define the function outside (so to be reused by others). However, that seems not possible. Or did I do it wrong?

If what you want is have Timer.delayedCall call a method with multiple arguments in a generic way, you can do it like this:
function Game:DoSomething(data)
print(data)
end
function invokeMethod(args)
local func=table.remove(args,1)
func(unpack(args))
end
local timer = Timer.delayedCall(
delay,
invokeMethod,
{self.DoSomething,self,20}) --self is the instance you are calling
PS: this is my first post on SO, sorry if it isn't formatted correctly...

Related

How do I bulk/chunk paginate existing Seq[(String)] session value in Gatling?

I am executing a call that saves a lot of values into a Seq[(String)], it looks as follows:
.exec(session => {session.set("Ids", session("externalIds").as[Seq[String]])})
There is a reason why I have to create another session variable called Ids our of externalIds but I wont get into it now.
I than have to execute another call and paginate 10 values out of ${Ids} until I send them all.
(So in case of 100 values, I'll have to execute this call 10 times)
The JSON looks as follows:
..."Ids": [
"962950",
"962955",
"962959",
"962966",
"962971",
"962974",
"962978",
"962983",
"962988",
"962991"
],...
What I usually do when I have to iterate through one value each time is simply:
.foreach("${Ids}", "id") {
exec(getSomething)
}
But since I need to send a [...] Of 10 values each, I am not sure if it should even be in the scenario level. Help! :)
Use transform in your check to transform your Seq[String] into chunks, eg with Seq#grouped.
I couldn't figure out how to go about this within the session so I took it
outside to a function and here is the solution:
.exec(session => {session.set("idSeqList", convertFileIdSeqToFileIdSeqList(session("idsSeq").as[Seq[String]]))})
def convertFileIdSeqToFileIdSeqList(idSeq: Seq[String]): Seq[Seq[String]] = {
idSeq.grouped(10).toList
}
Note that when placing your list within a JSON body, you will need to use .jsonStringify() to format it correctly in the JSON context like so:
"ids": ${ids.jsonStringify()},

Passing Ruby array elements to a method

I'm not a programmer, but I find myself writing some simple ruby and aren't sure about a few things.
I have the following function
def resolve_name(ns_name)
ip = Resolv.getaddress(ns_name)
return ip
end
and the array
array = ['ns-1.me.com', 'ns-2.me.com']
What I want to do is to pass every element in the array to the function to be evaluated, and spit out to... something. Probably a variable. Once I have the resolved IPs I'll be passing them to an erb template. Not quite sure yet how to handle when there may be 1 to 4 possible results either.
What I want think I need to do is do an each.do and typecast to string into my function, but I haven't been able to figure out how to actually do that or phrase my problem properly for google to tell me.
http://ruby-doc.org/core-2.0.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversion Doesn't quite have what I'm looking for.
irb(main):010:0> resolved = resolve_name(array)
TypeError: no implicit conversion of Array into String
Any suggestions?
Take a look at the documentation for ruby's Enumerable, which arrays implement. What you're looking for is the map method, which takes each element of an enumerable (i.e. an array) and passes it to a block, returning a new array with the results of the blocks. Like this:
array.map{|element| resolve_name(element) }
As an aside, in your method, you do not need to use a local variable if all you're doing with it is returning its value; and the return statement is optional - ruby methods always return the result of the last executed statement. So your method could be shortened to this:
def resolve_name(ns_name)
Resolv.getaddress(ns_name)
end
and then you really all it's doing is wrapping a method call in another. So ultimately, you can just do this (with array renamed to ns_names to make it self-explanatory):
ns_names = ['ns-1.me.com', 'ns-2.me.com']
ip_addresses = ns_names.map{|name| Resolv.getaddress(name) }
Now ip_addresses is an array of IP addresses that you can use in your template.
If you pass an array you could do:
def resolve_name(ns_name)
res = []
ns_name.each do |n|
res << {name: n, ip: Resolv.getaddress(name) }
end
res
end
And get an array of hashes so you know which address has which ip

Access $firebaseObject value with variable instead of string

I need to assign a variable to a string and pass that variable to a Firebase query in a $firebaseObject. But, when I try this, the firebaseObject is null. I tried with directly putting the string in and it works, but I don't want that.
Code:
//var itemkey='-JyO7Zsgsucf5ESttJwt';
var itemkey=window.localStorage['fbid'];-------> string: '-JyO7Zsgsucf5ESttJwt'
console.log(typeof itemkey); ------------------> string
$scope.galphoto = $firebaseObject(ref.child(itemkey));
console.log($scope.galphoto) ------------------> null
When I call
$scope.galphoto = $firebaseObject(ref.child(itemkey));
With itemkey='-JyO7Zsgsucf5ESttJwt',
then console.log($scope.galphoto) is properly shown. However, when I use what I really want to use,
window.localStorage['fbid']
then console.log($scope.galphoto) is null.
Why?
I think you may be trying to console.log the result before it's available. $firebaseObject returns the data with some delay. You may try to do something like
$scope.galphoto = $firebaseObject(ref.child(itemkey));
$scope.galphoto.$loaded().then(function () {
console.log($scope.galphoto);
}
You should see the expected result, since you're waiting for the promise to respond. It could help you better understand how to get the desired result.

How to make a function wait X amount of time in LUA (Love2d)?

I am very new to programming and coming from a "custom map" background in games like SC2. I am currently trying to make a platformer game in Love2d. But I wonder how I can make something wait X amount of seconds before doing the next thing.
Say I want to make the protagonist immortal for 5 seconds, how should that code look like ?
Immortal = true
????????????????
Immortal = false
As I have understood there is no built in wait in Lua nor Love2d.
It sounds like you're interested in a temporary state for one of your game entities. This is pretty common - a powerup lasts for six seconds, an enemy is stunned for two seconds, your character looks different while jumping, etc. A temporary state is different than waiting. Waiting suggests that absolutely nothing else happens during your five seconds of immortality. It sounds like you want the game to continue as normal, but with an immortal protagonist for five seconds.
Consider using a "time remaining" variable versus a boolean to represent temporary states. For example:
local protagonist = {
-- This is the amount of immortality remaining, in seconds
immortalityRemaining = 0,
health = 100
}
-- Then, imagine grabbing an immortality powerup somewhere in the game.
-- Simply set immortalityRemaining to the desired length of immortality.
function protagonistGrabbedImmortalityPowerup()
protagonist.immortalityRemaining = 5
end
-- You then shave off a little bit of the remaining time during each love.update
-- Remember, dt is the time passed since the last update.
function love.update(dt)
protagonist.immortalityRemaining = protagonist.immortalityRemaining - dt
end
-- When resolving damage to your protagonist, consider immortalityRemaining
function applyDamageToProtagonist(damage)
if protagonist.immortalityRemaining <= 0 then
protagonist.health = protagonist.health - damage
end
end
Be careful with concepts like wait and timer. They typically refer to managing threads. In a game with many moving parts, it's often easier and more predictable to manage things without threads. When possible, treat your game like a giant state machine versus synchronizing work between threads. If threads are absolutely necessary, Löve does offer them in its love.thread module.
I normally use cron.lua for what you're talking about: https://github.com/kikito/cron.lua
Immortal = true
immortalTimer = cron.after(5, function()
Immortal = false
end)
and then just stick immortalTimer:update(dt) in your love.update.
You could do this:
function delay_s(delay)
delay = delay or 1
local time_to = os.time() + delay
while os.time() < time_to do end
end
Then you can just do:
Immortal == true
delay_s(5)
Immortal == false
Of course, it'll prevent you from doing anything else unless you run it in its own thread. But this is strictly Lua as I know nothing of Love2d, unfortunately.
I reccomend that you use hump.timer in your game,like this:
function love.load()
timer=require'hump.timer'
Immortal=true
timer.after(1,function()
Immortal=false
end)
end
instead of using timer.after,you can also use timer.script,like this:
function love.load
timer=require'hump.timer'
timer.script(function(wait)
Immortal=true
wait(5)
Immortal=false
end)
end
don't forget to add timer.updateinto function love.update!
function love.update(dt)
timer.update(dt)
end
hope this helped ;)
Download link:https://github.com/vrld/hump

Alternative to putting an include inside a While loop

I have a chunk of PHP code that I'd like to include on a number of different pages but be able to update in one location (hence my use of an include file). However, the chunk of code needs to appear inside a while loop -- specifically inside a while loop that is echoing out MySQL rows.
However, there are roughly 200 rows in the MySQL query I'm echoing, so having an include in the loop really slows things down. I've tried making what's in the include file a function, like shown below, then including once at the top of the page and referencing the function inside the loop, but it it doesn't seem to work (I just don't get any data in the variables I'm setting, etc.)
How does one put a chunk of code inside a loop without using include?
Thanks very much.
function CYCalc()
{
// If the company's current fiscal quarter
// is equal to the current calendar quarter,
// use the company's fiscal years as calendar years
if ($UniverseResult[CurQ] == "Q1" && $UniverseResult[CurYear] == "2012") {
$C2011Sales = number_format($UniverseResult[SalesYear2]/1000000,1);
$C2012Sales = number_format($UniverseResult[SalesYear3]/1000000,1);
$C2011EPS = $UniverseResult[EPSYear2];
$C2012EPS = $UniverseResult[EPSYear3];
}
}
Remember PHP's scoping rules. Variables defined in the global scope are not visible inside functions unless you explicitly declare them as global within the function:
<?php
$x = 7;
function y() {
echo $x; // undefined
}
function z() {
global $x;
echo $x; // 7
}
function a($x) {
echo $x; // 7
}
For your CYCalc() to work, you'd need to declare $UniverseResult global as per z() above, or pass it in as a parameter as per a() above.

Resources