Multiple questions : math.random, objects, events ...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Rodrigogogo
Prole
Posts: 5
Joined: Mon Apr 14, 2014 11:42 am

Multiple questions : math.random, objects, events ...

Post by Rodrigogogo »

Hello everybody !

I'm barely new with Love AND Lua, i hope i won't ask stupid questions !

First of all, i'm training on this framework with a small game. Basicaly, a little window appears on top of the screen with a text inside. It moves down and the user loses if he doesn't kill the text before it reaches him. To kill the text, you just have to type what the text says.

SO

I'm using love.math.random to generate a number (between 1 and the total lenght of my dictionnary) to pop a word on the screen. BUT whenever i compile my soft, the number i get is always the same ! I'm using the math.random in an independant function :

Code: Select all

function dico ()
liste={"test", "test1", "test2", "test3", "test4", "test5", "test6"}
Count = 0
for _ in pairs(liste) do
Count = Count + 1
end
n1=love.math.random(1, Count)
return liste[n1]
end
Am i doing something wrong ?

Second, is there a way to declare objects (i have a Java way of thinking about objects). So i could easely create a text-object with attributes and methods and everything .... I've already read the chapter 16 of LUA programming, but i don't get the method to actually construc an object ...

And for my last request, how do events are managed ?
For the moment, i'm using a frame counter in my love.update(dt)

Code: Select all

timer=0
function love.update(dt)
    timer = timer+dt
    timer = math.ceil(timer)
    print(timer)
end
And then in my love.draw() :

Code: Select all

if timer > 20 then
            love.graphics.print(mot2, positionx, positiony))
            positiony=positiony+1
end
I saw the love.event wiki, but the exemple show keyboard inputs, so i guessed i won't be able do manage the creation of my 'words' with that.. or can i ?

Thanks for the replies !
luv
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Multiple questions : math.random, objects, events ...

Post by Roland_Yonaba »

Hi,

First, for math.random, the result is fairly normal. Lua implements its own random number generator, and unless you provide it a custom seed, you will always get the same series of output.
Usually, what people do is providing a number depending on the actual time (os.time()) to math.randomseed.
Like this:

Code: Select all

math.randomseed(os.time())
then you can call math.random. Each time you re-run this script, you will get a new series of output, since os.time (being the actual time) changes continuously.

Note that, with Löve 0.9.0, you can use the new functions love.math.random and love.math.randomNormal. They are already automatically seeded, AFAIK, so you won't need to use math.randomseed.

Second, for OOP, Lua doesn't have built-in OOP, but it provides the mechanims you need to build your own flexible OOP solution.
You can also use the existing class libraries. The commonly used ones are MiddleClass, SECS, or 30log.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Multiple questions : math.random, objects, events ...

Post by DaedalusYoung »

Rodrigogogo wrote:

Code: Select all

liste={"test", "test1", "test2", "test3", "test4", "test5", "test6"}
Count = 0
for _ in pairs(liste) do
Count = Count + 1
end
n1=love.math.random(1, Count)
You can do this easier:

Code: Select all

liste = { "test1", "test2", "test3" }
n1 = love.math.random(#liste)
Rodrigogogo
Prole
Posts: 5
Joined: Mon Apr 14, 2014 11:42 am

Re: Multiple questions : math.random, objects, events ...

Post by Rodrigogogo »

Hey guys ! Thanks for the replies !

I've tried MiddleClass, but have a little pblm with it :

Code: Select all

Text=class('Text')
function Text:initialize(mot, y, speed)
    self.mot=mot
    self.y=y
    self.speed=speed
end
function Text:scrolldown()
self.y=self.y+(1*self.speed)
end
Why this is sending me an error 'attempt to index local 'self' (a nil value)' on the slef.y=self.y(1*self.speed) line ? Is "self" the same as "this" in Java ?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Multiple questions : math.random, objects, events ...

Post by Robin »

Look at the call site. Have you written text:scrollDown() or text.scrollDown()? You need to use a colon here.
Help us help you: attach a .love.
Rodrigogogo
Prole
Posts: 5
Joined: Mon Apr 14, 2014 11:42 am

Re: Multiple questions : math.random, objects, events ...

Post by Rodrigogogo »

The problem may come from me, but i have an error coming from the middleclass.lua :

./middleclass.lu:110: attempt to call method 'allocate (a nil value)

here is my Text.lua "class" :

Code: Select all

local class = require 'middleclass'


Text=class('Text')
function Text:initialize(mot, y, speed)
    self.mot=mot
    self.y=y
    self.speed=speed
    isActive=true
end

function Text:scrollDown()
	self.y=self.y+(1*self.speed)
end

function Text:setActiveFalse()
	self.isActive=false
 end

and the way i try to create a new instance :

Code: Select all

   
function love.load()
...
local t1=Text.new(mot[1],100,1)
...
end

function love.draw()
...
t1.scrollDown()
...
end
I don't see where is my mistake ...
User avatar
moikmellah
Prole
Posts: 12
Joined: Fri Jan 31, 2014 8:31 pm
Location: USA
Contact:

Re: Multiple questions : math.random, objects, events ...

Post by moikmellah »

One issue I can see is with scope - you're declaring 't1' as a local variable in love.load(), so it wouldn't exist when you try to invoke it in love.draw(). Try this instead:

Code: Select all

-- global declaration of t1
t1 = nil

function love.load()
  ...
  -- instantiate t1
  t1=Text.new(mot[1],100,1)
  ...
end

function love.draw()
  ...
  -- Call member methods with ':' rather than '.'
  t1:scrollDown()
  ...
end
Rodrigogogo
Prole
Posts: 5
Joined: Mon Apr 14, 2014 11:42 am

Re: Multiple questions : math.random, objects, events ...

Post by Rodrigogogo »

Now the error is on my Text.lua :

attempt to index local 'self' (a string value)

Code: Select all

local class = require 'middleclass'


Text=class('Text')
function Text:new(mot, y, speed)
--  error supposed to be juste below
    self.mot=mot                        
    self.y=y
    self.speed=speed
    isActive=true
end

function Text:scrollDown()
	self.y=self.y+(1*self.speed)
end

function Text:setActiveFalse()
	self.isActive=false
 end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Multiple questions : math.random, objects, events ...

Post by Robin »

Be consistent. Either define it as function Text.new(mot, y, speed) or call it like Text:new(mot[1],100,1).
Help us help you: attach a .love.
Rodrigogogo
Prole
Posts: 5
Joined: Mon Apr 14, 2014 11:42 am

Re: Multiple questions : math.random, objects, events ...

Post by Rodrigogogo »

I don't get it. If i use a Text.lua file to declare a class outside my main.lua, and define a function like this :

Code: Select all

Text=class('Text')
function Text.new(mot, y, speed)
    self.mot=mot
    self.y=y
    self:speed=speed
    isActive=true
end
I have to call it in my main.lua like :

Code: Select all

function love.load()
    t1=Text.new("test", 100, 1)
no ?

Cause this is giving me the error "attempt to index local 'self' (a string value)"
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest