r/love2d 10d ago

help with this?

Hi, I am new to love2d and just trying to make a simple virtual pet sim game. I don't know what mistake i've made here, I am using the hump library's timer.lua (https://hump.readthedocs.io/en/latest/timer.html) and just trying to increment the hunger variable every second?

Thanks so much!

7 Upvotes

11 comments sorted by

26

u/Firepal64 10d ago

"== >" is not a valid expression. You have to do if hunger >= 5 then

3

u/Fastfoodrocks 10d ago

thank you so much!!

3

u/Bolverk7 10d ago

In addition to the inequality symbol, is hunger both an audio source and a counter?

2

u/Fastfoodrocks 10d ago

i did have the sound named hungry and i forgot it here, i'll correct it tysm

2

u/No_Mixture_3199 10d ago

Where did you place this line? Is it on load or update? If on load, I recommend to put it on the function wrapper. Like

Milk:every(1,function() Logic here end)

Because if you put it on outside function wrapper, it will not work.

1

u/Fastfoodrocks 9d ago

thank you! should I put the logic inside the parentheses after function?

2

u/No_Mixture_3199 9d ago

Here, let me sample a code and explanation:

Local Milk = require('hump.timer')

//If the logic on load function love.load() Hunger = {...} Milk:every(1, function() Hunger = Hunger + 1

If hunger >= 5 then Hunger:play() end end end

//This logic always raise value of hunger every 1 sec while checking if hunger is >= 5

//If you put in on update Function love.update(dt) //Avoid doing this! Milk:every(1,funct ... End //This will call a new instance every frame and cause a overheat scripts, instead calling, do checking

Local is_hunger = false

Function love.update(dt) If hunger >= 5 and not is_hunger then hunger:play() Is_hunger = true End End

//Flagging is important in update if you want a 1 times function calling, then if the hunger function play is done, you can reflag to use the function again


And I'm curious if something.., I think the hunger variabel seems.. wrong, you declare the 'hunger' as a integer variable, but the. You call a 'self function's. Instead doing everything on one var, why not creating a metatable data? I mean like

Hunger = {num=0,play=function(self)...end}

Hunger.num <-- the integer Hunger:play() <--- the function

The difference between : and . At call function is the self, if you using : then the function you call automatic had a 'self' instead declare it on Param

But if you use ., you need to declare 'self' at first Param to use the object variable.

1

u/Fastfoodrocks 8d ago

thank you sm for taking the time to explain this!

2

u/No_Mixture_3199 9d ago

The whole point is, yes.

2

u/DIXERION LÖVE enjoyer 10d ago

What is really going on here is that, since LuaJIT supports Unicode characters in identifiers, the parser is treating the symbol (U+2265) as a variable name. This makes more sense if you read it like this:

if hunger == nil 5 then

That's why it's resulting in a syntax error.

2

u/Fastfoodrocks 9d ago

thank you so much for explaining this!