Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

I must not have at least 1 brain cell... #2

Closed
TheDevster opened this issue Jun 10, 2021 · 6 comments
Closed

I must not have at least 1 brain cell... #2

TheDevster opened this issue Jun 10, 2021 · 6 comments

Comments

@TheDevster
Copy link

TheDevster commented Jun 10, 2021

I can't figure out why I cannot get this to work. I am missing something simple I am sure...

I will admit I am new to LUA and new to Redm/Fivem framework (Python / Ruby guy).

First, I have the resource added and started in the resource.cfg
At the top of my client and server I have placed this:
Callbacks = exports['callbacks']:FetchCallbacks()

I have a RegisterNUICallback() that calls the Trigger (I assumed this could work very similar to the register command example)

  local storeprice
 print("REGISTER NUI CALLBACK IS REACHED")
print("STORE ID IN NUI CALLBACK IS: ", store_id)
  Callbacks:TriggerCallback('get_store_cost', function(store_id)
    print("DATA SENT TO TRIGGER CALLBACK: ", store_id)
  end, storeprice)
  cb({price = storeprice})
end)

The two print statements before the Callbacks:TriggerCallback work. The third inside the Callbacks:TriggerCallback never gets called.

And on my Server side I have this:

  Callbacks:RegisterCallback('get_store_cost', function(idstore, source, cb)
    print("Made it here")
    MySQL.Async.fetchAll('select price, store_bank from stores WHERE store_id=@id_store', 
    {
      ['id_store'] = idstore
    }, function(results)
      if #results > 0 then 
        total_price = round_monies(results[1].price + results[1].store_bank)
        cb(total_price)
      end
    end)
  end)
end
registerCallbacks()

The print "Made it here" never gets executed.

@itsxScrubz
Copy link
Owner

It's because the syntax is Callbacks:TriggerCallback(callback_name, function, data_being_sent)

@itsxScrubz
Copy link
Owner

Since you flipped the data being returned with the data you're trying to send, the query isn't finding anything and therefor never triggering the callback function.

@TheDevster
Copy link
Author

So a modification, such as below should work right? Where callbackname is 'get_store_cost', function is where callback data will be stored (yes?), and the last parameters being the data from client that is being sent to server?

store is a table that gets sent as the data from client to the server.

 local storeprice
  Callbacks:TriggerCallback('get_store_cost', function(data) 
    print(data)
    storeprice = data
  end, store)

and my server side still good?

  Callbacks:RegisterCallback('get_store_cost', function(data, source, cb)
    print("Made it here")
    print("SOURCE", source)
    MySQL.Async.fetchAll('select price, store_bank from stores WHERE store_id=@id_store', 
    {
      ['id_store'] = source.id
    }, function(results)
      if #results > 0 then 
        total_price = round_monies(results[1].price + results[1].store_bank)
        cb(total_price)
      end
    end)
  end)

I assume with registering it's the same order of functions right?

@itsxScrubz
Copy link
Owner

['id_store'] = source.id

Since your sending data thru the callback, this should be ['id_store'] = data
function(data_sent_from_client, source, callback_function)

@TheDevster
Copy link
Author

TheDevster commented Jun 10, 2021

Nice! Ok I got it to work with your help! I really appreciate that. I do notice that if I change the code and restart my resource, I also have to restart the callback resource or I end up with errors like this:
[script:callbacks] SCRIPT ERROR: citizen:/scripting/lua/scheduler.lua:853: bad argument #1 to 'unpack' (table expected, got nil)

Hopefully I can gain some of my brain cells back now that I am heading in the correct direction..

@itsxScrubz
Copy link
Owner

It's due to how the resource is written. When you restart the callbacks, the table that holds them via: [ Callbacks = exports['callbacks']:FetchCallbacks() ] no longer exists. All you would have to do is just reassign the variable with the export again. These callbacks were in my framework, and when you restart a resource it automatically refreshes that component. Since they are standalone, this isn't the case!

But glad to be of help.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants